2013-03-20 32 views
0

我有两个开关,分别代表IPAddress和Port的'i'和'p'。BOOST程序选项命令行的格式是什么?

命令行的格式是什么?

我曾尝试:

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 

我的应用程序具有与该ip地址的问题,并与DDD故障排除unrevealing我得到的虚拟机。

此外,该应用程序作为守护进程运行,所以我的IP地址和端口的cout语句将被遗忘,并且打印到系统日志会受到输出值不是const char *的事实的阻碍。

我打算为其他事情使用程序选项,但我对此有点头痛。

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 
+3

而不是直接在您的后台应用程序的工作,你为什么不建立一个独立的测试用例为此,以了解如何使用它隔离? – 2013-03-20 01:26:34

+0

我曾想到这一点。我认为这是一个很好的方法,因为当前的设置有很多并发症测试。 – bentaisan 2013-03-20 02:15:14

+1

张贴一些代码请 – 2013-03-20 03:27:07

回答

1

BOOST程序选项支持从Unix系统中已知的常见命令行口味。 因此,这两个应该工作(他们的工作对我来说)

app -i 192.168.1.1 -p 12345 
app --IPAddress=192.168.1.1 --Port=12345 

备注:

  • 基本教程文档at boost.org(可能你已经知道了)
  • 写一个独立的单元测试因为这当然是一个好建议;升压还提供了一个简单易用的测试框架的C++
+0

你能看看gdb输出并给我一个提示吗?我现在在网上搜索,但这里有人可能会有一个快速解决方案。看起来像他们可能是一个rtti不匹配。难道是因为我正在编译一个64位目标? – bentaisan 2013-03-20 19:57:15

+0

奇怪......你确定它实际上是这样一个参数吗? vm.count(“IPAddress”)会产生什么结果?它应该是1(参数的一个实例)。如果它为零,那么不存在的结果确实不能被转换为字符串 – Ichthyo 2013-06-03 02:28:13

相关问题