2013-03-20 35 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

相關問題