program_options: Printing options and their values

I am using program_options and want to write to the screen the options and their values. This allows me to verify everything is working properly before I execute a long running command. I thought I could just iterate over the map and print out each option, but that requires casting the option to a specific variable which must be known a priori. This is what I have right now cout << "options:\n"; po::variables_map::iterator optIter = options.begin(); for( ; optIter != options.end(); ++optIter ){ cout << "\t" << optIter->first << " = " << optIter->second.as<string>() << std::endl; } When my option isn't a string (e.g. bool or int) then I get the error: terminate called after throwing an instance of 'boost::bad_any_cast' what(): boost::bad_any_cast: failed conversion using boost::any_cast Can someone show me how to do this? Thanks, Jeremy

Jeremy Conlin wrote:
This is what I have right now
cout << "options:\n"; po::variables_map::iterator optIter = options.begin(); for( ; optIter != options.end(); ++optIter ){ cout << "\t" << optIter->first << " = " << optIter->second.as http://second.as<string>() << std::endl; }
I have yet to find a really simple + satisfactory way to do this. I tried writing a wrapper that stored a string conversion function with the option when it was declared... but that was bit of a hack and I lost all the generality. I concluded it's best to try and print them one by one, not in a loop, but by name, and do the printing near where the options are added so you can easily see their names and types.
------------------------------------------------------------------------
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users

On Wed, Mar 12, 2008 at 8:19 PM, Andrew Schweitzer < a.schweitzer.grps@gmail.com> wrote:
Jeremy Conlin wrote:
This is what I have right now
cout << "options:\n"; po::variables_map::iterator optIter = options.begin(); for( ; optIter != options.end(); ++optIter ){ cout << "\t" << optIter->first << " = " << optIter->second.as http://second.as<string>() << std::endl; }
I have yet to find a really simple + satisfactory way to do this.
I tried writing a wrapper that stored a string conversion function with the option when it was declared... but that was bit of a hack and I lost all the generality.
I concluded it's best to try and print them one by one, not in a loop, but by name, and do the printing near where the options are added so you can easily see their names and types.
Thank you for your response. I have come to the same conclusion you did. Until there is a better solution, I'll have to do this manually. Thanks, Jeremy
participants (2)
-
Andrew Schweitzer
-
Jeremy Conlin