12

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
Community
  • 1
  • 1
Arman
  • 4,566
  • 10
  • 45
  • 66

3 Answers3

13

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;
kloffy
  • 2,928
  • 2
  • 25
  • 34
  • program_options are stripping "" , so "-2" became -2, also the \-2 takes as \-2 option, the single quotes '-2' stays as it is. – Arman Mar 29 '10 at 15:37
  • So the quotes are stripped and it is interpreted as another option? That is odd, from the documentation[1] it seemed like it would be the way to go. [1]http://www.boost.org/doc/libs/1_42_0/doc/html/program_options/overview.html#id1419101 – kloffy Mar 29 '10 at 15:45
  • I suppose quoting the entire list doesn't work either? (--IDlist="0 1 200 -2") – kloffy Mar 29 '10 at 15:52
  • yes, if I pass --IDlist="-0 100" the output is: in option 'IDlist': invalid option value '-0 100' Seems to me I should take IDlist as a vector of strings, then convert them to integers. – Arman Mar 29 '10 at 15:56
  • Sounds like a reasonable way to go about it. Not very satisfying though, you'd expect program_options to be able to handle that situation... – kloffy Mar 29 '10 at 16:06
  • 3
    It is the shell that is stripping the quotes. – Vladimir Prus Mar 29 '10 at 21:30
  • @Vladimir: good to know, actually I was testing it on WinXP, is it same on linux shells? – Arman Mar 30 '10 at 09:37
  • Arman -- I actually meant the Linux shell -- which creates an array of arguments and when doing so, processes quotes. On Windows, the command line is passed to the CreateProcess call, and then, I believe, stripping of quotes happens somewhere in runtime. – Vladimir Prus Apr 07 '10 at 20:31
9

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);
count0
  • 2,537
  • 2
  • 22
  • 30
  • Good point. Facing the same problem, I ended up reading the numbers as strings and asing the user to use quotes for negative numbers, to avoid disabling short options. – TomasG Sep 08 '15 at 18:21
-1

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

ano
  • 7
  • 1