next up previous contents
Next: Arguments that follow arguments Up: GetPot Version 1.0 Powerful Previous: Overview   Contents

Options

The easiest way to check if an argument is specified on the command line is to use the search()-function. It returns true in case the option is found and false in case it was not. In addition to that, GetPot provides a search() function with a variable argument list allowing to check elegantly for multiple options that are equivalent. Example:

...
     bool  be_nice_f = cl.search("--nice");
     if( cl.search("--do-nothing") ) exit(0);
     if( cl.search(4, "--help", "-h", "--hilfe", "--sos") ) {
        // print some information about how the program works
     }
...
The '4' as a first argument to search() indicates that four strings follow that represent equivalent options. search() functions belong to a class of cursor related functions. Cursor related functions are a very convenient means to parse the command line. In order to understand how they work, one has to understand how command line arguments are lined up. A command line given as
 > hello aux --recompile ... my_input.txt
is stored in an array as shown in [*]. The the search() functions set the cursor to a certain position in the array. Others increase the cursor position. The two member functions
 void         disable_loop();
 void         enable_loop();
allow to specify if the cursor is allowed to go back to the beginning, if no match is found until the end of the array. The default behavior is 'yes'. In case one needs to allow multiple occurrence of an option (e.g. as -I in gcc), the wrapping has to be turned off to avoid parsing the same option twice. Before parsing these kinds of options, one has to reset the cursor position by the function:
 void         reset_cursor();

Figure 1: Command line arguments lined up in the argv-array. A cursor iterates over the elements.
\includegraphics[width=\textwidth]{argv-array.eps}



Subsections
next up previous contents
Next: Arguments that follow arguments Up: GetPot Version 1.0 Powerful Previous: Overview   Contents
Frank-Rene Schaefer 2002-09-14