18

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);
Mankarse
  • 39,818
  • 11
  • 97
  • 141
Dan
  • 12,857
  • 7
  • 40
  • 57
  • possible duplicate of [How to support commandline syntax "-DEVICE:iphone" in Boost::Program_Options?](http://stackoverflow.com/questions/6906991/how-to-support-commandline-syntax-deviceiphone-in-boostprogram-options) – Nicol Bolas Aug 30 '11 at 03:07
  • Yes pretty much duplicate but the answer given there is not quite correct. It's not hard to work out what the solution is based on that but I've added it here for completeness. – Dan Aug 30 '11 at 09:23

1 Answers1

29

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.

Rob Kennedy
  • 161,384
  • 21
  • 275
  • 467
  • 7
    Oh dear, I swear that wasn't there when I looked. Not enough Coffee obviously. I'll try it out tomorrow when I get a chance. Thanks. – Dan Aug 29 '11 at 18:22
  • 2
    This seems to be the best solution, although there are a few caveats to be aware of. An unknown argument "`-myopt1`" will trigger the exception message "`unknown option -m`". Also the autogenerated help text will still show "`--myopt2`". – Drew Dormann Jun 22 '12 at 15:20