开发者

Can boost::program_options Use A Delimiter Other Than "-"?

开发者 https://www.devze.com 2022-12-31 23:27 出处:网络
I am using boost::program_options like this: namespace po = boost::program_options; po::options_description desc(\"Options\");

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")
  ("outp开发者_开发技巧ut-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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消