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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With