I'm using boost::program_options to get parameters from a config file.
i understand that i can create a file by hand and program options will parse it. but i'm looking for a way for the program to generate the file automatically. meaning printing out the name of the option and it's value. for example:
>./main
without option would generate init.cfg that looks like this
[wave packet]
width = 1
position = 2.0
[calculation parameters]
levels = 15
then i would go into that file change the values using text editor and use this file:
>./main init.cfg
a nice way to approach this would be to have variables_map to have operator<<
. this way i can just write it to file. change the values. read the file. all in the same format and no need to write each line.
i couldn't find anything like that in documentation or examples. please let me know if this is possible
EDIT: Sam Miller showed how to parse the ini file in sections. However, I still have a problem getting the values from boost::program_options::variables_map vm. i tried the following
for(po::variables_map::iterator it = vm.begin(); it != vm.end(); ++it)
{
if(it->first!="help"&&it->first!="config")
cout << "first - " << it->first << ", second - " << it->second.value() << "\n";
}
instead of it->second.value()
, got an error. i also tried it->second
. i also got an error:
icpc -lboost_serialization -lboost_program_options -c programOptions.cc
programOptions.cc(60): error: no operator "<<" matches these operands
operand types are: std::basic_ostream<char, std::char_traits<char>> << boost::any
cout << "first - " << it->first << ", second - " << it->second.value() << "\n";
^
compilation aborted for programOptions.cc (code 2)
make: *** [programOptions.o] Error 2
i get the value correctly if i use it->second.as<int>()
but not all of my values are ints and once i reach double, the program crashes with this:
terminate called after throwing an instance of 'boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::bad_any_cast> >'
what(): boost::bad_any_cast: failed conversion using boost::any_cast