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"
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With