5

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;
}

输出 -

C:\code>cmd.exe --helps
helps...
C:\code>cmd.exe --help
helps...
C:\code>cmd.exe --hel
helps...

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

4

2 回答 2

4

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

于 2010-11-22T11:33:32.127 回答
0

也许你打错了。你的例子很好。看看我得到的输出:

[vladimir@asa example]$ ./a.out --help
CmdLine utility:
  --hel                 hel message
  --help                produce help message
  --helps               helps message

[vladimir@asa example]$ ./a.out --hel
hel...
[vladimir@asa example]$ ./a.out --helps
helps...
于 2010-11-16T18:20:40.127 回答