【发布时间】:2013-03-20 01:25:00
【问题描述】:
我有两个开关,'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。
我也计划将程序选项用于其他事情,但我对此有点不知所措。
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);
这是一个完整的程序...在“Values”之后没有任何内容打印到控制台:
#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
【问题讨论】:
-
与其直接在您的守护程序应用程序上工作,为什么不为此构建一个独立的测试用例,以便了解如何单独使用它?
-
我已经想到了。我认为这是一个很好的方法,因为当前设置有很多复杂的测试。
-
请贴一些代码
-
@bentaisan:听起来你的调试选项非常有限,所以这可能不是一个好方法......
标签: c++ boost options boost-program-options