0

I was wondering how is it possible to pass program options in boost without giving argument name like program.exe var1 instead of program.exe --arg1 var1. However I know how to handle this without boost lib with obtaining just argv[1].

There are two cases: one, when I have to specify all args with their argNames and two, when I have only one argument - fileName and now I know how to handle the first one but the problem is second one - how to handle it having all together via in boost lib? Or in simple - is it possible to do that?

1 Answers1

0

The tutorial has just such an example:

The "input-file" option specifies the list of files to process. That's okay for a start, but, of course, writing something like:

compiler --input-file=a.cpp

is a little non-standard, compared with

compiler a.cpp

We'll address this in a moment.

The command line tokens which have no option name, as above, are called "positional options" by this library. They can be handled too. With a little help from the user, the library can decide that "a.cpp" really means the same as "--input-file=a.cpp". Here's the additional code we need:

po::positional_options_description p;
p.add("input-file", -1);

po::variables_map vm;
po::store(po::command_line_parser(ac, av).
          options(desc).positional(p).run(), vm);
po::notify(vm);

The first two lines say that all positional options should be translated into "input-file" options. Also note that we use the command_line_parser class to parse the command line, not the parse_command_line function. The latter is a convenient wrapper for simple cases, but now we need to pass additional information.

Barry
  • 286,269
  • 29
  • 621
  • 977
  • So if I only want the `input-file` to be the second positional options I need to change -1 to 1 ? –  Oct 29 '15 at 21:55
  • Nope it's not what I've wanted to do. I need to code two cases: one, when I have to specify all args; two, when I have only one argument - fileName and now I know how to handle the first one but the problem is second one - how to handle it in boost having all together. –  Oct 29 '15 at 22:43