In the boost::program_options
library, I cannot understand how to allow the user to pass a parameter which has not been added through add_options()
.
I would like it to be just ignored, instead of terminating the program.
Asked
Active
Viewed 3,626 times
11

Pietro
- 12,086
- 26
- 100
- 193
2 Answers
12
I ran into this exact same problem tonight. @TAS's answer put me on the right path, but it still took 20 minutes of finger-mumbling to figure out the exact syntax for my particular use case.
To ignore unknown options, instead of writing this:
po::variables_map vm;
po::store(po::parse_command_line(argc, argv, desc), vm);
po::notify(vm);
I wrote this:
po::variables_map vm;
po::store(po::command_line_parser(argc, argv).options(desc).allow_unregistered().run(), vm);
po::notify(vm);
Note that only the middle line is different.
In a nutshell, use commandline_parser()
rather than parse_commandline()
, with some 'dangly bits' (i.e., .options(desc).allow_unregistered().run()
) tacked on after the invocation.

evadeflow
- 4,704
- 38
- 51
10
From the boost::program_options documentation How To: Allowing Unknown Options
parsed_options parsed =
command_line_parser(argc, argv).options(desc).allow_unregistered().run();

TAS
- 2,039
- 12
- 17