Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how do I get the non-flag and non-option tokens after boost::program_options parses my command line args

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"

like image 796
Ross Rogers Avatar asked Aug 22 '09 00:08

Ross Rogers


2 Answers

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.

like image 172
Eugene Avatar answered Oct 13 '22 19:10

Eugene


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.

like image 34
D.Shawley Avatar answered Oct 13 '22 21:10

D.Shawley