I am writing a simple program to have -d with a parameter along with the other arguments
I want them run with aa.cc with the following options
aa -d 123 param1 param2oraa param1 param2 -d 123
I have written the code in the following way
/* sample */
char *level:
while ((ch = getopt(argc, argv, "d:")) != EOF) {
switch(ch) {
case 'd':
level = optarg;
debug = TRUE;
break;
default:
usage();
/*NOTREACHED*/
}
}
It is working fine when given as
aa -d 123 param1 param2 but not
when given as
aa param1 param2 -d 123.
Can anyone please suggest how we can achieve that?
getopt()is documented to return-1rather thanEOFafter the last option is processed. The POSIX standard header forgetopt()is<unistd.h>and that does not defineEOF, so the definition ofgetopt()was changed (quite a while ago now) to decouple it from the<stdio.h>header.