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?

4

1 回答 1

1
  1. 前面没有破折号的标记称为位置参数
  2. 您应该明确禁止预期行为的位置
  3. 为此,请制作空的位置列表并将其提供给解析器 https://stackoverflow.com/a/3859400/670719
于 2013-04-27T20:21:10.543 回答