C getopt 多个值

发布于 2024-09-28 05:49:02 字数 342 浏览 4 评论 0原文

我的论点是这样的:

./a.out -i file1 file2 file3

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

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

我只得到 file1;如何获取file2file3

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:

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

何以畏孤独 2024-10-05 05:49:02

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

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

    switch(command){
        case 'a':

        (...)

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

        break;
    }

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

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

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

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

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

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.

First of all for loop checks if the value of current argument is within boundaries of argv (argc is 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.

At the end increment optind to check for the next 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.

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

蝶…霜飞 2024-10-05 05:49:02

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

我认为最好还是改变你的语法:

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

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

/a.out file1 file2 file3

If you must, you could start at argv[optind] and increment optind yourself. 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?)

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
无语# 2024-10-05 05:49:02

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

Note that glibc's nonconformant argument permutation extension will break any attempt to use multiple arguments to -i in 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 getopt and write your own command line parser if you want to use this syntax, since it's not a syntax supported by getopt.

自由范儿 2024-10-05 05:49:02

我查看并尝试了上面的代码,但我发现我的解决方案更简单,并且对我来说效果更好:

我想要的处理是:(

-m mux_i2c_group mux_i2c_out

需要 2 个参数)。

这对我来说是这样的:

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;

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

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).

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.

幸福不弃 2024-10-05 05:49:02

事实证明,GoTTimw 的解决方案对我来说非常有用。然而,我想提一下这里尚未提出的另一个想法。

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

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

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

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.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文