C语言 Getopt- 为参数传递字符串参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17877368/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getopt- Passing string parameter for argument
提问by RagHaven
I have a program which takes in multiple command line arguments so I am using getopt. One of my arguments takes in a string as a parameter. Is there anyway to obtain that string through the getopt function or would I have to obtain it through the argv[] array? Also can getopt read args like -file? All the arguments I have seen till now have only one character such as -a
我有一个接受多个命令行参数的程序,所以我正在使用 getopt。我的一个参数接受一个字符串作为参数。无论如何要通过 getopt 函数获取该字符串,还是必须通过 argv[] 数组获取它?也可以 getopt 读取 args 之类的-file?到目前为止,我所看到的所有参数都只有一个字符,例如-a
EDIT
编辑
From the below answers I have written a program to use getopt_long(), but the switch statement only recognizes the argument when I use the character argument and not the long argument. I'm not sure why this happening. On passing the arguments -mf -file sampleI do not see the print statements.
从下面的答案中,我编写了一个使用 getopt_long() 的程序,但是当我使用字符参数而不是长参数时,switch 语句只识别参数。我不确定为什么会发生这种情况。在传递参数时,-mf -file sample我没有看到打印语句。
EDIT
编辑
I tried entering the command arguments as --fileand then it worked. Is it not possible to do this with just -file?
我尝试输入命令参数--file,然后它起作用了。难道不能用 just 来做到这一点-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;
}
}
回答by phoxis
Read man getopthttp://linux.die.net/man/3/getopt
阅读man getopthttp://linux.die.net/man/3/getopt
optstring is a string containing the legitimate option characters. If such a character is followed by a colon, the option requires an argument, so getopt() places a pointer to the following text in the same argv-element, or the text of the following argv-element, in optarg. Two colons mean an option takes an optional arg; if there is text in the current argv-element (i.e., in the same word as the option name itself, for example, "-oarg"), then it is returned in optarg, otherwise optarg is set to zero.
optstring 是一个包含合法选项字符的字符串。如果这样的字符后跟一个冒号,则该选项需要一个参数,因此 getopt() 在 optarg 中将指向以下文本的指针放置在同一 argv 元素中,或以下 argv 元素的文本中。两个冒号表示一个选项需要一个可选的参数;如果当前 argv 元素中有文本(即,与选项名称本身在同一个词中,例如,“-oarg”),则它在 optarg 中返回,否则 optarg 设置为零。
A sample code:
示例代码:
#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;
}
Here in the optstringis "i:o:" the colon ':' character after each character in the string tells that those options will require an argument. You can find argument as a string in the optargglobal var. See manual for detail and more examples.
这里optstring是 "i:o:" 字符串中每个字符后面的冒号 ':' 字符告诉这些选项需要一个参数。您可以在optarg全局变量中找到作为字符串的参数。有关详细信息和更多示例,请参阅手册。
For more than one character option switches, see the long options getopt_long. Check the manual for examples.
对于不止一个字符的选项开关,请参阅长选项getopt_long。查看手册以获取示例。
EDITin response to the single '-' long options:
编辑以响应单个“-”长选项:
From the man pages
从手册页
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.
getopt_long_only() 与 getopt_long() 类似,但“-”和“--”可以表示长选项。如果以“-”(不是“--”)开头的选项与长选项不匹配,但与短选项匹配,则将其解析为短选项。
Check the manual and try it.
检查手册并尝试。
回答by David M. Syzdek
To specify that a flag requires an argument, add a ':' right after the flag in the short_opt variable. To use long arguments, use getopt_long().
要指定标志需要参数,请在 short_opt 变量中的标志后添加一个“:”。要使用长参数,请使用 getopt_long()。
Here is a quick example program:
这是一个快速示例程序:
#include <getopt.h>
#include <stdio.h>
int main(int argc, char * argv[]);
int main(int argc, char * argv[])
{
int c;
const char * short_opt = "hf:";
struct option long_opt[] =
{
{"help", no_argument, NULL, 'h'},
{"file", required_argument, NULL, 'f'},
{NULL, 0, NULL, 0 }
};
while((c = getopt_long(argc, argv, short_opt, long_opt, NULL)) != -1)
{
switch(c)
{
case -1: /* no more arguments */
case 0: /* long options toggles */
break;
case 'f':
printf("you entered \"%s\"\n", optarg);
break;
case 'h':
printf("Usage: %s [OPTIONS]\n", argv[0]);
printf(" -f file file\n");
printf(" -h, --help print this help and exit\n");
printf("\n");
return(0);
case ':':
case '?':
fprintf(stderr, "Try `%s --help' for more information.\n", argv[0]);
return(-2);
default:
fprintf(stderr, "%s: invalid option -- %c\n", argv[0], c);
fprintf(stderr, "Try `%s --help' for more information.\n", argv[0]);
return(-2);
};
};
return(0);
}

