为了论证Getopt-传递字符串参数 [英] Getopt- Passing string parameter for argument
问题描述
我有一个计划,需要在多个命令行参数,所以我使用getopt的。我的一个参数接受一个字符串作为参数。反正是有通过getopt函数来获取字符串或我将不得不通过的argv []数组获得它?也可以getopt说明读起来像 -file
ARGS?我已经看到到现在的所有参数都只有一个字符,如 -a
修改
从下面的答案,我写了一个程序来使用getopt_long(),但是switch语句只承认当我使用的字符参数,而不是长期争论的论点。我不知道为什么这种情况发生。在传递的参数 -mf -file样品
我没有看到打印报表。
修改
我试着输入命令参数为 - 文件
,然后它的工作。它是不可能有这样做只是 -file
?
静态结构选项long_options [] =
{
{MF,required_argument,NULL,A},
{MD,required_argument,NULL,B},
{百万,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,'我'},
{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},
{文件,required_argument,NULL,Q},
{NULL,0,NULL,0}
};
INT CH = 0;
而((CH = getopt_long(ARGC,ARGV,abcdefghijklmnopq:long_options,NULL))!= -1)
{
//检查,看看是否有单个字符或长选项来通过
开关(CH){
案一:
COUT<<称号;
打破;
案例'B': 打破;
情况下C: 打破;
案D: 打破;
案例E: 打破;
案例'F': 打破;
案例'G': 打破;
案例'H': 打破;
案例'我': 打破;
案例'J': 打破;
案例'K': 打破;
案例'L': 打破;
案件的m: 打破;
案例'N': 打破;
案例'O': 打破;
案例'P': 打破;
案例'Q':
COUT<<文件;
打破;
案件 '?':
COUT<<错误消息
打破;
}
}
读男人的getopt
的 http://linux.die.net/man/3/getopt
optstring是包含合法选项字符的字符串。如果
这样的字符后跟一个冒号,选项需要一个
参数,所以getopt的()放置一个指针,在下面的文本
同样的argv元素,或以下的argv元素的文本,在
OPTARG。两个冒号意味着某种带有一个可选的ARG;如果有
文本在当前的argv元件(即,在相同的字作为选项
命名本身,例如,-oarg),则在OPTARG返回
否则OPTARG被设置为零。
块引用>一个样本code:
的#include<&stdio.h中GT;
#包括LT&;&unistd.h中GT;INT主(INT ARGC,CHAR *的argv [])
{
INT选择;
而((选择= getopt的(ARGC,ARGV,我:○:!))= - 1)
{
开关(OPT)
{
案例'我':
的printf(输入文件:\\%s \\的\\ n,OPTARG);
打破;
案例'O':
输出(输出文件:\\%s \\的\\ n,OPTARG);
打破;
}
}
返回0;
}下面的
optstring
是i:○:冒号:字符串中的每个字符后字符告诉这些选项需要一个参数。你可以找到说法是在OPTARG
全局变量的字符串。细节和更多的例子参见手册。有关多个字符选项开关,看到长选项
getopt_long
。检查手册的例子。修改响应单 - 长选项:
从手册页
getopt_long_only()是像getopt_long(),但 - ,以及 - 可以表示一个长的选择。如果开头的选项 -
(不是 - )不匹配的长选项,但匹配的短选项,
它被解析为一个短选项。
块引用>检查手动和尝试。
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
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 sample
I do not see the print statements.EDIT
I tried entering the command arguments as
--file
and then it worked. Is it not possible to do this with 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; } }
解决方案Read
man getopt
http://linux.die.net/man/3/getoptoptstring 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.
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
optstring
is "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 theoptarg
global var. See manual for detail and more examples.For more than one character option switches, see the long options
getopt_long
. Check the manual for examples.EDIT in 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.
Check the manual and try it.
这篇关于为了论证Getopt-传递字符串参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!