Greg
Greg

Reputation: 737

simple getopts c problems

So I have a pretty simple program but for some reason I can't get the options right.

For some reason I just can't get these results. I know its a simple fix I just can't seem to find the answer.

Right now this is what I have.

int main(int argc, char* argv[]){
   int c;

   while(( c = getopt( argc, argv, "h")) != -1){
      switch( c ){
      case 'h':
         usage(); return EXIT_SUCCESS;

      case '*':
         usage(); return EXIT_FAILURE;

      default:
         break;
      }
   }
   mainProgram();
   return EXIT_SUCCESS;
}

Upvotes: 0

Views: 135

Answers (1)

larsks
larsks

Reputation: 311516

If you read the getopt(3) man page:

If getopt() does not recognize an option character, it prints an error message to stderr, stores the character in optopt, and returns '?'. The calling program may prevent the error message by setting opterr to 0.

So, getopt() will return ? if someone passes in an unrecognized option. You're looking for *, which you will never receive. In C, * does not act as a wildcard so this does not mean "any character".

Using default here isn't the correct solution (although it will work), because this would also trigger on valid options for which you had not implemented a handler.

Upvotes: 1

Related Questions