Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Boost program_options Can I create shorthand options with more than one character? [duplicate]

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);
like image 393
Dan Avatar asked Nov 24 '22 21:11

Dan


1 Answers

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.

like image 70
Rob Kennedy Avatar answered May 13 '23 17:05

Rob Kennedy