[program_options] simple question

So far I see examples where the result of using program_options parsers is to store something into a variable map. Is it possible to directly store results into variables? For example, assume: int main (int argc, char** argv) { double value = 10.; mythical_parser (...); }; How do I use program_options to set "value"? Any examples? So far the only way I know is: po::options_description desc("Allowed options"); desc.add_options() ("help", "produce help message") ("value", po::value<int>()) ; po::variables_map vm; po::store(po::parse_command_line(argc, argv, desc), vm); po::notify(vm); if (vm.count("value")) value = vm["value"].as<int>();

OK, I found the answer to my (stupid) question --- I had to relearn how to use program_options. Which brings us to a IMHO really cool little example, combining lambda with program_options: ,----[ /home/nbecker/boost-program_options/Test1.cc ] | #include <boost/program_options.hpp> | #include <iostream> | #include <cxxabi.h> // demangle | #include <cmath> | | using namespace std; | namespace po = boost::program_options; | | #include <boost/lambda/lambda.hpp> | using namespace boost::lambda; | | int main (int argc, char** argv) { | double angle; | | std::set_terminate(__gnu_cxx::__verbose_terminate_handler); | po::options_description desc("Allowed options"); | desc.add_options() | ("help", "produce help message") | ("angle", po::value<double>()->notifier(var(angle)=180./M_PI*_1), "set | angle") ; | | po::variables_map vm; | | po::store(po::parse_command_line(argc, argv, desc), vm); | | | po::notify(vm); | | if (vm.count("help")) { | cout << desc << "\n"; | return 1; | } | | cout << "angle: " << angle << '\n'; | } `----

Neal Becker wrote:
Which brings us to a IMHO really cool little example, combining lambda with program_options:
| ("angle", po::value<double>()->notifier(var(angle)=180./M_PI*_1),
Yea, that's nice. I'll try to get it into docs. But, BTW, did you notice that for just storing into a variable, you can use ("angle", po::value<double>()->notifier(&angle)), ? I.e. for simple case there's no need to use lambda. Thanks, Volodya

Vladimir Prus wrote:
Neal Becker wrote:
Which brings us to a IMHO really cool little example, combining lambda with program_options:
| ("angle", po::value<double>()->notifier(var(angle)=180./M_PI*_1),
Yea, that's nice. I'll try to get it into docs.
But, BTW, did you notice that for just storing into a variable, you can use
("angle", po::value<double>()->notifier(&angle)),
? I.e. for simple case there's no need to use lambda.
Actually, just: ("angle", po::value(&angle)) Now I'm trying to figure out how to handle an enum. I can't really find a good solution so far.

Neal D. Becker wrote:
Actually, just: ("angle", po::value(&angle))
Yea, you right :-)
Now I'm trying to figure out how to handle an enum. I can't really find a good solution so far.
Well, you can specialize validator for the enum type and then use a map from enumerator name to value. Let me try to code it: map<string, my_enum> my_enum_values; bool demand_my_enum_values() { my_enum_values["first"] = first; ..... return true; } template<> void validator<my_enum>::operator()(any& v, const vector<string>& xs) { // Initialize table of the first use. static bool b = demand_my_enum_values(); check_first_occurence(v); string s(get_single_string(xs)); if (my_enum_values.count(s) == 0) throw ....; else v = any<my_enum>(my_enum_values); } Uhm... does not look overly nice. If we could steal Boost.Python syntax: enum_<color>("color") .value("red", red) .value("green", green) ; for describing enums and make validator use such descriptions, if you be great. But Boost.Python's enum_ heavily depends on Boost.Python, it seems. - Volodya

Vladimir Prus wrote:
Neal Becker wrote:
Which brings us to a IMHO really cool little example, combining lambda with program_options:
("angle", po::value<double>()->notifier(var(angle)=180./M_PI*_1),
Yea, that's nice. I'll try to get it into docs.
But, BTW, did you notice that for just storing into a variable, you can use
("angle", po::value<double>()->notifier(&angle)),
? I.e. for simple case there's no need to use lambda.
I'd expect ("angle", &angle), for the simple case.

Vladimir Prus wrote:
Which brings us to a IMHO really cool little example, combining lambda with program_options:
| ("angle", | po::value<double>()->notifier(var(angle)=180./M_PI*_1),
Yea, that's nice. I'll try to get it into docs.
But, BTW, did you notice that for just storing into a variable, you can use
("angle", po::value<double>()->notifier(&angle)),
? I.e. for simple case there's no need to use lambda.
Yet another good trick for the How to? Section or a related sample. Regards Hartmut BTW: attached just another patch I forgot to attach to my last message.

Hartmut Kaiser wrote:
But, BTW, did you notice that for just storing into a variable, you can use
("angle", po::value<double>()->notifier(&angle)),
? I.e. for simple case there's no need to use lambda.
Yet another good trick for the How to? Section or a related sample.
Actually: 1. I mistyped the code, so see Neal's correction. 2. It's described in tutorial, under "Option details" section.
Regards Hartmut
BTW: attached just another patch I forgot to attach to my last message.
I still don't get anything, could you send it to me personally? - Volodya P.S. I haven't missed your another email -- I just need to think a little about versioning in general.
participants (5)
-
hartmutkaiser@t-online.de
-
Neal Becker
-
Neal D. Becker
-
Peter Dimov
-
Vladimir Prus