C getopt 多个值
我的论点是这样的:
./a.out -i file1 file2 file3
我怎样才能利用getopt()
来获取3个(或更多)输入文件? 我正在做这样的事情:
while ((opt = getopt(argc, argv, "i:xyz.."))!= -1){
case 'i':
input = optarg;
break;
...
}
我只得到 file1
;如何获取file2
、file3
?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我知道这已经很老了,但我在寻找解决方案时遇到了这个。
我发现 int optind (getopt() 使用的 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.
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.
如果必须,您可以从
argv[optind]
开始并自行增加optind
。但是,我建议不要这样做,因为我认为这种语法形式很差。 (你怎么知道你什么时候到达了列表的末尾?如果有人有一个以-
作为第一个字符命名的文件怎么办?)我认为最好还是改变你的语法:
或者将文件列表视为位置参数:
If you must, you could start at
argv[optind]
and incrementoptind
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:
Or to treat the list of files as positional parameters:
请注意,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 dropgetopt
and write your own command line parser if you want to use this syntax, since it's not a syntax supported bygetopt
.我查看并尝试了上面的代码,但我发现我的解决方案更简单,并且对我来说效果更好:
我想要的处理是:(
需要 2 个参数)。
这对我来说是这样的:
这像往常一样从我那里获取了第一个值,然后只寻找第二个所需的值。
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:
(2 arguments required).
Here's how it panned out for me:
This grabbed the first value form me as normal and then just looked for the second, REQUIRED value.
事实证明,GoTTimw 的解决方案对我来说非常有用。然而,我想提一下这里尚未提出的另一个想法。
以这种方式将参数作为一个字符串传递。
然后你得到一个字符串作为一个参数,你只需要用空格分割它。
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.
Then you get one string as a single argument and you only need to split it by space.