I am trying to learn to use getopt_long
. From wikipedia, I see the code
#include <stdio.h> /* for printf */
#include <stdlib.h> /* for exit */
#include <getopt.h> /* for getopt_long; POSIX standard getopt is in unistd.h */
int main (int argc, char **argv) {
int c;
int digit_optind = 0;
int aopt = 0, bopt = 0;
char *copt = 0, *dopt = 0;
static struct option long_options[] = {
{"add", 1, 0, 0},
{"append", 0, 0, 0},
{"delete", 1, 0, 0},
{"verbose", 0, 0, 0},
{"create", 1, 0, 'c'},
{"file", 1, 0, 0},
{NULL, 0, NULL, 0}
};
int option_index = 0;
while ((c = getopt_long(argc, argv, "abc:d:012",
long_options, &option_index)) != -1) {
int this_option_optind = optind ? optind : 1;
switch (c) {
case 0:
printf ("option %s", long_options[option_index].name);
if (optarg)
printf (" with arg %s", optarg);
printf ("\n");
break;
case '0':
case '1':
case '2':
if (digit_optind != 0 && digit_optind != this_option_optind)
printf ("digits occur in two different argv-elements.\n");
digit_optind = this_option_optind;
printf ("option %c\n", c);
break;
case 'a':
printf ("option a\n");
aopt = 1;
break;
case 'b':
printf ("option b\n");
bopt = 1;
break;
case 'c':
printf ("option c with value '%s'\n", optarg);
copt = optarg;
break;
case 'd':
printf ("option d with value '%s'\n", optarg);
dopt = optarg;
break;
case '?':
break;
default:
printf ("?? getopt returned character code 0%o ??\n", c);
}
}
if (optind < argc) {
printf ("non-option ARGV-elements: ");
while (optind < argc)
printf ("%s ", argv[optind++]);
printf ("\n");
}
exit (0);
}
I don't quite understand the option long_options[]
object.
First column
I think the first "column" of long_options[]
should be the long flag (whatever follows --
) the user have used in the command line.
Second column
I would have thought the second column should contain only no_argument
, required_argument
, or optional_argument
but instead of that I see 0 and 1.
Third column
I don't understand the third column.
Fourth column and maximum number of flags
The fourth column is the unique identifier one uses in the switch statement. However, if the unique identifier can only be a single character, then are we limited to all lower case letters (26) + all uppercase letters (26) + the numbers (10) + eventually some special characters for a total of a bit more than 62 different arguments max? Is this a limitation of getopt
? If I am mistaken, then how can one indicate more than two characters to identify a flag in the third argument to getopt_long
(""abc:d:012""
)
I suppose the last row of option long_options[]
is for when getopt
returns -1
and therefore does not really matter as long as it exists.
C
. That was a mistake, I meantC++
. (tag edited). Thanks – Cochranegetopt
works in pureC
and I'll consider later best practices inC++
. I edited the tag back toC
. Thank you – CochraneC
, becauseC++
isn't really being used here.. as you are usingprintf
and so on.. I assumed you mistagged and put it asC++
. – Arsenicgetopt_long()
is causing problems? – Protuberantstruct option
. Refer to the real documentation ofgetopt_long()
, available in many places, apparently not including Wikipedia. Here, for instance, or here, not to mention probably on your own machine via theman
command. – Manufacture