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

发布于 2024-10-02 09:10:15 字数 1177 浏览 1 评论 0原文

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)

There seems to be a problem the way boost::program_options's options_description matching is done.

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

Output -

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

The output changes if I change the order in which options are added using add_options() call. Also it looks like program_options does not do a complete command string matching, so even if you enter a substring of the option, it will consider it as a valid option without doing a complete string comparison. If this is a boost::program_options feature, is there any way to force it to do exact string matching rather than doing it with substring matching ? (I am using Boost version 1.42)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

哑剧 2024-10-09 09:10:15

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

By default, program_option has allow_guessing style bit on, so a substring match is sufficient. The behaviour you observe, where an option is matching a prefix of the command line, even when there's a different option that matches fully, is a bug. It's fixed in 1.45.

孤独陪着我 2024-10-09 09:10:15

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

[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...

Maybe you called wrongly. You example is fine. Look at the output I got :

[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...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文