简体   繁体   中英

Why does my program output an argument twice in usage ([-d?] and [-d|--data])? (i'm using popt library for options parsing)

The program usage outputs -d option twice, though I provide it only once in struct poptOption . This is the usage output:

Usage: generate-test [-d?] [-d|--data] [-n|--test-name=STRING] [-?|--help]
        [--usage]

The --help output is correct:

Usage: generate-test [OPTION...]
  -d, --data                 Provide this option if the test needs external data from file(s)
  -n, --test-name=STRING     Provide a test name, this option is mandatory

Help options:
  -?, --help                 Show this help message
      --usage                Display brief usage message

This is my code:

char *test_name = NULL;
int use_external_data = 0;

struct poptOption popt_options[] = {
    {"data", 'd', POPT_ARG_NONE, NULL, 2, "Provide this option if the test needs an external data from file(s)", NULL },
    {"test-name", 'n', POPT_ARG_STRING, &test_name, 1, "Provide a test name, this option is mandatory", NULL },

    POPT_AUTOHELP { NULL, '\0', 0, NULL, 0, NULL, NULL }
};

poptContext popt_context;
popt_context = poptGetContext("Test generator", argc, argv, popt_options, 0);

int opt_ret = 0;
while ((opt_ret = poptGetNextOpt(popt_context)) > 0)
    switch (opt_ret)
    {
        case 2:
            use_external_data = 1;
            break;
    }

What do I not understand about the POpt library? Thanks!

I do agree this may look confusing. What popt is showing with [-d?] [-d|--data] is that the -d short option can be a standalone option, or it can be aggregated to other short options like -? (contrary to -n ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM