#include <cxxtools/arg.h>
Public Member Functions | |
| Arg (const T &def=T()) | |
| Arg (int &argc, char *argv[], char ch, const T &def=T()) | |
| Arg (int &argc, char *argv[], const char *str, const T &def=T()) | |
| Arg (int &argc, char *argv[]) | |
| bool | set (int &argc, char *argv[], char ch) |
| bool | set (int &argc, char *argv[], const char *str) |
| bool | set (int &argc, char *argv[]) |
| const T & | getValue () const |
| operator T () const | |
| bool | isSet () const |
Programs usually need some parameters. Usually they start with a '-' followed by a single character and optionally a value. Arg<T> extracts these and other parametes.
This default class processes paramters with a value, which defines a input-extractor-operator operator>> (istream&, T&).
Options are removed from the option-list, so programs can easily check after all options are extracted, if there are parameters left.
example:
int main(int argc, char* argv[]) { cxxtools::Arg<int> option_n(argc, argv, 'n', 0); std::cout << "value for -n: " << option_n << endl; }
| Arg | ( | const T & | def = T() |
) |
default constructor. Initializes value.
| def | initial value |
| Arg | ( | int & | argc, | |
| char * | argv[], | |||
| char | ch, | |||
| const T & | def = T() | |||
| ) |
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();
| Arg | ( | int & | argc, | |
| char * | argv[], | |||
| const char * | str, | |||
| const T & | def = T() | |||
| ) |
GNU defines long options starting with "--". This (and more) is supported here. Instead of giving a single option-character, you specify a string.
example:
Arg<int> option_number(argc, argv, "--number", 0); std::cout << "number =" << option_number.getValue() << std::endl;
| bool set | ( | int & | argc, | |
| char * | argv[], | |||
| char | ch | |||
| ) |
extract parameter.
| argc | 1. parameter of main | |
| argv | 2. of main | |
| ch | optioncharacter |
Arg<unsigned> offset;
offset.set(argc, argv, 'o');
unsigned value = offset.getValue();
| 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:
Arg<int> option_number;
number.set(argc, argv, "--number");
std::cout << "number =" << option_number.getValue() << std::endl;
| bool set | ( | int & | argc, | |
| char * | argv[] | |||
| ) |
Reads next parameter and removes it.
| const T& getValue | ( | ) | const |
returns the value.
| operator T | ( | ) | const |
returns the value.
Instead of calling getValue() the argument can be converted implicitly.
example:
void print(int i) { std::cout << i << std::endl; } int main(int argc, char* argv[]) { cxxtools::Arg<int> value(argc, argv, 'v', 0); print(value); // pass argument as a int to the function }
| bool isSet | ( | ) | const |
Liefert true zurück, wenn die Option gefunden wurde, also nicht der Default-Wert zum Einsatz kam.