Specify long command line arguments without the short format getopt

半世苍凉 提交于 2020-02-15 08:01: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.


回答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;
}


来源:https://stackoverflow.com/questions/22464891/specify-long-command-line-arguments-without-the-short-format-getopt

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!