I have the following code in testpo.cpp:
#include <iostream>
#include <boost/program_options.hpp>
#include <fstream>
int main(int argc, char* argv[]) {
namespace po = boost::program_options;
po::options_description config_descriptor;
po::variables_map vm;
config_descriptor.add_options()
("var1", po::value<int>()->required(), "var1");
//po::store(po::parse_config_file(input_config_file, config_descriptor, false), vm);
po::store(po::parse_command_line(argc, argv, config_descriptor), vm);
po::notify(vm);
try {
std::cout << vm.count("var1") << std::endl;
std::cout << "Count printed" << std::endl;
if (vm.count("var1") > 0) {
std::cout << "Imhere\n";
std::cout << "var1: " << vm["var1"].as<int>() << std::endl;
} else {
std::cout << "Missing var1 in config file" << std::endl;
}
} catch (std::exception& e) {
std::cout << "Exception: " << e.what() << std::endl;
}
}
I compile using the following command:
g++ -std=c++11 testpo.cpp -lboost_program_options
And I run it like this:
./a.out --var1 10
I get the following output:
1
Count printed
Imhere
Exception: boost::bad_any_cast: failed conversion using boost::any_cast
I have already looked at this but it doen't seem to help. I can't find what I'm doing wrong.
My system's relevant configuration:
g++ (Ubuntu 5.2.1-22ubuntu2) 5.2.1 20151010
Boost version: 1.58
When I run it on another system it works, configuration:
g++ (GCC) 4.8.3 20140911 (Red Hat 4.8.3-9)
Boost version: 1.53
Is it a bug in boost?