boost::program_options - 它是否对命令行选项进行精确的字符串匹配?

Onk*_*nde 5 c++ windows boost command-line-arguments boost-program-options

boost::program_options 的 options_description 匹配的完成方式似乎存在问题。

int main(int argc, char* argv[])
{
    boost::program_options::options_description desc("CmdLine utility");
    desc.add_options()
        ("hel", "hel message")
        ("help", "produce help message")
        ("helps","helps message")       
    ;
    boost::program_options::variables_map vm;
    boost::program_options::store(boost::program_options::parse_command_line(argc, argv,desc), vm);
    boost::program_options::notify(vm);

    if(vm.count("help")) {
        std::cout << desc << std::endl;
    }
    if(vm.count("helps")) {
        std::cout << "helps..." << std::endl;
    }
    if(vm.count("hel")) {
        std::cout << "hel..." << std::endl;
    }
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

输出 -

C:\code>cmd.exe --helps
helps...
C:\code>cmd.exe --help
helps...
C:\code>cmd.exe --hel
helps...
Run Code Online (Sandbox Code Playgroud)

如果我更改使用 call 添加选项的顺序,输出会发生变化add_options()。另外,program_options 似乎没有进行完整的命令字符串匹配,因此即使您输入选项的子字符串,它也会将其视为有效选项,而不进行完整的字符串比较。如果这是 boost::program_options 功能,是否有任何方法可以强制它进行精确的字符串匹配,而不是使用子字符串匹配?(我使用的是Boost版本1.42)

Vla*_*rus 4

默认情况下,program_option 具有allow_guessing 样式位,因此子字符串匹配就足够了。您观察到的行为(其中选项与命令行前缀匹配,即使存在完全匹配的不同选项)也是一个错误。已固定为 1.45。