I'd like to use boost program options in a program I'm writing. This program also uses CORBA, which is designed to accept CORBA-specific command line inputs. I'd like to make boost program options ignore these CORBA-related command line inputs and only process the others. How can I accomplish this?
As an example, given the following command line:
./myprogram -ORBInitRef NameService=corbaloc:iiop:localhost:1234/NameService --myBoostOptionFoo 1 --myBoostOptionBar trololo
How can I make boost program options only process myBoostOptionFoo and myBoostOptionBar?
All CORBA command line inputs begin with "-ORBxxxxxx", so that should help with the filtering, but I'm completely lost as to whether or not there's an easy way to accomplish this, since boost::PO will complain about command line options it doesn't understand.
Straight from the documentation on Allowing Unknown Options:
Usually, the library throws an exception on unknown option names. This behavior can be changed. For example, only some part of your application uses Program_options, and you wish to pass unrecognized options to another part of the program, or even to another application.
To allow unregistered options on the command line, you need to use the
basic_command_line_parser
class for parsing (notparse_command_line
) and call theallow_unregistered method
of that class:
parsed_options parsed = command_line_parser(argc, argv).options(desc).allow_unregistered().run();
For each token that looks like an option, but does not have a known name, an instance of basic_option will be added to the result. The string_key and value fields of the instance will contain results of syntactic parsing of the token, the unregistered field will be set to true, and the original_tokens field will contain the token as it appeared on the command line.
If you want to pass the unrecognized options further, the
collect_unrecognized
function can be used. The function will collect original tokens for all unrecognized values, and optionally, all found positional options. Say, if your code handles a few options, but does not handles positional options at all, you can use the function like this:
vector<string> to_pass_further = collect_unrecognized(parsed.options,
include_positional);