BOOST program_options:解析多个参数列表
我想传递具有正值或负值的多个参数。 可以解析吗?
目前我有以下初始化:
vector<int> IDlist;
namespace po = boost::program_options;
po::options_description commands("Allowed options");
commands.add_options()
("IDlist",po::value< vector<int> >(&IDlist)->multitoken(), "Which IDs to trace: ex. --IDlist=0 1 200 -2")
("help","print help")
;
并且我想调用:
./test_ids.x --IDlist=0 1 200 -2
unknown option -2
因此,program_options 假设我将 -2 作为另一个选项传递。
我可以以可接受负整数值的方式配置program_options吗?
谢谢 阿曼.
编辑: 顺便说一句,我是通过简单的解析器解析它的
store(command_line_parser(argc, argv).options(commands).run(), vm);
,但是 解决方案 是使用扩展的:
parse_command_line
I would like to pass the multiple arguments with positive or negative values.
Is it possible to parse it?
Currently I have a following initialization:
vector<int> IDlist;
namespace po = boost::program_options;
po::options_description commands("Allowed options");
commands.add_options()
("IDlist",po::value< vector<int> >(&IDlist)->multitoken(), "Which IDs to trace: ex. --IDlist=0 1 200 -2")
("help","print help")
;
and I would like to call:
./test_ids.x --IDlist=0 1 200 -2
unknown option -2
So,the program_options assumes that I am passing -2 as an another option.
Can I configure the program_options in such a way that it can accept the negative integer values?
Thanks
Arman.
EDIT:
BTW I was parsing it by the simple parser
store(command_line_parser(argc, argv).options(commands).run(), vm);
, but solution was to use the extended one:
parse_command_line
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你试过“-2”吗?编辑:引用似乎并不能解决问题,但是,更改命令行样式是有效的:
Have you tried "-2"?Edit: Quoting doesn't seem to do the trick, however, changing the command line style works:
注意:这是对已接受解决方案的评论。
禁用短选项是关键。 kloffy 提出的上述解决方案效果很好,但如果您碰巧使用
positional_option_description
(例如,在不使用ls file.txt 而不是 ls 等选项的情况下解析参数) --file=file.txt
)您可能很难使用parse_command_line
转换代码来执行此操作。不过,您也可以禁用短选项并继续使用
basic_command_line_parser
,如下所示:替换
为
NOTE: this is a remark to the accepted solution.
Disabling short options is the key. The solution above proposed by kloffy works great, but if you happen to use
positional_option_description
(e.g. to parse parameters without using an option likels file.txt instead of ls --file=file.txt
) you might have a hard time converting your code to do that usingparse_command_line
.However you can also disable short options and keep using the
basic_command_line_parser
like this:Replace
with
也许尝试 --IDlist "0, 1, 200, -2" 或 --IDlist="0, 1, 200, -2"
maybe try --IDlist "0, 1, 200, -2" or --IDlist="0, 1, 200, -2"