提升程序选项 - 解析命令行时崩溃

Pup*_*ppy 4 c++ boost boost-program-options

我有以下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
Run Code Online (Sandbox Code Playgroud)

但是,当我--mingw="stuff"在命令行中指定时,我发现它被拒绝了.发出--help命令后,似乎只有列表中选项的第一个选项实际注册opts- 即使以这种方式链接它是教程推荐的内容.

这个简单的示例程序出了什么问题?它基本上直接来自教程.

Joe*_*e Z 12

看一下教程,我看不到选项之间的逗号.即:

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

尝试删除每个选项末尾的逗号.

  • 公平地说,提升确实推动了合理语法的极限. (4认同)