#include <cxxtools/arg.h>
Public Member Functions | |
| Arg (bool def=false) | |
| Arg (int &argc, char *argv[], char ch, bool def=false) | |
| Arg (int &argc, char *argv[], const char *str, bool def=false) | |
| bool | set (int &argc, char *argv[], char ch) |
| bool | set (int &argc, char *argv[], const char *str) |
| bool | isTrue () const |
| bool | isFalse () const |
| operator bool () const | |
| bool | isSet () const |
Often programs need some switches, which are switched on or off. Users just enter a option without parameter.
example:
Arg<bool> debug(argc, argv, 'd'); if (debug) std::cout << "debug-mode is set" << std::endl;
| Arg | ( | bool | def = false |
) |
default constructor. Initializes value.
| def | initial value |
| Arg | ( | int & | argc, | |
| char * | argv[], | |||
| char | ch, | |||
| bool | def = false | |||
| ) |
extract parameter.
| argc | 1. parameter of main | |
| argv | 2. of main | |
| ch | optioncharacter | |
| def | default-value |
Arg<unsigned> offset(argc, argv, 'o', 0); unsigned value = offset.getValue();
| bool set | ( | int & | argc, | |
| char * | argv[], | |||
| char | ch | |||
| ) |
Use this constructor to extract a bool-parameter.
As a special case options can be grouped. The parameter is recognized also in a argument, which starts with a '-' and contains somewhere the given character.
example:
Arg<bool> debug(argc, argv, 'd'); Arg<bool> ignore(argc, argv, 'i');
Arguments debug and ignore are both set when the program is called with:
prog -id
prog -i -d
Options can also switched off with a following '-' like this:
prog -d-
In the program use:
Arg<bool> debug(argc, argv, 'd'); if (debug.isSet()) { if (debug) std::cout << "you entered -d" << std::endl; else std::cout << "you entered -d-" << std::endl; } else std::cout << "no -d option given" << std::endl;
This is useful, if a program defaults to some enabled feature, which can be disabled.
| bool set | ( | int & | argc, | |
| char * | argv[], | |||
| const char * | str | |||
| ) |
Setter for long-options.
The option-parameter is defined with a string. This can extract long-options like:
prog --debug
with
Arg<bool> debug(argc, argv, "--debug");
| bool isTrue | ( | ) | const |
returns true, if options is set.
| bool isFalse | ( | ) | const |
returns true, if options is not set.
| operator bool | ( | ) | const |
convertable to bool.
| bool isSet | ( | ) | const |
returns true, if option is explicitly set