Boost :: program_options: how to ignore unknown parameters?

In the boost::program_options library, I cannot figure out how to allow the user to pass a parameter that was not added via add_options() .
I would like this to be simply ignored, instead of terminating the program.

+4
source share
2 answers

Today I ran into the same problem. @TAS's answer set me on the right track, but it still took 20 minutes to miss a finger to figure out the exact syntax for my particular use case.

To ignore unknown parameters, instead:

 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); 

Please note that only the middle row is different.

In a nutshell, use commandline_parser() rather than parse_commandline() , with some "dangly bits" (ie .options(desc).allow_unregistered().run() ) attached after the call.

+2
source

From the documentation of boost :: program_options How to resolve unknown parameters

 parsed_options parsed = command_line_parser(argc, argv).options(desc).allow_unregistered().run(); 
+7
source

All Articles