Program Options not saving the values
Dear all, I am having a problem with the Program Options lib. I am using Archlinux and boost-libs 1.55.0-6. Consider the simple code: #include "boost/program_options.hpp" #include <iostream> #include <string> int main(int argc, char** argv) { int add(5); namespace po = boost::program_options; po::options_description desc("Options"); desc.add_options() ("help", "Print help messages") ("add",po::value<int>(&add)->required(), "additional options") ("like", "this"); po::variables_map vm; po::store(po::parse_command_line(argc, argv, desc), vm); if ( vm.count("help") ) { std::cout << "Basic Command Line Parameter App" << std::endl << desc << std::endl; return 1; } std::cout << "var: " << add << std::endl; std::cout << "vm: " << vm["add"].as<int>() << std::endl; po::notify(vm); return 1; } Compiling and running: $ g++ program_options.cpp -lboost_program_options $ ./a.out --add 4 var: 5 vm: 4 Why the value of the variable "add" is not receiving 4? Thanks for any help.
On 07/02/2014 02:02 AM, Angelo Mondaini wrote: Hi Angelo,
std::cout << "var: " << add << std::endl; std::cout << "vm: " << vm["add"].as<int>() << std::endl; po::notify(vm);
return 1; }
Compiling and running: $ g++ program_options.cpp -lboost_program_options $ ./a.out --add 4 var: 5 vm: 4
Why the value of the variable "add" is not receiving 4?
Because 'add' is only assigned when you call po::notify, and your program prints the value of 'add' before calling po::notify - Volodya
Indeed, thanks!
2014-07-02 4:13 GMT-03:00 Vladimir Prus
On 07/02/2014 02:02 AM, Angelo Mondaini wrote:
Hi Angelo,
std::cout << "var: " << add << std::endl;
std::cout << "vm: " << vm["add"].as<int>() << std::endl; po::notify(vm);
return 1; }
Compiling and running: $ g++ program_options.cpp -lboost_program_options $ ./a.out --add 4 var: 5 vm: 4
Why the value of the variable "add" is not receiving 4?
Because 'add' is only assigned when you call po::notify, and your program prints the value of 'add' before calling po::notify
- Volodya
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
participants (2)
-
Angelo Mondaini
-
Vladimir Prus