I have used getopt() quite a few times, and understand the basics. I decided to look into the getopt_long() function, but there's something seriously wrong with my code. Every time the following application runs, the default option is chosen. Why isn't the case 0 chosen if it's run with --help?
Code:
#include <stdio.h>
#include <getopt.h>
extern char *optarg;
extern int optind;
int main(int argc, char *argv[]) {
int c;
int index = 0;
static struct option opts[] = {
{"help", 0, 0, 0},
{0, 0, 0, 0}
};
while((c = getopt_long(argc, argv, "h", opts, &index) != -1)) {
switch(c) {
/*
* if getopt_long returned 0 a long option was chosen
*/
case 0:
printf("option %s\n", opts[index].name);
break;
case 'h':
printf("option h\n");
break;
default:
printf ("?? getopt returned character code %i\n", c);
break;
}
}
return 0;
}