Question

I'm using Boost Program Options to parse CLI.

The problem I'm facing is that if there is any token in CLI without '-' or '--' in front of it, the library silently ignores it instead of throwing exception.

Following is the sample program:

try
{
    options_description od;
    od.add_options()
        ("dummy,d", value<int>()->required(), "does nothing...");

    variables_map vm;
    wparsed_options po = parse_command_line(argc, argv, od);
    store(po, vm);
    notify(vm);

    cout << vm["dummy"].as<int>() << endl;
}
catch (const error& e)
{
    cout << e.what() << endl;
}

Following are some sample runs:

Debug>test
the option '--dummy' is required but missing

Debug>test -d
the required argument for option '--dummy' is missing

Debug>test -d 1
1

Debug>test -d 1 asas
1

Now, the first three runs are as expected. But, why is the third run not throwing any exception? 'asas' doesn't matches any option and -d doesn't accepts vector. What am I doing wrong? Or the library is designed this way?

Était-ce utile?

La solution

  1. a token without preceding dashes is called a positional argument
  2. you should explicitly forbid positionals for the expected behaviour
  3. to do this make empty list of positionals and feed it to the parser https://stackoverflow.com/a/3859400/670719
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top