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 版)