1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
|
/**** library.h ****/
class cmd_parser
{
public:
cmd_parser( int argc, char**argv, bool auto_init = true );
virtual int parseOthers( int curr, int argc, char **argv );
private:
int _argc;
char **_argv;
}
/**** library.cpp ****/
cmd_parser::cmd_parser( int argc, char**argv )
{
_argc = argc;
_argv = argv;
if ( auto_init )
{
for ( int i = 0; i < argc; ++i )
{
switch (argv[i][0])
{
case 'a':
//do something
break;
case 'b':
//do other
break;
defaults:
i = parseOthers( i, argc, argv );
break;
}
}
}
}
int cmd_parser::parseOthers ( int curr, int argc, char **argv )
{
printf( "Unrecognized switch: %s", argv[curr] );
}
/************************** END OF LIBRARY ****************/
/* Project code */
/**** derived.h ****/
#include "library.h"
class derived_cmd_parser : public cmd_parser
{
derived_cmd_parser( int argc, char **argv );
int parseOthers ( int curr, int argc, char**argv );
}
/**** derived.cpp ****/
derived_cmd_parser::derived_cmd_parser( int argc, char **argv ) : cmd_parser(argc,argv)
{
// do specific init
}
int derived_cmd_parser::parseOthers ( int curr, int argc, char **argv )
{
switch( argv[curr][0] )
{
case 'f':
if ( argc > curr+1 )
{
// do something with argv[curr] & argv[curr+1]
curr++;
}
break;
}
return curr;
}
/**** main.cpp ****/
#include "derived.h"
int main ( int argc, char **argv )
{
derived_cmd_parser *cmdline;
cmdline = new derived_cmd_parser( argc, argv );
// Here I'd like to have the "f" cmdline argument been parsed by the dervived_cmd_parser.
// but by this code it generates to me 2 "Unrecognized ..." from the cmd_parser method
delete cmdline;
return 0;
}
|