2013-07-07 24 views
4

我有以下boost :: program_options程序。升壓程序選項 - 解析命令行崩潰

boost::program_options::options_description opts("Allowed options"); 
opts.add_options() 
    ("help", "produce help message"), 
    ("mingw", boost::program_options::value<std::string>(), "Set the install path for MinGW"), 
    ("triple", boost::program_options::value<std::string>(), "Set the target triple"), 
    ("output", boost::program_options::value<std::string>(), "Set the output file"), 
    ("input", boost::program_options::value<std::vector<std::string>>(), "Set an input file."), 
    ("include", boost::program_options::value<std::vector<std::string>>(), "Set an include path.") 
; 

boost::program_options::positional_options_description posopts; 
posopts.add("input", -1); 

boost::program_options::variables_map vm; 
try { 
    boost::program_options::store(boost::program_options::command_line_parser(argc, argv).options(opts).positional(posopts).run(), vm); 
} catch(std::exception& e) { 
    std::cout << e.what(); 
    std::cin.get(); 
} 
boost::program_options::notify(vm); 

if (vm.find("help") != vm.end()) { 
    std::cout << opts << "\n"; 
    std::cin.get(); 
    return 1; 
} 
// Actual program logic 

然而,當我在命令行上指定--mingw="stuff",我發現它被拒絕。在發出--help命令之後,似乎只有列表中選項的第一個選項實際上是用opts註冊的 - 儘管以這種方式鏈接它是本教程推薦的內容。

這個簡單的示例程序出了什麼問題?它基本上來自教程。

回答

12

看看本教程,我看不到選項之間的逗號。即:

desc.add_options() 
    ("help", "produce help message") // no comma here! 
    ("compression", po::value<int>(), "set compression level") 
; 

嘗試刪除每個選項結尾處的逗號。

+0

哦,雞姦。那真是愚蠢。 – Puppy

+4

公平地說,提升確實會推動合理語法的極限。 –

+0

@JoeZ通常它也超過它們:P – 2013-07-07 19:04:37

0

我在開放SUSE Leap 42.2時遇到了與boost_1_63相同的問題。重新編譯所有提升庫與./b2 .... --build-type =完成並重新安裝該問題不再顯示。希望這會對你至少有一些幫助。

+0

使用--build-type = complete編譯boost庫會生成boost libs的所有版本(多線程,調試等)。我想,當所有版本的program_options lib安裝,最適合的版本用於鏈接,因此program_options不會在解析命令行時崩潰。可能沒有必要重新編譯所有的升級庫,但我現在沒有時間去探究崩潰的確切原因。 – zsoltoery