如何使用 Boost.program_options 检测拼写错误?

发布于 2024-12-11 12:35:07 字数 471 浏览 0 评论 0原文

我使用 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 技术交流群。

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

发布评论

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

评论(3

晚风撩人 2024-12-18 12:35:07

我想在这样的命令行上产生错误:

testprog.exe -u c- action1

请注意,用户输入了错误的“c-”而不是“-c”。但是解析器
将其理解为单个 -u 选项。我该如何处理此类情况?

指示 program_options 库不接受任何位置参数,您将获得所需的行为

代码和结果。编译:

macmini:stackoverflow samm$ cat po.cc
#include <boost/program_options.hpp>
#include <boost/version.hpp>

#include <iostream>

int
main(int argc, char* argv[])
{
    namespace po = 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::command_line_parser cmd_line( argc, argv );
    cmd_line.options( desc );
    cmd_line.positional( po::positional_options_description() );

    try {
        po::store( cmd_line.run(), vm );

        po::notify(vm);
    } catch ( const std::exception& e ) {
        std::cerr << e.what() << std::endl;
        return -1;
    }

    return 0;
}
macmini:stackoverflow samm$ g++ po.cc -I /opt/local/include -L/opt/local/lib -lboost_program_options -Wl,-rpath,/opt/local/lib

运行:

macmini:stackoverflow samm$ ./a.out -u c- action1
too many positional options
macmini:stackoverflow samm$ ./a.out -u -c action1
macmini:stackoverflow samm$ 

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?

Instruct the program_options library to accept no positional arguments and you get the desired behavior

code & compile:

macmini:stackoverflow samm$ cat po.cc
#include <boost/program_options.hpp>
#include <boost/version.hpp>

#include <iostream>

int
main(int argc, char* argv[])
{
    namespace po = 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::command_line_parser cmd_line( argc, argv );
    cmd_line.options( desc );
    cmd_line.positional( po::positional_options_description() );

    try {
        po::store( cmd_line.run(), vm );

        po::notify(vm);
    } catch ( const std::exception& e ) {
        std::cerr << e.what() << std::endl;
        return -1;
    }

    return 0;
}
macmini:stackoverflow samm$ g++ po.cc -I /opt/local/include -L/opt/local/lib -lboost_program_options -Wl,-rpath,/opt/local/lib

run:

macmini:stackoverflow samm$ ./a.out -u c- action1
too many positional options
macmini:stackoverflow samm$ ./a.out -u -c action1
macmini:stackoverflow samm$ 
转身泪倾城 2024-12-18 12:35:07

argc-1program_options 找到的参数数量进行比较?如果不匹配,则存在语法错误。

它不会捕获所有情况,但可能会捕获对您重要的情况。

Compare argc-1 to the number of arguments found by program_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.

回忆躺在深渊里 2024-12-18 12:35:07

我认为做到这一点的唯一方法是确保您需要的每个参数都存在,例如通过测试每种类型的计数。

if (vm.count("uninstall")) { ... }
if (vm.count("custom")) { ... }

如果您需要的选项不存在(即计数为 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.

if (vm.count("uninstall")) { ... }
if (vm.count("custom")) { ... }

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

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