getopt - is it possible to use getopt_long to parse arrays of strings similar to command line arguments in a C program? -
i aware getopt should used parse command line arguments, , not strings.
however, confused fact if pass array of strings "looks like" argv variable, getopt_long seems work, only first time call it. second, , subsequent times, call it ignores arguments , returns default ones.
the reason doing turning application used take command line arguments in "interactive" version of asks user arguments, actions based on arguments, asks more arguments , on.
the following code minimal example reproduces error
#include <stdio.h> #include <stdlib.h> #include <getopt.h> struct input_parameters{ char * d; // data set }; int solver_help(int argc, char* const argv[], struct input_parameters * p) { int c; p->d = "default name"; while (1) { static struct option long_options[] = { {"data", required_argument, 0, 'd'}, {0, 0, 0, 0} }; /* getopt_long stores option index here. */ int option_index = 0; c = getopt_long (argc, argv, "d:", long_options, &option_index); /* detect end of options. */ if (c == -1) break; switch (c) { case 'd': p->d = optarg; break; default: printf("wrong option specification\n"); exit(-1); } } return 0 ; } int main () { int argc_first = 3; char * const argv_first[] = { "getopt_test", "--data", "first" }; struct input_parameters first_input; solver_help(argc_first, argv_first, &first_input); printf("i think data is: %s\n", first_input.d); int argc_second = 3; char * const argv_second[] = { "getopt_test","--data", "second" }; struct input_parameters second_input; solver_help(argc_second, argv_second, &second_input); printf("i think data is: %s\n", second_input.d); return 0; }
the output of code is
i think data is: first think data is: default name
from manual page getopt
:
in order use
getopt()
evaluate multiple sets of arguments, or evaluate single set of arguments multiple times, variableoptreset
must set1
before second , each additional set of callsgetopt()
, , variableoptind
must reinitialized.
modifying code gives desired results:
#include <stdio.h> #include <stdlib.h> #include <getopt.h> struct input_parameters { char * d; // data set }; int solver_help(int argc, char* const argv[], struct input_parameters * p) { p->d = "default name"; static struct option long_options[] = { {"data", required_argument, 0, 'd'}, {0, 0, 0, 0} }; int option_index = 0; optreset = 1; /* add */ optind = 1; /* add */ int c = getopt_long(argc, argv, "d:", long_options, &option_index); switch ( c ) { case -1: break; case 'd': p->d = optarg; break; default: printf("in default case...\n"); printf("wrong option specification\n"); exit(exit_failure); } return 0; } int main(void) { int argc_first = 3; char * const argv_first[] = { "getopt_test", "--data", "first" }; struct input_parameters first_input; solver_help(argc_first, argv_first, &first_input); printf("i think data is: %s\n", first_input.d); int argc_second = 3; char * const argv_second[] = { "getopt_test","--data", "second" }; struct input_parameters second_input; solver_help(argc_second, argv_second, &second_input); printf("i think data is: %s\n", second_input.d); return 0; }
with output:
paul@horus:~/src/sandbox$ ./go think data is: first think data is: second paul@horus:~/src/sandbox$
Comments
Post a Comment