- a token without preceding dashes is called a positional argument
- you should explicitly forbid positionals for the expected behaviour
- to do this make empty list of positionals and feed it to the parser https://stackoverflow.com/a/3859400/670719
Boost Program Options is silently ignoring unknown value tokens. why?
-
13-04-2022 - |
Pregunta
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?
Solución
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow