2013-07-07 23 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