G+Smo
24.08.0
Geometry + Simulation Modules
|
Class for command-line argument parsing.
A typical setup looks as follows:
Public Types | |
enum | updateType |
Options for gsOptionList::update. | |
Public Member Functions | |
void | addInt (const std::string &flag, const std::string &name, const std::string &desc, intVal_t &value) |
Register an int option for the command line. More... | |
void | addInt (const std::string &label, const std::string &desc, const index_t &value) |
Adds a option named label, with description desc and value value. More... | |
void | addMultiInt (const std::string &label, const std::string &desc, const std::vector< index_t > &values) |
Adds an option-group gn containing values of a std::vector. More... | |
void | addMultiInt (const std::string &flag, const std::string &name, const std::string &desc, std::vector< intVal_t > &value) |
Register an int option for the command line, which can be assigned more than once. More... | |
void | addMultiReal (const std::string &flag, const std::string &name, const std::string &desc, std::vector< real_t > &value) |
Register a real option for the command line, which can be assigned more than once. More... | |
void | addMultiString (const std::string &flag, const std::string &name, const std::string &desc, std::vector< std::string > &value) |
Register a string option for the command line, which can be assigned more than once. More... | |
void | addNewInt (const std::string &flag, const std::string &name, const std::string &desc, intVal_t value) |
Second version: unbinded integer command-line argument. | |
void | addPlainString (const std::string &name, const std::string &desc, std::string &value) |
Register a string parameter that has to be given directly (not as an option, i.e., not after a flag starting with "-" or "--") More... | |
void | addReal (const std::string &label, const std::string &desc, const Real &value) |
Adds a option named label, with description desc and value value. More... | |
void | addReal (const std::string &flag, const std::string &name, const std::string &desc, real_t &value) |
Register a real option for the command line. More... | |
void | addString (const std::string &label, const std::string &desc, const std::string &value) |
Adds a option named label, with description desc and value value. More... | |
void | addString (const std::string &flag, const std::string &name, const std::string &desc, std::string &value) |
Register a string option for the command line. More... | |
void | addSwitch (const std::string &label, const std::string &desc, const bool &value) |
Adds a option named label, with description desc and value value. More... | |
void | addSwitch (const std::string &flag, const std::string &name, const std::string &desc, bool &value) |
Register a switch option for the command line. More... | |
void | addSwitch (const std::string &name, const std::string &desc, bool &value) |
Register a switch option for the command line without flag. More... | |
index_t | askInt (const std::string &label, const index_t &value=0) const |
Reads value for option label from options. More... | |
Real | askReal (const std::string &label, const Real &value=0) const |
Reads value for option label from options. More... | |
std::string | askString (const std::string &label, const std::string &value="") const |
Reads value for option label from options. More... | |
bool | askSwitch (const std::string &label, const bool &value=false) const |
Reads value for option label from options. More... | |
std::vector< OptionListEntry > | getAllEntries () const |
Provides a list of all entries as vector of gsOptionList::OptionListEntry structs. | |
bool | getExceptionHandling () const |
Gets the exception handling status (true/false) | |
gsOptionList | getGroup (const std::string &gn) const |
Creates a new gsOptionList, where only the options from the group gn are taken. In the result, the groupname and the corresponding dot are removed. More... | |
const index_t & | getInt (const std::string &label) const |
Reads value for option label from options. More... | |
std::string & | getMessage () |
Returns the program's description (as specified in the constructor) | |
std::vector< index_t > | getMultiInt (const std::string &gn) const |
Reads values of an option-group gn into a std::vector. More... | |
std::vector< Real > | getMultiReal (const std::string &gn) const |
Reads values of an option-group gn into a std::vector. More... | |
std::vector< std::string > | getMultiString (const std::string &gn) const |
Reads values of an option-group gn into a std::vector. More... | |
Real | getReal (const std::string &label) const |
Reads value for option label from options. More... | |
std::string | getString (const std::string &label) const |
Reads value for option label from options. More... | |
bool | getSwitch (const std::string &label) const |
Reads value for option label from options. More... | |
void | getValues (int argc, char *argv[]) |
Parses the command line based on the specified parameters. More... | |
gsCmdLine (const std::string &message, const char delimiter= ' ', bool helpAndVersion=true) | |
Command line constructor. Defines how the arguments will be parsed. More... | |
bool | hasGlobals () const |
Checks if there are labels that do not belong to a group. More... | |
bool | hasGroup (const std::string &gn) const |
Checks if there are labels that belong to the group gn. More... | |
std::ostream & | print (std::ostream &os) const |
Prints this list of options to stream os. | |
void | remove (const std::string &label) |
Removes the option named label (if it exists). | |
void | setExceptionHandling (const bool state) |
Sets exception handling (true/false) | |
void | setInt (const std::string &label, const index_t &value) |
Sets an existing option label to be equal to value. More... | |
void | setReal (const std::string &label, const Real &value) |
Sets an existing option label to be equal to value. More... | |
void | setString (const std::string &label, const std::string &value) |
Sets an existing option label to be equal to value. More... | |
void | setSwitch (const std::string &label, const bool &value) |
Sets an existing option label to be equal to value. More... | |
size_t | size () const |
Returns the length of this list of options. | |
void | swap (gsOptionList &other) |
Swaps contents. | |
void | toggleSwitch (const std::string &label) |
Toggles switch ( boolean ) | |
void | update (const gsOptionList &other, updateType type=ignoreIfUnknown) |
Updates the object using the data from other. More... | |
bool | valid (int argc, char *argv[]) const |
gsOptionList | wrapIntoGroup (const std::string &gn) const |
Creates a new gsOptionList where all labels are wrapped into a groupname gn. More... | |
Static Public Member Functions | |
static void | printVersion () |
Prints the version information. | |
Private Member Functions | |
void | updateOptionList () |
gsCmdLine | ( | const std::string & | message, |
const char | delimiter = ' ' , |
||
bool | helpAndVersion = true |
||
) |
Command line constructor. Defines how the arguments will be parsed.
message | The message to be used in the usage output. |
delimiter | The character that is used to separate the argument flag/name from the value. Defaults to ' ' (space). |
helpAndVersion | Whether or not to create the Help and Version switches. Defaults to true. |
The options that are allowed in the command line have to be defined using the member functions addInt, addMultiInt, addReal, addMultiReal, addString, addMultiString, addSwitch and addPlainString. Finally, the parsing is invoked by calling getValues.
void addInt | ( | const std::string & | flag, |
const std::string & | name, | ||
const std::string & | desc, | ||
intVal_t & | value | ||
) |
Register an int option for the command line.
flag | One character flag for using the option. If empty, no such flag can be used. |
name | Long form of the flag. |
desc | Description (printed if –help is invoked) |
value | This should be a non-const variable, initialized with the default value. When getValues is invoked and the user provided a value, it is written to that variable. |
If the flag is "n", the user might call "-n 10" at the command line. It the name is "size", the user might call "--size 10" at the command line.
|
inherited |
Adds a option named label, with description desc and value value.
If an option with label already exists with the same type, the function overwrites it. If it has another type, the function throws.
|
inherited |
Adds an option-group gn containing values of a std::vector.
If gn is not found, the function throws.
void addMultiInt | ( | const std::string & | flag, |
const std::string & | name, | ||
const std::string & | desc, | ||
std::vector< intVal_t > & | value | ||
) |
Register an int option for the command line, which can be assigned more than once.
flag | One character flag for using the option. If empty, no such flag can be used. |
name | Long form of the flag. |
desc | Description (printed if –help is invoked) |
value | This should be a non-const vector. When getValues is invoked, the vector is filled with that values. Pre-existing values are kept only if the option has been used zero times. |
If the flag is "n", the user might call "-n 10" at the command line. It the name is "size", the user might call "--size 10" at the command line.
void addMultiReal | ( | const std::string & | flag, |
const std::string & | name, | ||
const std::string & | desc, | ||
std::vector< real_t > & | value | ||
) |
Register a real option for the command line, which can be assigned more than once.
flag | One character flag for using the option. If empty, no such flag can be used. |
name | Long form of the flag. |
desc | Description (printed if –help is invoked) |
value | This should be a non-const vector. When getValues is invoked, the vector is filled with that values. Pre-existing values are kept only if the option has been used zero times. |
If the flag is "t", the user might call "-t .5" at the command line. It the name is "tau", the user might call "--tau .5" at the command line.
void addMultiString | ( | const std::string & | flag, |
const std::string & | name, | ||
const std::string & | desc, | ||
std::vector< std::string > & | value | ||
) |
Register a string option for the command line, which can be assigned more than once.
flag | One character flag for using the option. If empty, no such flag can be used. |
name | Long form of the flag. |
desc | Description (printed if –help is invoked) |
value | This should be a non-const vector. When getValues is invoked, the vector is filled with that values. Pre-existing values are kept only if the option has been used zero times. |
If the flag is "f", the user might call "-f foo.xml" at the command line. If the name is "file", the user might call "--file foo.xml" at the command line.
void addPlainString | ( | const std::string & | name, |
const std::string & | desc, | ||
std::string & | value | ||
) |
Register a string parameter that has to be given directly (not as an option, i.e., not after a flag starting with "-" or "--")
name | Name of the option. |
desc | Description (printed if –help is invoked) |
value | This should be a non-const variable. When getValues is invoked, the user-provided value is written to that variable. |
You must not declare more than one plain string.
|
inherited |
Adds a option named label, with description desc and value value.
If an option with label already exists with the same type, the function overwrites it. If it has another type, the function throws.
void addReal | ( | const std::string & | flag, |
const std::string & | name, | ||
const std::string & | desc, | ||
real_t & | value | ||
) |
Register a real option for the command line.
flag | One character flag for using the option. If empty, no such flag can be used |
name | Long form of the flag |
desc | Description (printed if –help is invoked) |
value | This should be a non-const variable, initialized with the default value. When getValues is invoked and the user provided a value, it is written to that variable. |
If the flag is "t", the user might call "-t .5" at the command line. It the name is "tau", the user might call "--tau .5" at the command line.
|
inherited |
Adds a option named label, with description desc and value value.
If an option with label already exists with the same type, the function overwrites it. If it has another type, the function throws.
void addString | ( | const std::string & | flag, |
const std::string & | name, | ||
const std::string & | desc, | ||
std::string & | value | ||
) |
Register a string option for the command line.
flag | One character flag for using the option. If empty, no such flag can be used. |
name | Long form of the flag. |
desc | Description (printed if –help is invoked) |
value | This should be a non-const variable, initialized with the default value. When getValues is invoked and the user provided a value, it is written to that variable. |
If the flag is "f", the user might call "-f foo.xml" at the command line. If the name is "file", the user might call "--file foo.xml" at the command line.
|
inherited |
Adds a option named label, with description desc and value value.
If an option with label already exists with the same type, the function overwrites it. If it has another type, the function throws.
void addSwitch | ( | const std::string & | flag, |
const std::string & | name, | ||
const std::string & | desc, | ||
bool & | value | ||
) |
Register a switch option for the command line.
flag | One character flag for using the option. If empty, no such flag can be used. |
name | Long form of the flag. |
desc | Description (printed if –help is invoked) |
value | This should be a non-const bool variable. When getValues is invoked and the user has added the switch on the command line, the value is toggled (i.e. if false it becomes true, if true it becomes false) |
If the flag is "l", the user might call "-l" at the command line. If the name is "log", the user might call "--log" at the command line.
|
inline |
Register a switch option for the command line without flag.
Reads value for option label from options.
If label is not found, it defaults to value (otherwise value is not used).
|
inherited |
Reads value for option label from options.
If label is not found, it defaults to value (otherwise value is not used).
|
inherited |
Reads value for option label from options.
If label is not found, it defaults to value (otherwise value is not used).
|
inherited |
Reads value for option label from options.
If label is not found, it defaults to value (otherwise value is not used).
|
inherited |
Creates a new gsOptionList, where only the options from the group gn are taken. In the result, the groupname and the corresponding dot are removed.
If the groupname is "IterativeSolver", then a label "InterativeSolver.Tolerance" becomes "Tolerance" and a label "Basis.Degree" is ignored.
|
inherited |
Reads value for option label from options.
If label is not found, the function throws.
|
inherited |
Reads values of an option-group gn into a std::vector.
If gn is not found, the function throws.
|
inherited |
Reads values of an option-group gn into a std::vector.
If gn is not found, the function throws.
|
inherited |
Reads values of an option-group gn into a std::vector.
If gn is not found, the function throws.
|
inherited |
Reads value for option label from options.
If label is not found, the function throws.
|
inherited |
Reads value for option label from options.
If label is not found, the function throws.
|
inherited |
Reads value for option label from options.
If label is not found, the function throws.
void getValues | ( | int | argc, |
char * | argv[] | ||
) |
Parses the command line based on the specified parameters.
The specification has to be done using addInt, addMultiInt, addReal, addMultiReal, addString, addMultiString, addSwitch and addPlainString before calling this member function.
The parameters argc and argv are those of the main function.
If the parsing did non succeed, the function throws.
|
inherited |
Checks if there are labels that do not belong to a group.
This is the case if there is a label which does not contain a dot.
|
inherited |
Checks if there are labels that belong to the group gn.
This is the case if there is a label which starts with the groupname and a dot.
|
inherited |
Sets an existing option label to be equal to value.
If label is not found, the function throws.
|
inherited |
Sets an existing option label to be equal to value.
If label is not found, the function throws.
|
inherited |
Sets an existing option label to be equal to value.
If label is not found, the function throws.
|
inherited |
Sets an existing option label to be equal to value.
If label is not found, the function throws.
|
inherited |
Updates the object using the data from other.
Options which do not exist in other, are kept unchanged. Options in other which do not exist in this, are kept unchanged if type is set to gsOptionList::ignoreIfUnknown (default) or are added if type is set to gsOptionList::addIfUnknown.
|
private |
Writes all given options (as specified by addInt, addReal, addString or addSwitch or addPlainString) into the gsOptionList base object.
bool valid | ( | int | argc, |
char * | argv[] | ||
) | const |
Parses the command line and returns true iff everything is okey. This function should be called after the parameters have been registered.
If the user has invoked –help or –version, the result is true.
|
inherited |
Creates a new gsOptionList where all labels are wrapped into a groupname gn.
Wrapping means that the label is prepended with the groupname and a dot. So, the label "Tolerance" wrapped into the group "IterativeSolver" is "InterativeSolver.Tolerance"