diff --git a/libutil/util_opt.c b/libutil/util_opt.c index bdc028e1..c6daa14e 100644 --- a/libutil/util_opt.c +++ b/libutil/util_opt.c @@ -49,6 +49,7 @@ struct util_opt_l *util_opt_l = &l; #define MAX_OPTLEN 256 static int opt_max_len(void); +static bool opt_is_active(struct util_opt *opt); /** * Initialize the command line options @@ -68,7 +69,9 @@ void util_opt_init(struct util_opt *opt_vec, const char *opt_prefix) opterr = 0; /* Get number of options */ - for (count = 0; opt_vec[count].desc != NULL; count++); + for (i = 0, count = 0; opt_vec[i].desc != NULL; i++) + if (opt_is_active(&opt_vec[i])) + count++; /* * Allocate short option string for worst case when all options have * optional parameters e.g "x::" and long option string. @@ -85,7 +88,9 @@ void util_opt_init(struct util_opt *opt_vec, const char *opt_prefix) /* Force getopt_long() to return ':' for missing required arguments */ *str++ = ':'; /* Construction of input structures for getopt_long() function. */ - for (i = 0, j = 0; i < count; i++) { + for (i = 0, j = 0; opt_vec[i].desc != NULL; i++) { + if (!opt_is_active(&opt_vec[i])) + continue; if (opt_vec[i].flags & UTIL_OPT_FLAG_SECTION) continue; if (!(opt_vec[i].flags & UTIL_OPT_FLAG_NOLONG)) { diff --git a/libutil/util_opt_command_example.c b/libutil/util_opt_command_example.c index a8fbf70b..a87ed5a0 100644 --- a/libutil/util_opt_command_example.c +++ b/libutil/util_opt_command_example.c @@ -42,6 +42,12 @@ static struct util_opt opt_vec[] = { .desc = "Option with a required argument REQ_ARG", .command = COMMAND_PULL, }, + { + .option = { "test", required_argument, NULL, 't'}, + .argument = "TEST", + .desc = "Option 'test' with a required argument TEST for pull", + .command = COMMAND_PULL, + }, { .desc = "OPTIONS", .flags = UTIL_OPT_FLAG_SECTION, @@ -57,7 +63,12 @@ static struct util_opt opt_vec[] = { .option = { NULL, no_argument, NULL, 'l'}, .desc = "Option with only a short name", .flags = UTIL_OPT_FLAG_NOLONG, - .command = "push", + .command = COMMAND_PUSH, + }, + { + .option = { "test", no_argument, NULL, 't'}, + .desc = "Option 'test' without an argument for push", + .command = COMMAND_PUSH, }, UTIL_OPT_SECTION("COMMON OPTIONS"), /* Standard option: -h,--help */ @@ -122,9 +133,6 @@ int main(int argc, char *argv[]) char **my_argv = argv; char *command = NULL; - /* Install option vector */ - util_opt_init(opt_vec, NULL); - /* The command name is the very first argument */ if (argc >= 2 && strncmp(argv[1], "-", 1) != 0) { command = argv[1]; @@ -144,6 +152,9 @@ int main(int argc, char *argv[]) util_opt_set_command(command); util_prg_set_command(command); + /* Install option vector */ + util_opt_init(opt_vec, NULL); + /* Parse all options specified in my_argv[] */ while (1) { /* Get the next option 'c' from my_argv[] */ @@ -173,6 +184,9 @@ int main(int argc, char *argv[]) case 'l': printf("Specified: -l\n"); break; + case 't': + printf("Specified: --test %s\n", optarg ? optarg : ""); + break; default: util_opt_print_parse_error(c, my_argv); return EXIT_FAILURE;