C getopt 多个值 [英] C getopt multiple value
问题描述
我的论点是这样的
./a.out -i file1 file2 file3
我如何利用 getopt()
获取 3 个(或更多)输入文件?我正在做这样的事情:
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;
...
}
我只得到file1
;如何获取file2
、file3
?
I get just the file1
; how to get file2
, file3
?
推荐答案
如果你必须,你可以从 argv[optind]
开始,然后自己增加 optind
.但是,我建议不要这样做,因为我认为这种语法形式很差.(你怎么知道你什么时候到达了列表的末尾?如果有人有一个以 -
作为第一个字符命名的文件怎么办?)
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
这篇关于C getopt 多个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!