RajSanpui
RajSanpui

Reputation: 12064

getopt: unable to recognize the missing argument ' : ' and invalid ones ' ? '

I have written a simple code using getopt for understanding perspective.

#include <stdio.h>
#include <unistd.h>

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

 /* Here since c is followed with colon, so 'c' takes an argument */
  const char *optstring = "abc:d";

  int ret;

  while((ret=getopt(argc, argv, optstring))!= -1)
  {
    switch(ret)
    {
      case 'a':
        printf("Option found: a, optind = %d\n",optind);
        break;

      case 'b':
         printf("Option found: b, optind = %d\n",optind);
         break;

      case 'c':
         printf("Option found: c, optind = %d\n",optind);
         printf("Option argument: %s\n",optarg);
         break;

      case 'd':
         printf("Option found: d, optind = %d\n",optind);
         break;

      case ':':
         printf("The option takes an argument which is missing");
         break;

     //case '?':
       //   printf("Didn't you enter an invalid option?");
           // break;
     }
   }
 }

The problem is:

(1) Case 1: If the case '?' is commented then:

[root@dhcppc0 getopt]# ./a.out -a -b -c
    Option found: a, optind = 2
    Option found: b, optind = 3
    ./a.out: option requires an argument -- c

So, as you can see, the case ':' did not take into effect, as normally we expect a missing argument to return a ':' (colon) by getopt.

(2) Case 2: AND, if i un-comment it, and then run the program, it hits the case '? even for missing argument.

enter code here
[root@dhcppc0 getopt]# ./a.out -a -b -c
   Option found: a, optind = 2
   Option found: b, optind = 3
      ./a.out: option requires an argument -- c
      Didn't you enter an invalid option?

What is the point i am missing here?

ADDED LATER:

Also why is the ./a.out: option requires an argument -- c the default error coming? How to handle it, since i am already taking care for it in the case ':', and don't want the default error message?

ADDED AGAIN: As suggested in the answer, i used the colon in beginning of optstring - const char *optstring = ":abc:d", then why is this happening?

./a.out -a -b -c -d returns -d as the argument to c?? -d is a separate optional character and not any argument

Upvotes: 3

Views: 9532

Answers (2)

Jonathan Leffler
Jonathan Leffler

Reputation: 753675

The POSIX version of the getopt() function specifies:

If getopt() encounters an option character that is not contained in optstring, it shall return the <question-mark> ( '?' ) character. If it detects a missing option-argument, it shall return the <colon> character ( ':' ) if the first character of optstring was a <colon>, or a <question-mark> character ( '?' ) otherwise.

Since your optstr does not start with a :, it should return a ? instead.

Upvotes: 4

selbie
selbie

Reputation: 104514

From man getopt:

If getopt() finds an option character in argv that was not included in optstring, or if it detects a missing option argument, it returns '?' and sets the external variable optopt to the actual option character.

So your program behavior is by design as expected. You may be confusing the expected return value of getopt with this statement in the man page:

If the first character (following any optional '+' or '-' described above) of optstring is a colon (':'), then getopt() returns ':' instead of '?' to indicate a missing option argument. If an error was detected, and the first character of optstring is not a colon, and the external variable opterr is nonzero (which is the default), getopt() prints an error message.

So try declaring your optstring as follows:

const char *optstring = ":abc:d";

Upvotes: 4

Related Questions