void display(char * str){
printf("%s: Missing file\n", str);
}
int main(int argc, char **argv)
{
int longIndex, opt = 0;
const char *optString = "h?";
static const struct option longOpts[] = {
{ "help", no_argument, NULL, 'h' },
{ NULL, no_argument, NULL, 0 }
};
opt = getopt_long( argc, argv, optString, longOpts, &longIndex );
while( opt != -1 ) {
switch( opt ) {
case 'h':
case '?':
display(argv[0]);
break;
default:
break;
}
opt = getopt_long( argc, argv, optString, longOpts, &longIndex );
}
return 0;
}
This code compiles fine but when I run it like:
./a.out ?
it does not call display. What am I missing?
The question mark ‘?’ is returned by getopt when it finds an argument that’s not in the optstring or if it detects a missing option argument, so you shouldn’t use ‘?’ in optstring because it’s sort of reserved for that, instead you should use the more conventional ‘h’ for help.
See the man page
Edit:
This is an example: