The traditional way of indicating the end of options for command line programs is with the option --
. How can I get boost::program_options to recognize this as an option and accept the rest of the command line as positional arguments? The following doesn't work:
namespace po = boost::program_options;
po::positional_options_description posOpts;
posOpts.add("keywords", 1);
posOpts.add("input", 1);
std::vector<std::string> final_args;
po::options_description desc("Allowed Options");
desc.add_options()
...
("", po::value< std::vector<std::string> >(&final_args)->multitoken(), "end of options")
...
;
po::command_line_parser(argc, argv).options(desc).positional(posOpts).run();
If I give foo bar
as arguments, I get nothing in final_args
(as expected), but also when I give -- foo bar
as arguments (when I would expect to find final_args[0] == "foo"
and final_args[1] == "bar"
). I'm assuming here that --
is a long argument with the empty string as its argument name. If, instead, it's supposed to be interpreted as a short argument, with -
as the argument name, how do I specify that? Changing the argument specification from ""
to ",-"
doesn't affect the result, so far as I can see.
How does one get boost::program_options to handle --
correctly?
Edit: Here's an attempt to do what Tim Sylvester suggested by creating an extra_style_parser
:
std::vector<po::option> end_of_opts_parser(std::vector<std::string>& args) {
std::vector<po::option> result;
std::vector<std::string>::const_iterator i(args.begin());
if (i != args.end() && *i == "--") {
for (++i; i != args.end(); ++i) {
po::option opt;
opt.string_key = "pargs";
opt.original_tokens.push_back(*i);
result.push_back(opt);
}
args.clear();
}
return result;
}
"pargs"
was added to the options like this:
("pargs", po::value< std::vector<std::string> >(&pargs), "positional arguments")
Running this with a --
in the argument list causes a required_option
exception. (I get similar results if instead of making a po::option
for each trailing arg, I pack them all into po::option::original_tokens
in one po::option
.)