The easiest way to use the command line library is to use the GetSingeValue function.
Calling aCmdLineObj.GetSingleValue(-N, nMyIntVar) will set nMyIntVar to the value given at the command line for option -N.
If your program's user uses -N 17 as part of the command line, then nMyIntVar will be set to 17. It will be left untouched, if the user does not use the option -N.
A complete working example looks like this:
#include <iostream> #include <string> #include "cmdline.h" using namespace std; using namespace cmdl; // defined in cmdline.h int main(int argc, char *argv[]) { int nTest = 0; string strTest; CmdLine CL; CL.Init( --argc, ++argv ); // skip program name (argv[0]) // set nTest is set to an int-value, if given at the command line CL.GetSingleValue( "-N", nTest ); // set strTest to a string-value, if given at the command line CL.GetSingleValue( "-S", strTest ); CL.Done(); // checks if every option given was handled. cout << "an integer:" << nTest << endl << "a string: " << strTest << endl; }
Assume the program above is named cmdltest. When calling cmdltest -N 17 -S "a String" then nTest is set to 17 and strTest to "a String".
The program's output will be:
an integer: 17 a string: a String
After each call of GetSingleValue the given command line option is marked handled. Thus, only the first call of GetSingleValue (with a certain command line option as an argument) has an effect. Subsequent calls (with the same command line option) are ignored.
A sample with with error handling is found here.
See also the CmdLine's member function call, which is explained in the next section.