C语言 C getopt 多个值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3939157/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 06:42:29  来源:igfitidea点击:

C getopt multiple value

cinputgetopt

提问by w00d

My argument is like this

我的论点是这样的

./a.out -i file1 file2 file3

How can I utilize getopt()to get 3 (or more) input files? I'm doing something like this:

如何利用getopt()获取 3 个(或更多)输入文件?我正在做这样的事情:

while ((opt = getopt(argc, argv, "i:xyz.."))!= -1){
  case 'i':
     input = optarg; 
     break;
  ...
}

I get just the file1; how to get file2, file3?

我只得到file1; 怎么获得file2file3

采纳答案by jamesdlin

If you must, you could start at argv[optind]and increment optindyourself. However, I would recommend against this since I consider that syntax to be poor form. (How would you know when you've reached the end of the list? What if someone has a file named with a -as the first character?)

如果你必须,你可以开始argv[optind]并增加optind自己。但是,我建议不要这样做,因为我认为这种语法形式很差。(您怎么知道何时到达列表的末尾?如果有人将文件命名为 a-作为第一个字符怎么办?)

I think that it would be better yet to change your syntax to either:

我认为最好将您的语法更改为:

/a.out -i file1 -i file2 -i file3

Or to treat the list of files as positional parameters:

或者将文件列表视为位置参数:

/a.out file1 file2 file3

回答by GoTTimw

I know this is quite old but I came across this in my search for a solution.

我知道这已经很老了,但我在寻找解决方案时遇到了这个问题。

while((command = getopt(argc, argv, "a:")) != -1){

    switch(command){
        case 'a':

        (...)

        optind--;
        for( ;optind < argc && *argv[optind] != '-'; optind++){
              DoSomething( argv[optind] );         
        }

        break;
    }

I found that int optind(extern used by getopt()) points to next position after the 'current argv' selected by getopt(); That's why I decrease it at the beginning.

我发现int optindgetopt()使用的 extern )指向getopt()选择的“当前argv”之后的下一个位置;这就是为什么我在开始时减少它。

First of all for loopchecks if the value of current argument is within boundaries of argv(argcis the length of array so last position in array argv is argc-1). Second part of &&compares if the next argument's first char is '-'. If the first char is '-' then we run out of next values for current argument else argv[optind] is our next value. And so on until the argv is over or argument runs out of values.

首先for 循环检查当前参数的值是否在argv 的边界内(argc是数组的长度,因此数组 argv 中的最后一个位置是argc-1)。&& 的第二部分比较下一个参数的第一个字符是否为“-”。如果第一个字符是“-”,那么我们用完当前参数的下一个值,否则 argv[optind] 是我们的下一个值。依此类推,直到 argv 结束或参数用完值。

At the end increment optindto check for the next argv.

最后增加optind以检查下一个 argv。

Note that because we are checking 'optind < argc' first second part of condition will not be executed unless first part is true so no worries of reading outside of array boundaries.

请注意,因为我们正在检查 ' optind < argc' 条件的第一第二部分将不会执行,除非第一部分为真,因此不必担心读取数组边界之外的内容。

PS I am a quite new C programmer if someone has an improvements or critique please share it.

PS 我是一个相当新的 C 程序员,如果有人有改进或批评,请分享。

回答by R.. GitHub STOP HELPING ICE

Note that glibc's nonconformant argument permutation extension will break any attempt to use multiple arguments to -iin this manner. And on non-GNU systems, the "second argument to -i" will be interpreted as the first non-option argument, halting any further option parsing. With these issues in mind, I would drop getoptand write your own command line parser if you want to use this syntax, since it's not a syntax supported by getopt.

请注意,glibc 的不一致参数置换扩展将破坏以-i这种方式使用多个参数的任何尝试。在非 GNU 系统上,“的第二个参数-i”将被解释为第一个非选项参数,停止任何进一步的选项解析。考虑到这些问题,getopt如果您想使用这种语法,我会删除并编写您自己的命令行解析器,因为它不是getopt.

回答by Andrew Ward

I looked and tried the code above, but I found my solution a little easier and worked better for me:

我查看并尝试了上面的代码,但我发现我的解决方案更容易一些并且更适合我:

The handling I wanted was:

我想要的处理是:

-m mux_i2c_group mux_i2c_out

(2 arguments required).

(需要 2 个参数)。

Here's how it panned out for me:

以下是它对我的影响:

case 'm':
    mux_i2c_group = strtol(optarg, &ch_p, 0);

    if (optind < argc && *argv[optind] != '-'){
        mux_i2c_out = strtol(argv[optind], NULL, 0);
        optind++;
    } else {
        fprintf(stderr, "\n-m option require TWO arguments <mux_group> "
                        "<mux_out>\n\n");
        usage();
    }

    use_mux_flag = 1;
    break;

This grabbed the first value form me as normal and then just looked for the second, REQUIRED value.

这会像往常一样从我那里获取第一个值,然后只查找第二个所需的值。

回答by Dmytro Oliinychenko

The solution by GoTTimw has proven very useful to me. However, I would like to mention one more idea, that has not been suggested here yet.

事实证明,GoTTimw 的解决方案对我非常有用。但是,我想再提一个想法,这里还没有提出。

Pass arguments as one string in this way.

以这种方式将参数作为一个字符串传递。

./a.out -i "file1 file2 file3"

Then you get one string as a single argument and you only need to split it by space.

然后你得到一个字符串作为单个参数,你只需要按空格分割它。