So I have just started adding option support to my code. I can either do it myself or use boost's program options. The only thing that holds me back from using boost's is yet another dependency that I'm adding to the project. However, if it can handle parsing complex objects with ease, I'm more willing to pay the price.
I just tried something like this based on the examples and it does not work:
#include <boost/program_options.hpp>
namespace po = boost::program_options;
#include <iostream>
#include <fstream>
#include <iterator>
using namespace std;
struct Grid{
double xmin, xmax;
};
int main(int ac, char* av[])
{
try {
Grid g;
po::options_description cmd("Allowed options");
cmd.add_options()
("help", "produce help message")
("Grid", "grid information")
;
po::variables_map vm;
store(parse_command_line(ac, av, cmd), vm);
notify(vm);
if (vm.count("help")) {
cout << cmd << "\n";
return 0;
}
g = vm["Grid"].as<Grid>();
cout << g.xmin << " " << g.xmax << endl;
}
catch(exception& e)
{
cout << e.what() << "\n";
return 1;
}
return 0;
When I run the code with ./a.out --Grid {-1, 1}
, I get boost::bad_any_cast: failed conversion using boost::any_cast
. I do not understand what that means but my guess is I cannot properly tell boost of my object type Grid
. How can I do this correctly with boost?