BOOST program_options:解析多个参数列表

发布于 2024-08-27 04:01:25 字数 1010 浏览 14 评论 0原文

我想传递具有正值或负值的多个参数。 可以解析吗?

目前我有以下初始化:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

忱杏 2024-09-03 04:01:25

你试过“-2”吗?

编辑:引用似乎并不能解决问题,但是,更改命令行样式是有效的:

char* v[] = {"name","--IDlist=0","1","200","-2"};
int c = 5;

std::vector<int> IDlist;

namespace po = boost::program_options;     
po::options_description commands("Allowed options");
commands.add_options()              
    ("IDlist",po::value< std::vector<int> >(&IDlist)->multitoken(), "Which IDs to trace: ex. --IDlist=0 1 200 -2")
    ("help","print help")
;

po::variables_map vm;
po::store(parse_command_line(c, v, commands, po::command_line_style::unix_style ^ po::command_line_style::allow_short), vm);
po::notify(vm);

BOOST_FOREACH(int id, IDlist)
    std::cout << id << std::endl;

Have you tried "-2"?

Edit: Quoting doesn't seem to do the trick, however, changing the command line style works:

char* v[] = {"name","--IDlist=0","1","200","-2"};
int c = 5;

std::vector<int> IDlist;

namespace po = boost::program_options;     
po::options_description commands("Allowed options");
commands.add_options()              
    ("IDlist",po::value< std::vector<int> >(&IDlist)->multitoken(), "Which IDs to trace: ex. --IDlist=0 1 200 -2")
    ("help","print help")
;

po::variables_map vm;
po::store(parse_command_line(c, v, commands, po::command_line_style::unix_style ^ po::command_line_style::allow_short), vm);
po::notify(vm);

BOOST_FOREACH(int id, IDlist)
    std::cout << id << std::endl;
拥抱没勇气 2024-09-03 04:01:25

注意:这是对已接受解决方案的评论。

禁用短选项是关键。 kloffy 提出的上述解决方案效果很好,但如果您碰巧使用 positional_option_description (例如,在不使用 ls file.txt 而不是 ls 等选项的情况下解析参数) --file=file.txt)您可能很难使用 parse_command_line 转换代码来执行此操作。

不过,您也可以禁用短选项并继续使用 basic_command_line_parser,如下所示:

替换

store(command_line_parser(argc, argv).options(commands).run(), vm);

store(command_line_parser(argc, argv).options(commands).style(
po::command_line_style::unix_style ^ po::command_line_style::allow_short
).run(), vm);

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 like ls file.txt instead of ls --file=file.txt) you might have a hard time converting your code to do that using parse_command_line.

However you can also disable short options and keep using the basic_command_line_parser like this:

Replace

store(command_line_parser(argc, argv).options(commands).run(), vm);

with

store(command_line_parser(argc, argv).options(commands).style(
po::command_line_style::unix_style ^ po::command_line_style::allow_short
).run(), vm);
木緿 2024-09-03 04:01:25

也许尝试 --IDlist "0, 1, 200, -2" 或 --IDlist="0, 1, 200, -2"

maybe try --IDlist "0, 1, 200, -2" or --IDlist="0, 1, 200, -2"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文