【发布时间】:2020-08-12 19:29:22
【问题描述】:
我正在尝试将多个参数传递给一个选项。 例如,
(1)./someProgram -f opt1 opt2 opt3 -j opt -d.
(2)./someProgram -f /dir/dir/* -j opt -d.
我的 getopt_long 设置如下所示。
const char *short_opt = "hp:j:o:f:";
struct option long_opt[] =
{
{"help", no_argument, NULL, 'h'},
{"journal", required_argument, NULL, 'j'},
{"partition", required_argument, NULL, 'p'},
{"output", required_argument, NULL, 'o'},
{"file", required_argument, NULL, 'f'},
{NULL, 0, NULL, 0}
};
我尝试简化主要代码,以使其看起来不至于不堪重负。 我留下了与 getopt_long 变量交互的部分。
while ((arg = getopt_long(argc, argv, short_opt, long_opt, NULL)) != -1) {
switch (arg) {
case -1:
case 0:
break;
...
case 'j':
if (optarg) {
setSomeVar; //And nothing else, only set var and break.
}
break;
case 'f':
if (optarg) {
index = optind - 1;
while (index < argc) {
nextOpt = strdup(argv[index]);
index++;
if (nextOpt[0] != '-') {
callFunc(nextOpt);
}
else {
break;
}
}
optind = index - 1;
}
else {
fprintf(stderr, "...\n");
exit(EXIT_FAILURE);
}
break;
case ':':
case '?':
fprintf(stderr, "...");
return (EXIT_FAILURE);
default:
fprintf(stderr, "...", argv[0], arg);
return (EXIT_FAILURE);
};
如果我像下面这样调用我的程序,一切都会按预期进行。
第一个输入
./prog -f /dir/dir/* - (60 files) or ./prog -f file1 file2 file3
info about file1
info about file2
... and so on
如果我要在末尾添加另一个选项。
第二次输入
./prog -f file1 file2 file3 file4 -j smth
info about1 file1
action with -j smth. **End.**
我试图在开头列出所有参数,第二个输入如下:
for (int i = 0; i < argc; i++) {
printf("argv[%d] = %s\n", i, argv[i]);
}
结果如预期(第二次输入)
-f, file1,file... -j,print
我再次将这个 for 循环包含在我的 switch 中,在 'f' 的情况下。 使用第二个输入,我看到已经有 3 个参数是 -j,而第 4 个是 j 的选项,只有在那之后才有所有其他文件。 输出就像你已经猜到了,
file1, -j, print, file2, file3,file4
我的问题是为什么会这样?或者我应该如何解决它,或者我需要看看? 我还尝试改进我的代码,查看这里回答的几个类似问题,但看起来我已经遵循了所有建议。
Parsing command line options with multiple arguments [getopt?]
Get several values for the same option [duplicate]
通过在引号中传递所有参数的解决方案对我不起作用,因为用户可能希望重定向来自 ls 的输入,例如,或者在最后传递带有 * 的目录。
谢谢你:)
【问题讨论】:
-
为什么不只是
./someProgram -f opt1 -f opt2 -f opt3 -j opt -d? -
@KamilCuk,因为如果你调用这样的程序
./p -f /dir/dir/* -j opt -d它将不起作用。 -
请不要破坏您自己的帖子。当您在此处发布时,即表示您授予 SO 在 CC-by SA 4.0 下分发内容的权利。任何破坏行为都将被撤销。
标签: c arguments getopt optional-arguments getopt-long