Reputation: 4523
I am using getopt and getopt_long to parse the arguments of a c++ program. When the arguments are given correctly, I dont have a problem. Also, when wrong short arguments are given, the error message is printed correctly. But when a wrong long argument is given, I am not getting its error message.
Here is the code:
#include <stdio.h>
#include <ctype.h>
#include <getopt.h>
static struct option long_options[] = {
{"aa", no_argument, 0, 'a' },
{"bb", no_argument, 0, 'b' },
{0, 0, 0, 0 }
};
int main (int argc, char **argv)
{
int c;
int option_index;
opterr = 0;
while ((c = getopt_long (argc, argv, "ab",
long_options, &option_index)) != -1)
switch (c) {
case 0:
printf ("option %s", long_options[option_index].name);
if (optarg)
printf (" with arg %s", optarg);
printf ("\n");
break;
case 'a':
printf("got option a\n");
break;
case 'b':
printf("got option b\n");
break;
case '?':
if (isprint (optopt))
printf ("Unknown option `-%c'.\n", optopt);
else
printf ("Unknown option character `\\x%x'.\n", optopt);
return 1;
default:
printf("?? getopt_long returned character code 0%o ??\n", c);
return 1;
}
if (optind < argc) {
printf("non-option ARGV-elements: ");
while (optind < argc)
printf("%s ", argv[optind++]);
printf("\n");
}
return 0;
}
Here are the runs:
$ ./a.exe -ab --aa --bb # works correctly
got option a
got option b
got option a
got option b
$ ./a.exe -z # prints error message correctly
Unknown option `-z'.
$ ./a.exe --zz # not getting the error message for "--zz"
Unknown option character `\x0'.
How to print error message that --zz is an unknown option?
Upvotes: 1
Views: 1173
Reputation: 48615
In this case I have used the fact that optind
has just been incremented after pointing to the failed option to discover the option from the original argv
.
So I would do something like this:
case '?':
std::cerr << "Unknown option " << argv[optind - 1] << ".\n";
return EXIT_FAILURE;
There is no need to differentiate between long and short options this way.
Upvotes: 0