I'm using getopt
to parse the parameters from the command line, and I have problems to make it recognize the optional parameter order.
I want to achieve these cases:
$ ./program -i file.html -o output.html #ex 1
$ ./program -i -o file.html output.html #ex 2
$ ./program -o output.html -i file.html #ex 3
my code looks like this
while((c = getopt(argc, argv, "hi:o:")) != -1) {
switch(c) {
case 'h':
//prints the help file
break;
case 'i':
ivalue = optarg;
break;
case 'f':
fvalue = optarg;
break;
case '?':
//prints an error
break;
default:
abort();
}
}
to debug better this I've also wrote outside the while
for(int i = optind; i < argc; i++) {
printf("non optional argument %s\n", argv[i]);
return 0;
}
so the examples 1 and 3 are correctly working, while the example 2 is not getting the parameters straight. At first I thought that it was simply not possible with this function, but then in this example I saw it was.
There's also a bonus question: how come calling the program with no parameters doesn't abort()
?
I'm using ubuntu 15.10 and gcc 5.2.1 installed with apt-get (dunno if useful but better safe that sorry).
in this example I saw it was [possible].
No, you didn't. Your option -i
requires an argument, but in your non-working case you try to insert a different option between the -i
and its argument. That is not allowed -- if an option takes an argument, whether optional or required, that argument (if provided) must immediately follow the option letter. The example you linked does not show differently.
What you are trying to do not only is not supported by getopt()
, it does not follow standard Unix convention. Moreover, although it could conceivably work the way you imagine for options with required arguments, it is wholly unworkable for options with optional arguments. When arguments are optional, there is no way to correctly match them with their options if they are not required to follow directly.
There's also a bonus question: how come calling the program with no parameters doesn't abort()?
Why would it? If no options are specified to the program on its command line then getopt()
returns -1 on the first call, and the loop body is never executed. To get an abort()
from your code, you would need to specify an option that is in the option string, but for which you do not provide a specific case, or else an option that requires an argument, but without providing the argument (in which event getopt()
returns ':'
, for which you do not provide a specific case). As the code is presently written, that would need to be -o
, either with or without an argument, or -i
without an argument.