Specify long command line arguments without the sh

2020-04-08 13:08发布

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.

标签: c getopt
1条回答
小情绪 Triste *
2楼-- · 2020-04-08 14:11

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;
}
查看更多
登录 后发表回答