I'm writing a program using Boost's program_options library. Now, I want to allow it to extend with arbitrary code, which the configuration parser is not aware of - but which would still get some specific options passed to it.
My idea was to somehow pass it a key-value map, possibly even a program_options::variable_map . The thing is, program_options needs to know which options to expect in advance, I can't directly a map with the keys I like.
So, I was thinking maybe I could get program_options to accept arbitrary key-value pairs with string keys (if necessary, string values), put those in some map from string to either string or std::experimental::any, and pass that onwards.
To be more concrete, I'll give an example (although it doesn't have to be quite like this exactly). I would write:
$ magic_app --key1 val1 --key2 val2 --key3 val3 positional1 positional2
and suppose program_options knows about key2
but not about key1
or key3
. Then it will...
- parse
key2
, - create a map containins two keys,
key1
andkey3
, with valuesval1
andval2
respectively (say string values), and - parse the positional arguments
positional1
andpositional2
.
So that the app can pass on the map (perhaps after some transformation) to another component with its own argument processor.
Is this doable? Is there a simpler/better alternative I could use with boost::program_options
?
Notes:
- Somewhat related to this question, which could serve as a crude implementation with some post-parsing - keys will be odd elements in the list, values will be the even elements.
- The user must be able to specify the options unknown to
program_options
just like all other options.