[英]Does any of the command line parsing libraries for C++ allow options with N arguments
I am porting a python script to C++ and have not found a way to express an argument such as: 我正在将python脚本移植到C ++,但是还没有找到一种表达参数的方法,例如:
--metadata KEY VALUE
In the C++ argument parsing libraries I've seen: 在C ++参数解析库中,我看到了:
Maybe it exists as a possibility in one of those, maybe I have to explore others, maybe I just have to do this by hand. 也许其中一种可能存在,也许我必须探索其他事物,也许我只需要手工做。
Do you know of a C++ command line option parsing library that supports multiple arguments for options? 您是否知道支持选项多个参数的C ++命令行选项解析库?
Command-line flags which take more than one argument are discouraged by the standard shell utilities syntax guidelines. 标准的shell实用程序语法准则不建议采用多个参数的命令行标志。 So I doubt whether you will find a standard library which handles it. 因此,我怀疑您是否会找到一个处理它的标准库。 It's more common to accept options in the format, for example: 接受格式中的选项更为常见,例如:
--metadata KEY=VALUE
and you'll probably find standard libraries which will help you with that format (of course, strtok
or std::string::find
are straightforward to use, too). 并且您可能会找到标准库,这些库将帮助您使用这种格式(当然, strtok
或std::string::find
也很容易使用)。
If you really insist on the multiple-option argument design choice, you can do it with getopt_long
if you tell it not to permute arguments (put a +
as the first character of optstring
); 如果您确实坚持多选项参数设计选择,那么如果您告诉它不要置换参数(使用+
作为optstring
的第一个字符),则可以使用getopt_long
来完成; all you need to do is to manually increment optind
as you read the arguments. 您需要做的就是在读取参数时手动增加optind
。 (That is: the first argument will be in optarg
, the next one at argv[optind++]
and so on.) There are probably equivalent low-level hacks in the C++ libraries, too. (也就是说,第一个参数位于optarg
,第二个参数位于argv[optind++]
,依此类推。)C ++库中也可能存在等效的低级技巧。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.