The complete source code of the example above is given below:
#include <iostream> #include <string> #include "cmdline.h" using namespace std; using namespace cmdl; // defined in cmdline.h void PrintHelp() { cout << "PrintHelp() called." << endl; } void SetInt(const int& i) { cout << "SetInt called with argument " << i << "." << endl; } void AddStringToList(const string& s) { cout << "AddStringToList called with argument " << s << "." << endl; } int main(int argc, char *argv[]) { CmdLine CL; CL.Init( --argc, ++argv ); // skip program name CL.Call( "-h", PrintHelp ); CL.Call( "-N", SetInt ); CL.Call( "-S", AddStringToList, CmdLine::cmdMultipleArgs ); CL.Done(); }
Assume the program above is named cmdline, then calling
cmdline -S "first" "second" "third" -N 17 -S "fourth" -h
will lead to the following output:
SetInt called with argument 17 AddStringToList called with argument first. AddStringToList called with argument second. AddStringToList called with argument third. AddStringToList called with argument fourth. PrintHelp() called.
After each call of Call (and of GetSingleValue) the given command line option is marked handled. Thus, only the first call of GetSingleValue or Call (for a certain command line option) has an effect. Subsequent calls are ignored.
An example including error handling is found here.