I have a program (myprogram) that takes a file for input and creates another file for output. The syntax is:
myprogram inputfile outputfile
It looks something like this:
int main ( int argc, char *argv[] )
{
...
if ( argc != 3 ) {
cout<<"wrong number of arguments"<< endl; return 1;}
}
myInputFile = argv[1];
myOutputFile = argv[2];
...
}
So here I have argv[1] being the inputfile.I would now like to add some options to the syntax. So, for example, I would like myprogram -a inputfile outputfile as an option. The number of options will vary.
That means that the filenames will depend on the number of arguments.
What is the smartest way to do this in the code? I can see that one would just take the last two arguments as the filenames and then assume that the arguments before that are the options (so something like myInputFile = argv[argc - 1]. This doesn't quite feel right because I end up with a lot of if ... then statements.
So my question is: How does one best deal with a variable number of arguments?