16

I'm using boost::program_options to implement a command-line utility with this syntax:

myutil command [--in file_name] [---out file_name]

where 'command' is mandatory, and is one of the following:

read | write | find | version | help

the thing is that 'command' should not require -- or - and I have not found how to do that with boost::program_options.

Himanshu
  • 31,810
  • 31
  • 111
  • 133
Periodic Maintenance
  • 1,698
  • 4
  • 20
  • 32

1 Answers1

13

The command line options which have no name are called positional options:

po::positional_options_description p;
p.add("command", -1);
po::variables_map vm;
po::store(po::command_line_parser(ac, av).
          options(desc).positional(p).run(), vm);
perreal
  • 94,503
  • 21
  • 155
  • 181
  • 15
    Yes this works. One important caveat: positional option should be added as a regular option as well: `po::positional_options_description p;` `p.add("command", -1);` `boost::program_options::options_description desc;` `desc.add_options()("command", "read | write | find | version | help");` `po::variables_map vm;` `po::store(po::command_line_parser(ac, av).options(desc).positional(p).run(), vm);` – Periodic Maintenance Jul 30 '12 at 08:18