next up previous contents
Next: Flags Up: GetPot Version 1.0 Powerful Previous: Directly followed options   Contents


Variables

Variables are a very elegant feature to pass arguments to your program. In the style of 'awk' one can specify some variables, assign values to them, and read them from inside your program. A 'variable' is defined on the command line like

variable-name '=' value.

Note that no blanks are allowed. Quotes can be used, though, to pass longer strings or vectors. Variables are accessed through the function call operator. In the same way as the next() and follow() function groups, the function call operator determines its return type through the default argument. By a code fragment like

...
     const double  initial_velocity = cl("v0", 10.);              // [m/s]
     const char*   integration_type = cl("integration", "euler");
     const int     no_samples_per_sec = cl("samples", 100);       // [1/s]
...
one can parse a command line like:
> myprog ... v0=14.56 integration=runge-kutta samples=500 ...
GetPot, can do even more. It is able to treat input vectors, of mixed type. This allows to have command line arguments like
> myprog ... sample-interval='-10. 10. 400' \
             color-mode='32 RGB 0.4 0.2 0.5' ...

Vectors variables are also read with the function call operator. However, the position in the vector has to be specified by an index. The total number of elements in a vector can be determined through the function vector_variable_size(VariableName). Here is an example of how to parse the above command line:

...
    if( cl.vector_variable_size("sample-interval") < 1 ) {
       cerr << "error: sample interval, argument not the following format:\n";
       cerr << "       MINIMUM MAXIMUM NO_SAMPLES\n".
       cerr << "       need at least the minimum !\n";
    }
    const double Min = cl("sample-interval", 0., 0);        // [s] default 0 sec
    const double Max = cl("sample-interval", 10., 1);       // [s] default 10 sec
    const int    NoSamples = cl("sample-interval", 200, 2); // no of samples
...
    const int    BPP  = cl("color-mode", 64, 0);
    const char*  Mode = cl("color-mode", "Grayscale", 1);
    if( ! strcmp(Mode,"RGB") && BPP < 24 ) 
       cerr << "RGB not possible with less than 24 bits per pixel.\n";
...
The third argument to the ()-operator specifies the position in the vector. The first element of the vector 'sample-interval' (index 0) defines the minimum Min. The second argument the maximum (index 1) and the third element (index 2) the number of samples. Respectively the ' color-mode' information is parsed.


next up previous contents
Next: Flags Up: GetPot Version 1.0 Powerful Previous: Directly followed options   Contents
Frank-Rene Schaefer 2002-09-14