[program_options] How to use unknown program options?
Hi, I am trying to allow unknown options in the command line of my application (as described in "Allowing Unknown Options" section of program_options docs). Here is the code snippet: //----------------------------------------------------- boost::program_options::options_description my_options("Options"); my_options.add_options() /* adding some allowed options */ ; boost::program_options::variables_map vm; boost::program_options::store( boost::program_options::command_line_parser( ac, av ).options( my_options ).allow_unregistered().run(), vm ); boost::program_options::notify( vm ); //----------------------------------------------------- If an option is not specified in my_options, the call boost::program_options::store() above throws exception "unknown option". (I checked that it is store() that throws.) Does anyone have any suggestions how I could store and access the option values in this case? Of course, I can use basic_parsed_options returned by run(), but it seems to defeat the convenience of the library. Is there any way to use store() and variables_map in this case? Thank you for help. Best regards, Vsevolod Vlaskine __________________________________________ Yahoo! DSL Something to write home about. Just $16.99/mo. or less. dsl.yahoo.com
Hi Vsevolod,
If an option is not specified in my_options, the call boost::program_options::store() above throws exception "unknown option". (I checked that it is store() that throws.)
Does anyone have any suggestions how I could store and access the option values in this case? Of course, I can use basic_parsed_options returned by run(), but it seems to defeat the convenience of the library. Is there any way to use store() and variables_map in this case?
I'm afraid no, in 1.33.1. I was assuming that with unregistered option, user has to do some manual word with basic_parsed_options object anyway (like pass unregistered options to some other part of the program), so did not paid attention to "store". Now that you bring it up, it seems reasonable to ignore unregistered options in "store". A change and a testcase to that effect are just committed. If you don't want to use CVS version, you can just add if (options.options[i].unregistered) continue; to the loop in the store function. HTH, Volodya
participants (2)
-
Vladimir Prus
-
Vsevolod Vlaskin