我正在使用boost program_options库来处理命令行和配置文件数据,但我不清楚如何从处理过的数据中获取所需的字符串。
如何将get_drinfo函数的program_options命令行参数转换为正确的格式。
//First, the options are declared
po::options_description config("Configuration");
config.add_options()
("IPAddress", po::value< vector<string> >(),"127.0.0.1")
("Port", po::value< vector<string> >(),"5000")
;
//...
// Attach the config descriptions to the command line and/or config file
po::options_description cmdline_options;
cmdline_options.add(config).add(hidden);
po::options_description config_file_options;
config_file_options.add(hidden);
po::options_description visible("Allowed options");
visible.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);
notify(vm);
// Use the command line options for IPAddress and Port
// TODO: Load the config file's address and port information
int retval = getaddrinfo(vm["IPAdress"].as< string >(),vm["Port"].as<string>(), &hint, &list);
// This doesn't work and neither does this
// int retval = getaddrinfo(vm["IPAdress"].as< vector<string> >(),vm["Port"].as<vector <string> >(), &hint, &list);
// getaddressinfo
netdb for getaddrinfo的原型是:
extern int getaddrinfo (__const char *__restrict __name,
__const char *__restrict __service,
__const struct addrinfo *__restrict __req,
struct addrinfo **__restrict __pai);
此处提供program_options API的链接:
http://www.boost.org/doc/libs/1_37_0/doc/html/program_options.html
答案 0 :(得分:3)
如果所需的参数应该具有类型char *
并且您尝试提供string
,则编译器将告诉您哪里出错了。阅读该错误消息并尝试理解它。
要从char *
获取string
,请使用c_str()
方法。