- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
我有一个接受各种命令行参数的程序。为了简单起见,我们说它需要 3 个标志,-a
、-b
和 -c
,并使用以下代码解析我的论点:
int c;
while((c = getopt(argc, argv, ":a:b:c")) != EOF)
{
switch (c)
{
case 'a':
cout << optarg << endl;
break;
case 'b':
cout << optarg << endl;
break;
case ':':
cerr << "Missing option." << endl;
exit(1);
break;
}
}
注意:a和b在标志后面带参数。
但是,如果我调用我的程序说,我会遇到问题
./myprog -a -b parameterForB
在我忘记了 parameterForA 的地方,parameterForA(由 optarg 表示)返回为 -b
并且 parameterForB 被认为是没有参数的选项,并且 optind 设置为 argv 中 parameterForB 的索引。
在这种情况下,期望的行为是 ':'
在找不到 -a
的参数后返回,并且 Missing 选项。
打印为标准错误。但是,这只发生在 -a
是传递给程序的最后一个参数的情况下。
我想问题是:有没有办法让 getopt()
假定没有选项会以 -
开头?
最佳答案
见 POSIX standard definition对于 getopt
。它说
If it [getopt] detects a missing option-argument, it shall return the colon character ( ':' ) if the first character of optstring was a colon, or a question-mark character ( '?' ) otherwise.
关于那个检测,
- If the option was the last character in the string pointed to by an element of argv, then optarg shall contain the next element of argv, and optind shall be incremented by 2. If the resulting value of optind is greater than argc, this indicates a missing option-argument, and getopt() shall return an error indication.
- Otherwise, optarg shall point to the string following the option character in that element of argv, and optind shall be incremented by 1.
看起来 getopt
被定义为不做你想做的事,所以你必须自己实现检查。幸运的是,您可以通过检查 *optarg
并自己更改 optind
来做到这一点。
int c, prev_ind;
while(prev_ind = optind, (c = getopt(argc, argv, ":a:b:c")) != EOF)
{
if ( optind == prev_ind + 2 && *optarg == '-' ) {
c = ':';
-- optind;
}
switch ( …
关于c++ - getopt 无法检测到选项的缺失参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2219562/
是否可以将参数传递给通过 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:
我是一名优秀的程序员,十分优秀!