[Home]Optional Option Present Parameter To Add Options - Program Options Suggestion

BOOST WIKI | RecentChanges | Preferences | Page List | Links List

Optional "option present" parameter to add_options()

Back to Suggestions - program options library


It would be useful to be able to pass an optional boolean to add_options(), which is set in the event that the corresponding option is seen. For example:

    bool isMagicSeen;

    desc.add_options()
        ...
        ("magic", parameter("value", &magic), "magic value for the program",
                                                                &isMagicSeen)
        ...
        ;
    
    ....

    if (isMagicSeen)
        do stuff;

You can currently do this another way, e.g.:

    variables_map vm;
    store(oa, vm, desc);


    if (vm.count("magic")) {
        ...
    }

However, this is prone to error -- for example, what if you change the name of the option (something which happens all the time)? If you forget to change it in both places, you're hosed.

Also, the proposed method would be less verbose.

    - People/Chuck Messenger


Could you provide a use case for it? I assume that you mostly have boolean options, which can be associated with bool variables via "parameter", and all the other options, for which testing if it's set or not is not necessary. For example, if no include paths are provided, you don't use any.

    - People/Vladimir Prus


No - I'm talking about detecting whether an option has been specified. For example,

    $ grep -f file

tells you not only that you're taking patterns from a file, but also, it gives you the name of the file. This sort of thing is quite common for me -- looking through my existing programs. I have a built-in capability for this, which I use alot. I found the program_options version to be pretty clunky (using the method I showed above).

    - People/Chuck Messenger


Thanks for clarifying it! I think this is reasonable desire. I'm only not sure if it isn't better to have boolean flag as third parameter to 'parameter' function.

   - People/Vladimir Prus



BOOST WIKI | RecentChanges | Preferences | Page List | Links List
Edit text of this page | View other revisions
Last edited May 22, 2003 7:22 am (diff)
Search:
Disclaimer: This site not officially maintained by Boost Developers