views:

117

answers:

1

I am using boost::program_options like this:

namespace po = boost::program_options;
po::options_description desc("Options");
desc.add_options()
  ("help,?", "Show Options")
  ("capture-file,I", po::value<string>(), "Capture File")   
  ("capture-format,F", po::value<string>()->default_value("pcap"), "Capture File Format")
  ("output-file,O", po::value<string>()->default_value("CONOUT$"), "Output File");

po::variables_map vm;
po::store(po::command_line_parser(ac, av).options(desc)./*positional(pd).*/run(), vm);

If I pass the command line parameter -I hithere it works, but it I pass /I hithere boost throws a boost::bad_any_cast with a what() of " failed conversion using boost::any_cast".

Is it possible to use program_options to parse either /-delimitted or --delimitted options? Bonus question, can it be configured so that / and - set the same option, but are binary opposites of each other? For example, /verbose might mean verbose logging while -verbose might mean less verbose logging.

+3  A: 

To use / and -, use command_line_parser's style() method with the appropriate combination of style_t flags. For example:

po::store(po::command_line_parser(ac, av)
    .options(desc)
    .style(po::command_line_style::default_style
        | po::command_line_style::case_insensitive
        | po::command_line_style::allow_slash_for_short
        | po::command_line_style::allow_long_disguise)
    /*.positional(pd)*/
    .run(), vm);

(allow_long_disguise lets / start a long option.)

You could probably make / and - opposites by adding your own additional parser; however, this would be very nonstandard and therefore potentially confusing to end users, so I'm not sure it's a good idea.

Josh Kelley
You're probably right. Is it possible to determine which delimiter was used after the call to `store`?
John Dibling
Unless you're using your own additional parser, I don't think so.
Josh Kelley
OK, that was a bonus question anyway. :)
John Dibling