Specify long command line arguments without the sh

2020-04-08 13:26发布

问题:

Let's say I provide the following long option arguments structure:

static const struct option long_opts[] = {
    { "version",   no_argument,       NULL, 'v' },
    { "help",      no_argument,       NULL, 'h' },
    { NULL, 0, NULL, 0 } 
};

How can I specify an additional option, named '--myoption', but without the short form? So I would be able to call only:

./binary --myoption

I need this because I ran out of letters.

回答1:

If you don't put that option into shortopts then no short option for that parameter will be used. E.g.:

#define MYOPT 1000

static struct option long_options[] = {
    {"myopt", no_argument, 0, MYOPT },
}

[...]

c = getopt_long(argc, argv, "", long_options, &option_index);
switch (c) {
case MYOPT:
    /* Do stuff. */
    break;
}


标签: c getopt