- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我有一个接受多个命令行参数的程序,所以我正在使用 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/
是否可以将参数传递给通过 getopt::long 调用的子例程?例如,当用户在命令行上指定 script.pl -pandora argument 时,我有此代码调用 &Salt GetOption
我正在尝试将 getopts 用于 bash 脚本。这个脚本可以有标志,所有这些标志都是强制性的,需要包含一个值。当本应包含值的强制标志之一为空时,getopts 使用下一行标志作为其内容。我该如何防
我公司使用 Getopt::Declare 作为其命令行选项解析器。我们的期权处理 block 的结构通常如下所示: Readonly my $ARGS => Getopt::Declare->new
我有一个带有可能的命令行参数的字符串(使用 Read-Eval-Print-Loop 程序),我希望它在传递给 Getopt::Long 时被解析为类似于命令行参数。 详细说明: 我有一个字符串 $s
我刚刚在代码审查中第一次被要求检查对 GetOptions() 的调用的返回码。 Getopt::Long 的功能Perl 模块。 我不记得曾经看到过这样的 GetOptions() 测试。功能。 那
我有这个 getopt: GetOptions( GetOptions ("library=s" => \@libfiles); @libfiles = split(/,/,join(','
我想使用 Getopt::Long::GetOptions 获取脚本的命令行选项。 我有这样的需求: perl script.pl -c -c -m argument 这里我们有选项标志 -c
我正在尝试使用 getopt() 解析命令行参数。下面是我的代码。无论我在运行程序时传递什么参数,getopt() 总是返回 -1。 例如: $ gcc -o test test.c $ ./test
在我的 python 脚本中使用 getopt.getopt() 函数时,临时返回值保持为空。我缺少什么。 def ParseOpts(cmdName): shortForm = 'c:n:'
我想使用 getopt,但它行不通。 它给了我 gcc -g -Wall -std=c99 -ftrapv -O2 -Werror -Wshadow -Wundef -save-temps -Werr
有人可以帮助我使用 getopt 函数吗? 当我在 main 中执行以下操作时: char *argv1[] = {"testexec","-?"}; char *argv2[] = {"testex
我有一个脚本,它从 CLI 获取 3 个输入变量并将其分别插入到 3 个变量: GetOptions("old_path=s" => \$old_path, "var=s" =
如何以这种方式接受命令行参数: ./a.out --printall 所以在我的程序中,我有类似的东西 if (printall) { // do something } 我不想这样做: if (
在 Perl getopts 中,是否可以多次使用相同的选项但具有不同的值?我想为用户提供输入不同网格坐标的选项,但使用相同的选项名称以尽量减少混淆。 例如: my_grid.pl --coords=
是否有用于Groovy的标准或推荐的getopts库,该库可以让我快速处理Groovy中的长期和短期命令行争论? groovy foo.groovy --fname = foo.txt --outpu
use Getopt::Long::Configure(pass_through); # .... GetOptions( "display=s" => \$display,
我正在运行 bash 4.2 版,我正在尝试使用内置命令 getopts 解析命令行参数, 但是getopts好像没有正确解析,如果-s不是第一个参数,就不会被解析 -s 未解析: %> ./geto
GetOptions( "r|repo=s" => \$repo, "R|list-repos" => \$list, ); 当我用 -r qwe 调用这个脚本
我正在尝试使用 getopt() 从 PHP7 cli 获取选项,但是在调用 php myprocess.php task -d -o 时我得到一个空数组。不知道我错过了什么,希望你能帮助我。 这就是
我正在寻找一种方法来处理包含必须解析的空格的参数 通过 shell getopts 命令。 while getopts ":a:i:o:e:v:u:" arg do echo "ARG is:
我是一名优秀的程序员,十分优秀!