ArgParser

Declared in lemon/arg_parser.h, in the group Miscellaneous Tools; the same class on the Doxygen site.

class ArgParser

Command line arguments parser.

For a complete example see the arg_parser_demo.cc demo file.

Options

ArgParser & intOption(
const std::string & name,
const std::string & help,
int value = 0,
bool obl = false
)

Add a new integer type option.

Parameters:
  • name – The name of the option. The leading ‘-’ must be omitted.

  • help – A help string.

  • value – A default value for the option.

  • obl – Indicate if the option is mandatory.

ArgParser & doubleOption(
const std::string & name,
const std::string & help,
double value = 0,
bool obl = false
)

Add a new floating point type option.

Parameters:
  • name – The name of the option. The leading ‘-’ must be omitted.

  • help – A help string.

  • value – A default value for the option.

  • obl – Indicate if the option is mandatory.

ArgParser & boolOption(
const std::string & name,
const std::string & help,
bool value = false,
bool obl = false
)

Add a new bool type option.

Note

A mandatory bool obtion is of very little use.

Parameters:
  • name – The name of the option. The leading ‘-’ must be omitted.

  • help – A help string.

  • value – A default value for the option.

  • obl – Indicate if the option is mandatory.

ArgParser & stringOption(
const std::string & name,
const std::string & help,
std::string value = "",
bool obl = false
)

Add a new string type option.

Parameters:
  • name – The name of the option. The leading ‘-’ must be omitted.

  • help – A help string.

  • value – A default value for the option.

  • obl – Indicate if the option is mandatory.

ArgParser & other( const std::string & name , const std::string & help = "" )

Give help string for non-parsed arguments.

With this function you can give help string for non-parsed arguments. The parameter name will be printed in the short usage line, while help gives a more detailed description.

Options with External Storage

Using this functions, the value of the option will be directly written into a variable once the option appears in the command line.

ArgParser & refOption(
const std::string & name,
const std::string & help,
int & ref,
bool obl = false
)

Add a new integer type option with a storage reference.

Parameters:
  • name – The name of the option. The leading ‘-’ must be omitted.

  • help – A help string.

  • obl – Indicate if the option is mandatory.

  • ref – [out] The value of the argument will be written to this variable.

ArgParser & refOption(
const std::string & name,
const std::string & help,
double & ref,
bool obl = false
)

Add a new floating type option with a storage reference.

Parameters:
  • name – The name of the option. The leading ‘-’ must be omitted.

  • help – A help string.

  • obl – Indicate if the option is mandatory.

  • ref – [out] The value of the argument will be written to this variable.

ArgParser & refOption(
const std::string & name,
const std::string & help,
bool & ref,
bool obl = false
)

Add a new bool type option with a storage reference.

Note

A mandatory bool obtion is of very little use.

Parameters:
  • name – The name of the option. The leading ‘-’ must be omitted.

  • help – A help string.

  • obl – Indicate if the option is mandatory.

  • ref – [out] The value of the argument will be written to this variable.

ArgParser & refOption(
const std::string & name,
const std::string & help,
std::string & ref,
bool obl = false
)

Add a new string type option with a storage reference.

Parameters:
  • name – The name of the option. The leading ‘-’ must be omitted.

  • help – A help string.

  • obl – Indicate if the option is mandatory.

  • ref – [out] The value of the argument will be written to this variable.

Option Groups and Synonyms

ArgParser & optionGroup( const std::string & group , const std::string & opt )

Bundle some options into a group.

You can group some option by calling this function repeatedly for each option to be grouped with the same groupname.

Parameters:
  • group – The group name.

  • opt – The option name.

ArgParser & onlyOneGroup( const std::string & group )

Make the members of a group exclusive.

If you call this function for a group, than at most one of them can be given at the same time.

ArgParser & mandatoryGroup( const std::string & group )

Make a group mandatory.

Using this function, at least one of the members of group must be given.

ArgParser & synonym( const std::string & syn , const std::string & opt )

Create synonym to an option.

With this function you can create a synonym syn of the option opt.

Public Functions

ArgParser( int argc , const char * const * argv )

Constructor.

~ArgParser()
ArgParser & parse()

Start the parsing process.

inline ArgParser & run()

Synonym for parse().

inline const std::string & commandName() const

Give back the command name (the 0th argument).

inline bool given( std::string op ) const

Check if an opion has been given to the command.

inline RefType operator[]( const std::string & n ) const

Give back the value of an option.

See also

RefType

inline const std::vector< std::string > & files() const

Give back the non-option type arguments.

Give back a reference to a vector consisting of the program arguments not starting with a ‘-’ character.

inline void throwOnProblems()

Throw instead of exit in case of problems.

Protected Types

enum OptType

Values:

enumerator UNKNOWN
enumerator BOOL
enumerator STRING
enumerator DOUBLE
enumerator INTEGER
enumerator FUNC
typedef std::map< std::string , ParData > Opts
typedef std::map< std::string , GroupData > Groups

Protected Attributes

int _argc
const char * const * _argv
Opts _opts
Groups _groups
std::vector< OtherArg > _others_help
std::vector< std::string > _file_args
std::string _command_name
class ParData

Public Functions

inline ParData()

Public Members

bool * bool_p
int * int_p
double * double_p
std::string * string_p
void (*p)(void*)
void * data
struct lemon::ArgParser::ParData func_p
std::string help
bool mandatory
OptType type
bool set
bool ingroup
bool has_syn
bool syn
bool self_delete
class GroupData

Public Types

typedef std::list< std::string > Opts

Public Functions

inline GroupData()

Public Members

Opts opts
bool only_one
bool mandatory
struct OtherArg

Public Functions

inline OtherArg( std::string n , std::string h )

Public Members

std::string name
std::string help
class RefType

Magic type for operator[].

This is the type of the return value of ArgParser::operator[](). It automatically converts to int, double, bool or std::string if the type of the option matches, which is checked with an assertion (i.e. it performs runtime type checking).

Public Functions

inline RefType( const ArgParser & p , const std::string & n )

inline operator bool()

inline operator std::string()

inline operator double()

inline operator int()