//Using boost program options to read command line and config file data
#include <boost/program_options.hpp>
using namespace std;
using namespace boost;
namespace po = boost::program_options;
int main (int argc, char *argv[])
{
po::options_description config("Configuration");
config.add_options()
("IPAddress,i","IP Address")
("Port,p","Port")
;
po::variables_map vm;
po::store(po::parse_command_line(argc, argv, config),vm);
po::notify(vm);
cout << "Values\n";
string address = (vm["IPAddress"].as<std::string >()).c_str();
string port = (vm["Port"].as<std::string>()).c_str();
cout << (vm["IPAddress"].as< string >()).c_str();
cout << " " << (vm["Port"].as<string>()).c_str();
return 0;
}
Are the inputted values somehow unprintable?
Here is gdb output, seems to be be cast problem:
terminate called after throwing an instance of 'boost::exception_detail::clone_impl
' what(): boost::bad_any_cast: failed conversion using boost::any_cast
Program received signal SIGABRT, Aborted. 0x0000003afd835935 in raise () from /lib64/libc.so.6
string address = (vm["IPAddress"].as<std::string >()).c_str();
is where the error occurs; I have tried std::string and string with the same results.
testboostpo -i 192.168.1.10 -p 5000
is the command line.
I tried declaring the types, like so:
config.add_options()
("IPAddress,i", po::value<std::string>(), "IP Address")
("Port,p", po::value<std::string>(), "Port");
but the error still occurred.
Could this be a genuine bug?
std::string::c_str()
to astd::string
– Goner