Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

boost::program_options: how to ignore unknown parameters?

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.

like image 736
Pietro Avatar asked Mar 21 '13 15:03

Pietro


1 Answers

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.

like image 70
evadeflow Avatar answered Nov 03 '22 10:11

evadeflow