22

I recently discovered an undocumented feature of boost::program_options, namely that it accepts "*" as a special wildcard that allows declaration of a group of options with the same prefix, like this:

configOptions.add_options()
    ("item_*", value<int>(), "items");

This declaration worked as expected and recognized item_1, item_3, etc, while rejecting unknown options. Well now the question is, how can I write a custom validate function that will populate a map with options key and its value, like this:

map<string, int> itemsMap;
options_description items("items options");
    items.add_options()
        ("item_*",value<map<string, int>>(&itemsMap)->multitoken(), "items")
    ;

My question is - how do I get the key of the option being validated from within validate() function?

template <typename T> void validate(boost::any& v, const std::vector<std::string>& values, map<string, T> *, int)
user1876484
  • 610
  • 6
  • 16
  • 1
    which version of boost are you using? I recall [a proposed patch](http://lists.boost.org/Archives/boost/2010/12/173888.php) on the mailing list, but do not see it ever discussed on [trac](https://svn.boost.org/trac/boost/query?status=assigned&status=closed&status=new&status=reopened&component=program_options&col=id&col=summary&col=status&col=owner&col=type&col=milestone&order=priority). – Sam Miller Jan 18 '13 at 21:25
  • Version 1.52.0. Check the files: options_description.cpp Line 87 and config_file.cpp Line 43 . As far as I understand, the guy also knew about this feature back in 2010. – user1876484 Jan 19 '13 at 18:56
  • Opened a [ticket on trac](https://svn.boost.org/trac/boost/ticket/7933). – user1876484 Jan 27 '13 at 16:43
  • 4
    check the accepted answer here : http://stackoverflow.com/questions/15554842/boostprogram-options-parameters-with-a-fixed-and-a-variable-token – Sander De Dycker May 23 '13 at 12:49

1 Answers1

1

You need to iterate through all arguments identifying which has the correct prefix or write a custom parser. The instructions for both options are in the correct answer of the link below:

boost::program_options: parameters with a fixed and a variable token?

The iteration option might seem easier to understand (implement and read) but the custom parser seems good too (though I never used it).

Community
  • 1
  • 1