Get a string from the command line when using boost program_options
-
21-07-2021 - |
Pergunta
I am using the boost program_options library to process command line and configuration file data, but it is not clear to me how to get the required string from the processed data.
How do I get the program_options command line parameters into the correct form for the getaddrinfo function.
//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
The prototype from netdb for getaddrinfo is:
extern int getaddrinfo (__const char *__restrict __name,
__const char *__restrict __service,
__const struct addrinfo *__restrict __req,
struct addrinfo **__restrict __pai);
Here the link to boost program_options API:
http://www.boost.org/doc/libs/1_37_0/doc/html/program_options.html
Solução
If the needed argument should have the type char *
and you try to supply a string
, the compiler will tell you what's wrong. Read that error message and try to understand it.
To get a char *
from a string
, you use the method c_str()
.
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow