How to get tokens without a flag and without an option after boost :: program_options parses my command line arguments

In python, I can build an optparse instance so that it automatically filters the parameters and optional / flags into two different buckets:

(options, args) = parser.parse_args() 

With boost :: program_options, how do I get a list of tokens that are the rest of non-flags and non-flags?

eg. If my program has flags

 --foo --bar BAR 

and then I pass on the command line:

 --foo hey --bar BAR you 

how can I get a list consisting exclusively of hey and you tokens

+4
source share
2 answers

IIRC, you must use a combination of positional_options_description and hidden parameters . The idea is to (1) add a normal option and give it a name, perhaps something like --positional=ARG , (2) not include this option in the help description, (3) configure command_line_parser to handle all positional arguments as if --positional was specified, and (4) extracts positional arguments using vm["positional"].as< std::vector<std::string> >() .

There is probably an example somewhere in the source tree, but now I do not have it on this machine.

+2
source

Here is an example:

 namespace po = boost::program_options; po::positional_options_description m_positional; po::options_description m_cmdLine; po::variables_map m_variables; m_cmdLine.add_options() (/*stuff*/) ("input", po::value<vector<string> >()->composing(), "") ; m_positional.add("input", -1); po::parsed_options parsed = po::command_line_parser(argc, argv) .options(m_cmdLine) .positional(m_positional) .allow_unregistered() .run(); // store, notify, etc 

Then just enter "input" named options as the row vector, and you are all set.

+4
source

All Articles