How about that in case you do not want to use libraries:
char* myInputFile = NULL ;
char* myOutputFile = NULL ;
bool option_a = false ;
int argi = 1 ;
while ( argi < argc )
{
const char* args = argv[ argi++ ] ;
switch ( *args )
{
case '-' : /* option/switch */
if ( strcmp( args, "-a" ) == 0 )
option_a = true ;
else
{
std::cerr << "Unknow option " << args << "\n" ;
exit( 1 ) ;
}
break ;
default :
if ( myInputFile == NULL )
myInputFile = args ;
else if ( myOutputFile == NULL )
myOutputFile = args ;
else
{
std::cerr << "Unexpected argument " << args << "\n" ;
exit( 1 );
}
break ;
}
}
At the end, when you process all your arguments you can do some final checking like, for example, testing if the input file and output file were set:
if ( myInputFile == NULL )
{
std::cerr << "Input file missing!\n" ;
exit( 1 ) ;
}
...
You could also put all your arguments into a structure instead of having them separately:
struct options_t
{
char* myInputFile ;
char* myOutputFile ;
bool option_a ;
options_t() : myInputFile(), myOutputFile(), option_a() {}
} ;
This way you can pass all arguments down to your functions instead of listing them one by one.
Just a thought...