|
Boost Users : |
Subject: [Boost-users] program_options Parsing Suboptions
From: Robert (robertln_at_[hidden])
Date: 2014-09-14 17:17:43
Hi,
I am trying to use program_options to parse suboptions:
Command âS â-a 1 âb test âc 1.2â
(the suboptions in âS are to be passed into other programs)
Using the example source code, I tried something like the code below,
but get the following error The argument (â 1â) for option ââTestAâ is
invalid. Apparently the space between âa and 1 is treated as part of the
argument and thus will not convert to an integer.
I tried at the top argument level (e.g. command âM 1) and it works as
expected, so something about creating the vector of suboptions is
confusing program_options.
Any suggestions? Thanks very much.
options_description desc("Allowed options");
desc.add_options()("subopt,S",value<string>(),"Sub Options");
variables_map vm; store(command_line_parser(ac,
av).options(desc).run(), vm);
vm.notify();
if (vm.count("subopt")) { string
substring=vm["subopt"].as<string>();
boost::regex re("\\s+(?=-)");
boost::sregex_token_iterator i(substring.begin(),
substring.end(), re, -1);
boost::sregex_token_iterator j;
vector<string> splitResults;
copy(i,j,back_inserter(splitResults));
int ta; string tb; float tc;
options_description subopt_desc("Suboptions");
subopt_desc.add_options() ("TestA,a",
value<int>(&ta)->default_value(0), "integer")
("TestB,b",value<string>(&tb),"string")
("TestC,c",value<float>(&tc), "float") ;
variables_map subopt_vm;
store(command_line_parser(splitResults).options(subopt_desc).run(),
subopt_vm);
subopt_vm.notify();
cout<<"a="<<ta<<endl; cout<<"b="<<tb<<endl;
cout<<"c="<<tc<<endl;
}
-- http://www.fastmail.fm - Choose from over 50 domains or use your own
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net