Boost Program Options Syntax

2019-02-04 12:42发布

I'm using boost::program_options to read the users' input from the command line argument. It works very nicely and allows me to output helpful usage messages and validate input properly. However, by default long option names must come after a double-dash for example --my_long_option and short options come after a single dash and must be a single character, example; -m.

Is there a way to either...

  • Allow long options after a single -?
  • Allow short options to have more than one character?

Thus allowing me to have command lines which looks like

./a.out -myopt1 foo -myopt2 bar

The two possibilities should have the same effect though from a programming point of view the first would be better. I had a look at boost::program_options::command_line_style but it doesn't look like it can do what I need.

Thanks

Edit: Further to accepted answer below to get it to use that style one must add the following code (following the naming convention of the boost docs)

po::store(
    po::command_line_parser(ac,av)
        .options(desc)
        .style(
            po::command_line_style::unix_style
          | po::command_line_style::allow_long_disguise)
        .run(),
    vm);

1条回答
女痞
2楼-- · 2019-02-04 13:35

Short options by definition have just one character. If they had more, they'd be long options.

To allow long options to start with a single dash, include the allow_long_disguise command-line style, as described on the documentation page you linked to:

It's possible to introduce long options by the same character as short options, see allow_long_disguise.

查看更多
登录 后发表回答