HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ArgParse::Arg Class Reference

#include <argparse.h>

Public Member Functions

 Arg (ArgParse &ap)
 
 Arg (const Arg &)=delete
 
const Argoperator= (const Arg &)=delete
 
Arghelp (string_view help)
 Set the help / description of this command line argument. More...
 
Argnargs (int n)
 
Argmetavar (string_view name)
 
Argdest (string_view dest)
 
template<typename T >
Argdefaultval (const T &val)
 
Arghidden ()
 Mark the argument as hidden from the help message. More...
 
Argstore_true ()
 
Argstore_false ()
 
Argaction (ArgAction &&func)
 Add an arbitrary action: func(Arg&, cspan<const char*>) More...
 
Argaction (Action &&func)
 Add an arbitrary action: func(cspan<const char*>) More...
 
Argaction (void(*func)())
 Add an arbitrary action: func() More...
 
Argaction (int(*func)(int, const char **))
 
string_view name () const
 Return the name of the argument. More...
 
string_view dest () const
 
ArgParseargparse ()
 Get a reference to the ArgParse that owns this Arg. More...
 

Protected Attributes

ArgParsem_argparse
 

Detailed Description

A call to ArgParse::arg() returns an Arg&. There are lots of things you can do to that reference to modify it. Nearly all Arg methods return an Arg& so you can chain the calls, like this:

ArgParse ap;
...
ap.add_argument("-v")
  .help("Verbose mode")
  .action(Arg::store_true());

Definition at line 404 of file argparse.h.

Constructor & Destructor Documentation

ArgParse::Arg::Arg ( ArgParse ap)
inline

Definition at line 408 of file argparse.h.

ArgParse::Arg::Arg ( const Arg )
delete

Member Function Documentation

Arg& ArgParse::Arg::action ( ArgAction &&  func)

Add an arbitrary action: func(Arg&, cspan<const char*>)

Arg& ArgParse::Arg::action ( Action &&  func)
inline

Add an arbitrary action: func(cspan<const char*>)

Definition at line 513 of file argparse.h.

Arg& ArgParse::Arg::action ( void(*)()  func)
inline

Add an arbitrary action: func()

Definition at line 528 of file argparse.h.

Arg& ArgParse::Arg::action ( int(*)(int, const char **)  func)
inline

Definition at line 534 of file argparse.h.

ArgParse& ArgParse::Arg::argparse ( )
inline

Get a reference to the ArgParse that owns this Arg.

Definition at line 549 of file argparse.h.

template<typename T >
Arg& ArgParse::Arg::defaultval ( const T val)
inline

Initialize the destination attribute with a default value. Do not call .dest("name") on the argument after calling defaultval, of it will end up with the default value in the wrong attribute.

Definition at line 478 of file argparse.h.

Arg& ArgParse::Arg::dest ( string_view  dest)

Override the destination attribute name (versus the default which is simply the name of the command line option with the leading dashes stripped away). The main use case is if you want two differently named command line options to both store values into the same attribute. It is very important that if you use dest(), you call it before setting the action.

Example:

// Add -v argument, but store its result in `ap["verbose"]`
// rather than the default `ap["v"]`.
ap.add_argument("-v")
  .help("verbose mode")
  .dest("verbose")
  .action(ArgParse::store_true());
string_view ArgParse::Arg::dest ( ) const

Return the "destination", the name of the attribute in which the argument value will be stored.

Arg& ArgParse::Arg::help ( string_view  help)

Set the help / description of this command line argument.

Arg& ArgParse::Arg::hidden ( )

Mark the argument as hidden from the help message.

Arg& ArgParse::Arg::metavar ( string_view  name)

Set the name(s) of any argument parameters as they are printed in the help message. For arguments that take multiple parameters, just put spaces between them. Note that the number of arguments is inferred by the number of metavar names, so there is no need to set nargs separately if metavar() is called properly.

Examples:

ArgParse ap;
ap.add_argument("--aa")
  .help("set sampling rate (per pixel)")
  .metavar("SAMPLES");
ap.add_argument("--color")
  .help("set the diffuse color")
  .metavar("R G B");

Will print help like:

--aa SAMPLES       set sampling rate (per pixel)
--color R G B      set the diffuse color
string_view ArgParse::Arg::name ( ) const

Return the name of the argument.

Arg& ArgParse::Arg::nargs ( int  n)

Set the number of subsequent parameters to this argument. Setting nargs(0) means it is a flag only, with no parameters.

const Arg& ArgParse::Arg::operator= ( const Arg )
delete
Arg& ArgParse::Arg::store_false ( )
inline

Set the action for this argument to store 0 in the destination attribute. Initialize the destination attribute to 1 now. Do not call .dest("name") on the argument after calling store_false, you must override the destination first!

Definition at line 502 of file argparse.h.

Arg& ArgParse::Arg::store_true ( )
inline

Set the action for this argument to store 1 in the destination attribute. Initialize the destination attribute to 0 now. Do not call .dest("name") on the argument after calling store_true, you must override the destination first!

Definition at line 491 of file argparse.h.

Member Data Documentation

ArgParse& ArgParse::Arg::m_argparse
protected

Definition at line 552 of file argparse.h.


The documentation for this class was generated from the following file: