gpt4 book ai didi

c - Getopt- 为参数传递字符串参数

转载 作者:太空狗 更新时间:2023-10-29 16:42:21 37 4
gpt4 key购买 nike

我有一个接受多个命令行参数的程序,所以我正在使用 getopt。我的一个参数接受一个字符串作为参数。无论如何要通过 getopt 函数获取该字符串,还是我必须通过 argv[] 数组获取它? getopt 也可以读取像 -file 这样的参数吗?到目前为止我看到的所有参数都只有一个字符,例如 -a

编辑

根据下面的答案,我编写了一个使用 getopt_long() 的程序,但是当我使用字符参数而不是长参数时,switch 语句只识别参数。我不确定为什么会这样。在传递参数 -mf -file sample 时,我没有看到打印语句。

编辑

我尝试将命令参数作为 --file 输入,然后成功了。仅使用 -file 是不可能做到这一点的吗?

static struct option long_options[] =
{
{"mf", required_argument, NULL, 'a'},
{"md", required_argument, NULL, 'b'},
{"mn", required_argument, NULL, 'c'},
{"mw", required_argument, NULL, 'd'},
{"lf", required_argument, NULL, 'e'},
{"ld", required_argument, NULL, 'f'},
{"ln", required_argument, NULL, 'g'},
{"lw", required_argument, NULL, 'h'},
{"rf", required_argument, NULL, 'i'},
{"rd", required_argument, NULL, 'j'},
{"rn", required_argument, NULL, 'k'},
{"rw", required_argument, NULL, 'l'},
{"df", required_argument, NULL, 'm'},
{"dd", required_argument, NULL, 'n'},
{"dn", required_argument, NULL, 'o'},
{"dw", required_argument, NULL, 'p'},
{"file", required_argument, NULL, 'q'},
{NULL, 0, NULL, 0}
};
int ch=0;
while ((ch = getopt_long(argc, argv, "abcdefghijklmnopq:", long_options, NULL)) != -1)
{
// check to see if a single character or long option came through
switch (ch){
case 'a':
cout<<"title";
break;
case 'b':

break;
case 'c':

break;
case 'd':

break;
case 'e':

break;
case 'f':

break;
case 'g':

break;
case 'h':

break;
case 'i':

break;
case 'j':

break;
case 'k':

break;
case 'l':

break;
case 'm':

break;
case 'n':

break;
case 'o':

break;
case 'p':

break;
case 'q':
cout<<"file";
break;
case '?':
cout<<"wrong message"
break;
}
}

最佳答案

阅读 man getopt http://linux.die.net/man/3/getopt

optstring is a string containing the legitimate option characters. Ifsuch a character is followed by a colon, the option requires anargument, so getopt() places a pointer to the following text in thesame argv-element, or the text of the following argv-element, inoptarg. Two colons mean an option takes an optional arg; if there istext in the current argv-element (i.e., in the same word as the optionname itself, for example, "-oarg"), then it is returned in optarg,otherwise optarg is set to zero.

示例代码:

#include <stdio.h>
#include <unistd.h>

int main (int argc, char *argv[])
{
int opt;
while ((opt = getopt (argc, argv, "i:o:")) != -1)
{
switch (opt)
{
case 'i':
printf("Input file: \"%s\"\n", optarg);
break;
case 'o':
printf("Output file: \"%s\"\n", optarg);
break;
}
}
return 0;
}

这里在 optstring 中是“i:o:”,字符串中每个字符后的冒号“:”告诉这些选项需要一个参数。您可以在 optarg 全局变量中找到作为字符串的参数。有关详细信息和更多示例,请参阅手册。

对于多于一个字符的选项开关,请参见长选项 getopt_long。查看手册中的示例。

EDIT 以响应单个“-”长选项:

来自手册页

getopt_long_only() is like getopt_long(), but '-' as well as "--" can indicate a long option. If an option that starts with '-'(not "--") doesn't match a long option, but does match a short option,it is parsed as a short option instead.

查看手册并尝试一下。

关于c - Getopt- 为参数传递字符串参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17877368/

37 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com