getopt 不解析参数的可选参数
在 C 中,getopt_long 不会解析命令行参数参数的可选参数。
当我运行程序时,可选参数无法被识别,就像下面运行的示例一样。
$ ./respond --praise John
Kudos to John
$ ./respond --blame John
You suck !
$ ./respond --blame
You suck !
这是测试代码。
#include <stdio.h>
#include <getopt.h>
int main(int argc, char ** argv )
{
int getopt_ret, option_index;
static struct option long_options[] = {
{"praise", required_argument, 0, 'p'},
{"blame", optional_argument, 0, 'b'},
{0, 0, 0, 0} };
while (1) {
getopt_ret = getopt_long( argc, argv, "p:b::",
long_options, &option_index);
if (getopt_ret == -1) break;
switch(getopt_ret)
{
case 0: break;
case 'p':
printf("Kudos to %s\n", optarg); break;
case 'b':
printf("You suck ");
if (optarg)
printf (", %s!\n", optarg);
else
printf ("!\n", optarg);
break;
case '?':
printf("Unknown option\n"); break;
}
}
return 0;
}
In C, getopt_long does not parse the optional arguments to command line parameters parameters.
When I run the program, the optional argument is not recognized like the example run below.
$ ./respond --praise John
Kudos to John
$ ./respond --blame John
You suck !
$ ./respond --blame
You suck !
Here is the test code.
#include <stdio.h>
#include <getopt.h>
int main(int argc, char ** argv )
{
int getopt_ret, option_index;
static struct option long_options[] = {
{"praise", required_argument, 0, 'p'},
{"blame", optional_argument, 0, 'b'},
{0, 0, 0, 0} };
while (1) {
getopt_ret = getopt_long( argc, argv, "p:b::",
long_options, &option_index);
if (getopt_ret == -1) break;
switch(getopt_ret)
{
case 0: break;
case 'p':
printf("Kudos to %s\n", optarg); break;
case 'b':
printf("You suck ");
if (optarg)
printf (", %s!\n", optarg);
else
printf ("!\n", optarg);
break;
case '?':
printf("Unknown option\n"); break;
}
}
return 0;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
尽管 glibc 文档或 getopt 手册页中未提及,但长样式命令行参数的可选参数需要“等号”(=)。 将可选参数与形参分隔开的空格不起作用。
使用测试代码运行的示例:
Although not mentioned in glibc documentation or getopt man page, optional arguments to long style command line parameters require 'equals sign' (=). Space separating the optional argument from the parameter does not work.
An example run with the test code:
手册页当然没有很好地记录它,但是源代码有一点帮助。
简而言之:您应该执行如下操作(尽管这可能有点过于迂腐):
虽然你必须做一些字里行间的阅读。 以下内容可以满足您的要求:
The man page certainly doesn't document it very well, but the source code helps a little.
Briefly: you're supposed to do something like the following (though this may be a little over-pedantic):
... though you have to do some reading between the lines. The following does what you want:
我最近自己也遇到了这个问题。 我得出了与 Brian Vandenberg 和 Haystack 建议的类似的解决方案。 但为了提高可读性并避免代码重复,您可以将其全部包装在一个宏中,如下所示:
该宏可以这样使用:
如果您有兴趣,您可以在我写的博客文章中详细了解该解决方案的工作原理:
https://cfengine.com/blog/2021/optical-arguments -with-getopt-long/
该解决方案已经过测试,并且在撰写本文时目前已在 CFEngine 中使用。
I recently came across this issue myself. I arrived at a similar solution to the one Brian Vandenberg and Haystack suggested. But to improve readability and avoid code duplication, you can wrap it all up in a macro like below:
The macro can be used like this:
If you are interested, you can read more about how this solution works in a blog post I wrote:
https://cfengine.com/blog/2021/optional-arguments-with-getopt-long/
This solution is tested and is – at the time of this writing – currently used in CFEngine.
我也遇到了同样的问题,就来到了这里。 然后我意识到了这一点。
您没有太多“可选参数”的用例。 如果需要一个选项,您可以从程序逻辑中进行检查,如果一个选项是可选的,那么您不需要执行任何操作,因为在 getopt 级别,所有选项都是可选的,它们不是强制性的,因此不存在“可选参数”的用例。 希望这可以帮助。
ps:对于上面的例子,我认为正确的选项是
--赞美 --赞美名称“名称” --责备 --责备名称“名称”
I also ran into the same problem and came here. Then I realised this .
You don't have much of a use case of "optional_argument" . If an option is required you check from program logic, if an option is optional then you need not do anything because at getopt level all options are optional , they are not mandatory, so there is no use case of "optional_argument". Hope this helps.
ps: for the above example i think the correct options are
--praise --praise-name "name" --blame --blame-name "name"