Boost::program_optiions - Defining The Config File As A Cmdline Parameter

Hi All, I want to be able to do the following using the Boost::program_options library. 1) Parse a set of command line options in a standard way (I know how to achieve this). 2) Have an command line option which is "get other command line options from a config file defined as the argument to this option. The example multiple_sources is very close to the sort of thing I want, however in that example the config file is hard coded. I want the user to interactively set the config file at runtime. If I try simply putting the following lines in the set of if statements my program crashes (I know this is still hard coding the cfg file, but it was a first attempt before I made it more general), if ((vm.count("input-file")) { ifstream ifs("multiple_sources.cfg"); store(parse_config_file(ifs, config_file_options), vm); notify(vm); } Any help would be much appreciated, Adam

Adam Hartshorne wrote:
Hi All,
I want to be able to do the following using the Boost::program_options library.
1) Parse a set of command line options in a standard way (I know how to achieve this).
2) Have an command line option which is "get other command line options from a config file defined as the argument to this option.
The example multiple_sources is very close to the sort of thing I want, however in that example the config file is hard coded. I want the user to interactively set the config file at runtime.
If I try simply putting the following lines in the set of if statements my program crashes (I know this is still hard coding the cfg file, but it was a first attempt before I made it more general),
if ((vm.count("input-file")) { ifstream ifs("multiple_sources.cfg"); store(parse_config_file(ifs, config_file_options), vm); notify(vm); }
This should work. What is the crash, specifically. Are you sure you're not calling 'notify' twice? Do you have a a try/catch block to catch any possible errors? - Volodya

Here the is full code example I have adapted from the aforementioned given example. The error message thrown is Unhandled exception at 0x7c81eb33 in Test.exe: Microsoft C++ exception: boost::bad_any_cast @ 0x0013f63c. If I comment out the two lines, ifstream ifs("multiple_sources.cfg"); store(parse_config_file(ifs, config_file_options), vm); everything works fine. #include <boost/program_options.hpp> namespace po = boost::program_options; #include <fstream> #include <iostream> #include <iterator> using namespace std; int main(int ac, char* av[]) { int compLevel = 0 ; string inputFileName ; int opt; // Declare a group of options that will be // allowed only on command line po::options_description generic("Generic options"); generic.add_options() ("version,v", "print version string") ("help", "produce help message") ; // Declare a group of options that will be // allowed both on command line and in // config file po::options_description config("Configuration"); config.add_options() ("input-file", po::value<string>(), "input file") ("optimization", po::value<int>(&opt)->default_value(10), "optimization level") ("include-path,I", po::value< vector<string> >()->composing(), "include path") ; // Hidden options, will be allowed both on command line and // in config file, but will not be shown to the user. //po::options_description hidden("Hidden options"); //hidden.add_options() // ("input-file", po::value<string>(), "input file") // ; po::options_description cmdline_options; cmdline_options.add(generic).add(config); po::options_description config_file_options; config_file_options.add(config); po::options_description visible("Allowed options"); visible.add(generic).add(config); po::positional_options_description p; p.add("input-file", -1); po::variables_map vm; store(po::command_line_parser(ac, av). options(cmdline_options).positional(p).run(), vm); if (vm.count("help")) { cout << visible << "\n"; return 0; } if (vm.count("input-file")) { cout << "Input file is: " << vm["input-file"].as<string>() << "\n"; ifstream ifs("multiple_sources.cfg"); store(parse_config_file(ifs, config_file_options), vm); //notify(vm); } if (vm.count("include-path")) { cout << "Include paths are: " << vm["include-path"].as<string>() << "\n"; } cout << "Optimization level is " << opt << "\n"; return 0; }

Have successfully got it up and running now, Adam
participants (3)
-
Adam Hartshorne
-
Adam Teasdale Hartshorne
-
Vladimir Prus