0

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?

anni
  • 338
  • 2
  • 12

1 Answers1

1
  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
Community
  • 1
  • 1
Riga
  • 2,099
  • 1
  • 19
  • 27
  • Got the expected behaviour. Sadly, the exception thrown doesn't contain the token (or list of tokens) which caused the exception. This makes the message shown to the user very vague. – anni Apr 29 '13 at 06:33
  • 1
    You could permit one (and multi-token if you like) positional and then print error containing unproper tokens manually if you get positionals in input. – Riga Apr 29 '13 at 15:12
  • Good idea. But, one would expect that the library would by default throw exception if positionals are not explicitly mentioned. In case, `allow_unregistered` is set, it should collect these tokens but if its not, then library should definitely throw exception. – anni May 02 '13 at 07:32