When an argument is found by search(), the cursor is set to its position. Now, the next() functions allow to parse through terms that follow. A line like
> myprog --size 14.2341 -i fichier.txtcan be parsed using search() and next() as follows:
... if( ! cl.search("--size") ) return; const double XSize = cl.next(0.); if( ! cl.search(2, "-i", "--file") ) return; const char* infile = cl.next("dummy.txt"); ...Note that the desired return type is specified by the default argument. XSize is of type double so the default argument for next() is of type double. If the argument following '-size' cannot be converted to a double, then the default value '0.' will be assigned to XSize. Respectively the same thing happens with infile where 'dummy.txt' specifies that a const char* parameter is required.
The above code may still seem a bit cumbersome. That is why GetPot provides the follow() functions. These functions combine the search for an argument with the search for a following argument. The above code can be rewritten as:
... const double XSize = cl.follow(0., "--xsize"); const char* infile = cl.follow("dummy.txt", 2, "-i","--file"); ...
The search() and follow() functions set the cursor to the next argument that matches, starting from the current cursor position. These functions can also be used to parse arguments that occur multiple times. The following code shows how multiple occurrence of an argument can be parsed.
... vector<double> accelerations; // [m/s^2] accelerations to be tried const double v_d = cl.follow("-a", 9.81); // 9.81 [m/s^2] is forbidden // read all arguments that start with '-I' cl.init_multiple_occurrence(); while(fabs(v_d) <= 9.81) accelerations.push_back(cl.follow("-a", 9.81)); // re-enable wrapping around for following parsing operations ... cl.enable_loop(); ...Since every time when one needs to parse options with multiple occurrence, one needs to call disable_loop() and reset_cursor(), these two functions are combined into one, for convenience:
void init_multiple_occurrence();The above code parses command lines like:
> myprog ... -a 3.1 ... -a -8.2 ... -a 0.2 ... -a 1.02 ...