I am using MinGW on Windows 32 bit and am trying to parse some command line arguments. I have an arg for input file (-i). I also have some other args that don't take operands. The problem is that if I pass -i without an argument, and then follow up with another argument (-a, for example), getopt thinks -a is the operand to -i! This of course works as expected if -i is the last argument on the command line.
Example: ./program.exe -i -a
It thinks the filename is "-a" instead of empty.
Here's my simplified code:
Code:
#include <getopt.h>
char *input_filepath = NULL;
int do_something_else = 0;
int op;
while((op = getopt(argc, argv, "ai:")) != -1){
switch(op){
case 'i':
input_filepath = optarg;
break;
case 'a':
do_something_else = 1;
break;
case ':':
fprintf(stderr,"Option -%c requires an operand\n", optopt);
return -1;
break;
case '?':
fprintf(stderr,"Unknown argument: %c\n", optopt);
return -1;
break;
}
}