I'm struggling to get boost program options to work properly. I need to be able to start my program from the terminal window (Linux) with an optional argument that takes a value. No matter what I did, this would not work; no matter what value I typed from the terminal, it just returned the default value. Furthermore, if I did not include the option in my termina command, it returned with
terminate called after throwing an instance of 'boost::exception_detail::clone_impl<boost::exception_detail::error_info_injec tor<std::logic_error> >'
what(): character conversion failed
Aborted (core dumped)
So I found a minimal example on the internet to see if it was something I had done wrong. Here is the example I found that does a similar thing that I need:
#include <iostream>
#include <boost/program_options.hpp>
namespace po = boost::program_options;
int main (int argc, char* argv[]) {
po::options_description desc("Usage");
desc.add_options()
("robots", po::value<int>()->default_value(3),
"How many robots do you want to send on a murderous rampage?");
po::variables_map opts;
po::store(po::parse_command_line(argc, argv, desc), opts);
try {
po::notify(opts);
}
catch (std::exception& e) {
std::cerr << "Error: " << e.what() << "\n";
return 1;
}
int nRobots = opts["robots"].as<int>();
// automatically assigns default when option not supplied by user!!
std::cout << nRobots << " robots have begun the silicon revolution"
<< std::endl;
return 0;
}
This does exactly the same thing however, and I am starting to think this is either a bug in Boost (unlikely I guess) or something about my system that it doesn't like?
Could anybody hint at what might be wrong please? Thanks