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
The POSIX version of the
getopt()function specifies:Since your
optstrdoes not start with a:, it should return a?instead.