views:

122

answers:

2

In python, I can construct my optparse instance such that it will automatically filter out the options and non-option/flags into two different buckets:

(options, args) = parser.parse_args()

With boost::program_options, how do I retrieve a list of tokens which are the remaining non-option and non-flag tokens?

e.g. If my program has flags

--foo 
--bar BAR

and I then pass in the command line:

--foo hey --bar BAR you

how can I get a list comprised solely of tokens "hey" and "you"

+2  A: 

IIRC, you have to use a combination of positional_options_description and hidden options. The idea is to (1) add a normal option and give it a name, maybe something like --positional=ARG, (2) don't include that option in the help description, (3) configure command_line_parser to treat all positional arguments as if --positional was specified, and (4) retrieve the positional arguments using vm["positional"].as< std::vector<std::string> >().

There is probably an example somewhere in the source tree but I don't have it on this machine right now.

D.Shawley
+1  A: 

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 get "input" named options as vector of strings and you are all set.

Eugene