我正在使用 boost program_options 库来处理命令行和配置文件数据,但我不清楚如何从处理的数据中获取所需的字符串。
如何将 program_options 命令行参数转换为 getaddrinfo 函数的正确形式。
//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 的 getaddrinfo 原型是:
extern int getaddrinfo (__const char *__restrict __name,
__const char *__restrict __service,
__const struct addrinfo *__restrict __req,
struct addrinfo **__restrict __pai);
这里是 boost program_options API 的链接:
http://www.boost.org/doc/libs/1_37_0/doc/html/program_options.html