我有两个开关,'i'和'p'分别代表IPAddress和Port。
命令行的格式是什么?
我试过了:
app -i192.168.1.1 -p12345
app -i 192.168.1.1 -p 12345
app -i=192.168.1.1 -p=12345
app -i='192.168.1.1' -p='12345'
app --IPAddress 192.168.1.1 --Port12345
我的应用程序出现了IPAddress的问题,而DDD的故障排除方法却没有发现,因为我找到了vm。
此外,应用程序作为守护程序运行,因此我的IP地址和端口的cout语句将被遗忘,并且输出值不是const char *的事实阻碍了对syslog的打印。 / p>
我计划将程序选项用于其他事情,但我对此有点了解。
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(ac, av, config),
vm);
po::notify(vm);
//...and this is how the values are used
int retval = getaddrinfo((vm["IPAddress"].as< string >()).c_str(),(vm["Port"].as<string>()).c_str(), &hint, &list);
这是一个完整的程序......在'值'之后没有任何内容打印到控制台:
#include <sstream>
#include <algorithm>
#include <stdlib.h>
#include <iterator>
#include <string>
//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";
cout << (vm["IPAddress"].as< string >()).c_str();
cout << " " << (vm["Port"].as<string>()).c_str();
return 0;
}
输入的值是否以某种方式无法打印?
这是gdb输出,似乎是演员问题:
28 string address = (vm["IPAddress"].as< string >()).c_str();
(gdb) n
terminate called after throwing an instance of 'boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::bad_any_cast> >'
what(): boost::bad_any_cast: failed conversion using boost::any_cast
Program received signal SIGABRT, Aborted.
0x0000003afd835935 in raise () from /lib64/libc.so.6
答案 0 :(得分:1)
BOOST程序选项支持Unix系统中已知的常见命令行风格。 因此,这两个应该工作(他们为我工作)
app -i 192.168.1.1 -p 12345
app --IPAddress=192.168.1.1 --Port=12345
说明: