开发者

BOOST program_options: parsing multiple argument list

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_descripti开发者_开发知识库on 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


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;


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);


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜