To use the command line parser utility, you have only to create an instance of the CmdLineParser class and initialize it with the argv and argc parameters passed to the main() function, plus two strings containing all parameter and non-parameter switches, respectively. Then call the
get_next( std::string & parameter ) |
routine in a loop until the parsing is finished. get_next returns:
a letter specifying a non-parameter switch (parameter is will be empty).
a letter specifying a parameter switch (parameter will obtain a parameter string).
a - character - this indicates that the switch section has already been terminated; parameter obtains an argument string.
a \0 character (ASCII 0) - stands for the end of parsing; no more switches or parameters available.
int main( char ** argv, int argc ) { try { CmdLineParser cmd_line( argc, argv, "vs", "f" ); /* This initialization line sets up a command line parser * instance and makes it consider 'v' and 's' a * non-parameter switches, whereas 'f' a parameter one. */ bool parse = true; while( parse ) { std::string parameter; const char option = cmd_line.get_next( parameter ); switch( option ) { case '-': /* Switches section has already been terminated.*/ process_parameters( parameter ); break; case '\0': /* End of parsing. */ parse = false; break; case 'v': process_switch_v(); break; case 's': process_switch_s(); break; case 'f': /* 'f' is a parameter switch - therefore * 'parameter' variable must now contain the * parameter string. */ process_switch_f( parameter ); break; default: assert( 0 ); /* Invalid switch not caught by exception. */ } // switch } // while } catch( ExMissingSwitch & ) { /* There was only '-' on the command line, but the * character switch didn't follow. */ ... } catch( ExUnexpectedCommandLineSwitch & ) { /* There was a character switch that has been defined * neither in the parameter nor in the non-parameter * switches string. */ ... } catch( ExMissingCommandLineArgument & ) { /* There was a parameter switch on the command line, but * it was not followed by its argument. */ } ... //Continue processing of the 'main' routine. return 0; } // main |