关闭 getopt_long (getopt.h) 中的缩写?
是否可以在 getopt_long()
中关闭缩写?从手册页:
如果缩写是唯一的或者与某些定义的选项完全匹配,则可以缩写长选项名称。
我想这样做是因为我收到的一段代码的规范要求标志的全长精确匹配,并且有很多标志。
Is it possible to turn off abbreviation in getopt_long()
? From the man page:
Long option names may be abbreviated if the abbreviation is unique or is an exact match for some defined option.
I want to do this because the specification I have received for a piece of code requires a full-length exact match of the flags, and there are many flags.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
似乎没有办法禁用缩写功能。您并不是唯一一个希望获得此功能的人。请参阅:http://sourceware.org/bugzilla/show_bug.cgi?id=6863< /a>
不幸的是,glibc 开发人员似乎不想要这个选项,因为上面链接的错误报告已通过“WONTFIX”解决。你可能不走运:-\
It appears there isn't a way to disable the abbreviation feature. You aren't alone in wishing for this feature. See: http://sourceware.org/bugzilla/show_bug.cgi?id=6863
Unfortunately, It seems the glibc developers don't want the option as the bug report linked above was resolved with "WONTFIX". You may be out of luck here :-\
如果您使用 argp_parse() 而不是 getopt() (强烈推荐,顺便说一句),您可以通过访问用户输入的确切标志。
这有点黑客,但应该可以工作。
If you use argp_parse() instead of getopt() (highly reccommended, BTW), you can access the exact flag entered by the user through
It's a bit of a hack, but should work.
这不是完美的解决方案,但您可以在调用 getopt_long() (通常在 switch 内)后检查用户给出的确切参数,如下所示:
optind 指向您需要处理的下一个参数。因此,您可以使用 optind-1 访问原始 arg。
This is not perfect solution but you can check exact arg given by a user after calling getopt_long() (normally within switch) like below:
optind points a next argument that you need to process. Thus, you can access the original arg using optind-1.