#include <Pt/Arg.h>
Read and extract command-line options. More...
Inherits ArgBaseT< T >.
Public Member Functions | |
Arg (const T &def=T()) | |
Constructor with initial value. | |
Arg (int &argc, char *argv[], char ch, const T &def=T()) | |
Extract short option. More... | |
Arg (int &argc, char *argv[], const char *str, const T &def=T()) | |
Extract long option. More... | |
Arg (int &argc, char *argv[]) | |
Extracts the next parameter. | |
const T & | get () const |
Returns the value. | |
bool | isSet () const |
Returns true if the option is set, false if default is used. | |
bool | set (int &argc, char *argv[], char ch) |
Extract short option. More... | |
bool | set (int &argc, char *argv[], const char *str) |
Extract long option. More... | |
bool | set (int &argc, char *argv[]) |
Reads next parameter and removes it. | |
Related Functions | |
template<typename T > | |
std::ostream & | operator<< (std::ostream &out, const Arg< T > &arg) |
Write Arg to an std::ostream. | |
Arg objects can be used to process command line options passed to the main function of the program. A syntax for short-named and long-named options is supported. Short-named options start with a single hypen followd by a character (-O). Optionally, a value follows directly (-Ofoo) or separated by whitespace(-O foo). Alternatively, option names can be consist of any character sequence to support unix style options(–option) and windows style options (/OPTION). An optional value follows either separated by whitespace (–option yes) or an equal character (/OPTION=yes). Note that constructing an Arg from with the character 'n' or with the string "-n" are equivalent.
The template parameter of the Arg class is the argument value type, which must be streamable, i.e. the operator >> (std::istream&, T&) must be defined for the type T. When an Arg is constructed, the operator will be used to extract the value from the command-line string. the next example demonstrates this:
Options are removed from the option-list, so programs can easily check, if there are parameters left, after all options were extracted. A specialization exists for boolean parameters. This implements a switch, which is on, if the option is present and off, if it is missing. The option consists, in this case, only of a command line flag without a value. Boolean parameters can also be grouped, so -abc is processed like -a -b -c.
The example shown above not only shows a boolean parameter, but also how long-named options are handled, in this case "--debug".
Arg | ( | int & | argc, |
char * | argv[], | ||
char | ch, | ||
const T & | def = T() |
||
) |
argc | 1. parameter of main |
argv | 2. of main |
ch | optioncharacter |
def | default-value |
Example:
Arg | ( | int & | argc, |
char * | argv[], | ||
const char * | str, | ||
const T & | def = T() |
||
) |
Long option names starting with "--". This (and more) is supported here. Instead of giving a single option-character, you specify a string.
Example:
bool set | ( | int & | argc, |
char * | argv[], | ||
char | ch | ||
) |
argc | 1. parameter of main |
argv | 2. of main |
ch | optioncharacter |
Example:
bool set | ( | int & | argc, |
char * | argv[], | ||
const char * | str | ||
) |
GNU defines long options starting with "--". This (and more) is supported here. Instead of giving a single option-character, you specify a string.
Example: