Loading...
Searching...
No Matches
ArgParser Class Reference
Tools and Utilities » Miscellaneous Tools

## Detailed Description

Command line arguments parser.

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

`#include <lemon/arg_parser.h>`

## Classes

class | RefType
| Magic type for operator[]. More...

## Public Member Functions

| ArgParser (int argc, const char *const *argv)
| Constructor.
ArgParser & | parse ()
| Start the parsing process.
ArgParser & | run ()
| Synonym for parse().
const std::string & | commandName () const
| Give back the command name (the 0th argument).
bool | given (std::string op) const
| Check if an opion has been given to the command.
RefType | operator[] (const std::string &n) const
| Give back the value of an option.
const std::vector< std::string > & | files () const
| Give back the non-option type arguments.
void | throwOnProblems ()
| Throw instead of exit in case of problems.
Options
ArgParser & | intOption (const std::string &name, const std::string &help, int value=0, bool obl=false)
| Add a new integer type option.
ArgParser & | doubleOption (const std::string &name, const std::string &help, double value=0, bool obl=false)
| Add a new floating point type option.
ArgParser & | boolOption (const std::string &name, const std::string &help, bool value=false, bool obl=false)
| Add a new bool type option.
ArgParser & | stringOption (const std::string &name, const std::string &help, std::string value="", bool obl=false)
| Add a new string type option.
ArgParser & | other (const std::string &name, const std::string &help="")
| Give help string for non-parsed arguments.
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.
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.
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.
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.
Option Groups and Synonyms
ArgParser & | optionGroup (const std::string &group, const std::string &opt)
| Bundle some options into a group.
ArgParser & | onlyOneGroup (const std::string &group)
| Make the members of a group exclusive.
ArgParser & | mandatoryGroup (const std::string &group)
| Make a group mandatory.
ArgParser & | synonym (const std::string &syn, const std::string &opt)
| Create synonym to an option.

## Member Function Documentation

## intOption()

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.

## doubleOption()

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.

## boolOption()

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

Add a new bool 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.

Note
  A mandatory bool obtion is of very little use.

## stringOption()

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.

## other()

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

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.

## refOption() [1/4]

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.

Return values

  ref | The value of the argument will be written to this variable.

## refOption() [2/4]

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.

Return values

  ref | The value of the argument will be written to this variable.

## refOption() [3/4]

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.

Parameters

  name | The name of the option. The leading '-' must be omitted.
  help | A help string.
  obl | Indicate if the option is mandatory.

Return values

  ref | The value of the argument will be written to this variable.

Note
  A mandatory bool obtion is of very little use.

## refOption() [4/4]

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.

Return values

  ref | The value of the argument will be written to this variable.

## optionGroup()

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

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.

## onlyOneGroup()

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

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

## mandatoryGroup()

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

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

## synonym()

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

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

## operator[]()

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

inline

Give back the value of an option.

See also
  RefType

## files()

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

inline

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