I want to store a command line value into a variable. Here's my complete code:
#include <iostream>
#include <boost/program_options.hpp>
int main(int argc, char *argv[]) {
int nselect = 100;
boost::program_options::options_description desc("Allowed options");
desc.add_options()
("help,h", "print usage message")
("nselect,N", boost::program_options::value<int>(&nselect), "number to select");
boost::program_options::variables_map vm;
boost::program_options::store(boost::program_options::parse_command_line(argc, argv, desc), vm);
if (vm.count("help")) {
std::cout << desc << "\n";
return 0;
}
std::cout<<"nselect = "<<nselect<<"\n";
return(0);
}
I compile it as g++ a.cpp -lboost_program_options
, and then run:
$ ./a.out -N 5
nselect = 100
Why isn't it storing the command line value?
Resolution:
Calling notify(vm)
automatically stores the value into the variable specified in the variable. Or one can map it from vm, as in the answer by @Matthieu-Brucher below.