如何使用 Boost.program_options 检测拼写错误?
我使用 boost.program_options 库。考虑这个简化的情况。
po::options_description desc("Usage");
desc.add_options()
("uninstall,u", "uninstall program")
("custom,c", po::wvalue<std::wstring>(), "specify custom action");
po::variables_map vm;
po::store(po::parse_command_line(argc, argv, desc), vm);
po::notify(vm);
我想在这样的命令行上产生错误:
testprog.exe -u c- action1
注意,用户输入错误“c-”而不是“-c”。但解析器将其理解为单个 -u 选项。我该如何处理此类情况?
I use boost.program_options library. Consider this simplified case.
po::options_description desc("Usage");
desc.add_options()
("uninstall,u", "uninstall program")
("custom,c", po::wvalue<std::wstring>(), "specify custom action");
po::variables_map vm;
po::store(po::parse_command_line(argc, argv, desc), vm);
po::notify(vm);
I want to produce error on such command-line:
testprog.exe -u c- action1
Note, user made a typo "c-" instead of "-c". But the parser understands this as a single -u option. How do I handle such cases?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
指示
program_options
库不接受任何位置参数,您将获得所需的行为代码和结果。编译:
运行:
Instruct the
program_options
library to accept no positional arguments and you get the desired behaviorcode & compile:
run:
将
argc-1
与program_options
找到的参数数量进行比较?如果不匹配,则存在语法错误。它不会捕获所有情况,但可能会捕获对您重要的情况。
Compare
argc-1
to the number of arguments found byprogram_options
? If it doesn't match, there is a syntax error.It won't catch all cases but it may catch those important to you.
我认为做到这一点的唯一方法是确保您需要的每个参数都存在,例如通过测试每种类型的计数。
如果您需要的选项不存在(即计数为 0)或存在(例如无法指定
-u
和-c
将是 count,则可能会生成错误两者都是>0
)。I think the only way you can do this is to ensure that each argument you require is present, for example by testing the count of each type.
You can generate an error if the options you require are not present (i.e. count is 0) or are present (for example
-u
and-c
cannot be specified would be count of both is>0
).