【问题标题】:How to use QCommandLineParser for arguments with multiple params?如何将 QCommandLineParser 用于具有多个参数的参数?
【发布时间】:2014-12-22 19:07:34
【问题描述】:

我想知道,如何在QCommandLineParser 中使用多个或子参数? 例如:

/home/my_app --my_option_with_two_params first_param second_param --my-option-with-one-param param?

【问题讨论】:

    标签: c++ qt qt5 qtcore qcommandlineparser


    【解决方案1】:

    试试这个类似于-I /my/include/path1 -I /my/include/path2

     --my_option_with_two_params first_param --my_option_with_two_params second_param
    

    ...然后您可以使用this method 访问这些值:

    QStringList QCommandLineParser::values(const QString & optionName) const

    返回为给定选项名称 optionName 找到的选项值列表,如果未找到,则返回空列表。

    提供的名称可以是使用 addOption() 添加的任何选项的任何长名称或短名称。

    在这里你可以找到一个简单的测试用例:

    main.cpp

    #include <QCoreApplication>
    #include <QCommandLineParser>
    #include <QCommandLineOption>
    #include <QDebug>
    
    int main(int argc, char *argv[])
    {
        QCoreApplication app(argc, argv);
        QCoreApplication::setApplicationName("multiple-values-program");
        QCoreApplication::setApplicationVersion("1.0");
    
        QCommandLineParser parser;
        parser.setApplicationDescription("Test helper");
        parser.addHelpOption();
        parser.addVersionOption();
    
        QCommandLineOption targetDirectoryOption(QStringList() << "t" << "target-directory",
                QCoreApplication::translate("main", "Copy all source files into <directory>."),
                QCoreApplication::translate("main", "directory"));
        parser.addOption(targetDirectoryOption);
    
        parser.process(app);
    
        qDebug() << parser.values(targetDirectoryOption);
        return 0;
    }
    

    main.pro

    TEMPLATE = app
    TARGET = main
    QT = core
    SOURCES += main.cpp
    

    构建

    qmake && make
    

    使用--help时的输出

    Usage: main [options]
    Test helper
    
    Options:
      -h, --help                          Displays this help.
      -v, --version                       Displays version information.
      -t, --target-directory <directory>  Copy all source files into <directory>.
    

    运行和输出

    ./main -t foo -t bar -> ("foo", "bar")
    ./main -t foo bar    -> ("foo")
    

    【讨论】:

    • 别生气:)我明白你在说什么。我只是不想让我的虚拟用户写command --option arg1 --option arg2,而是写command --option arg1 arg2。是否有一些收件箱解决方案,或者我应该为它写一些具体的东西?
    • @VALOD9:因为你什么时候可以写,例如在包含路径的clang或任何其他常用运行的软件中?现在,像分隔符这样的东西会很好,这样你就可以写“foo,bar”,但是只要你添加这样的东西,复杂性就会开始成倍增加。当我和 David Faure 一起学习这门课时,我们力求简单,同时考虑到最常见的用例。几乎不可能为每个人的口味编写一个通用解析器,其 API 对大多数人来说仍然很简单。
    • 我不是命令行界面专家:) 我刚刚查看了 Qt 5.2、Qt 5.3 和 Qt 5.4 beta 中的新功能并尝试了解新功能。那么,您是说在大多数使用 CLI 的项目中,我们没有这种可能性?
    • @VALOD9:我的经验表明在这种情况下使用分隔符,例如在 gcc 中,但如果我可以这样说并且也令人困惑,那么它也是非标准的,因为 -I(包含路径)不能像那样工作,所以它们是不一致的。这个类本来是非常简单的,如果需要可以在以后扩展。只是一个简单的问题:你怎么知道 -t foo bar 是否意味着 -t foo + 位置参数?但是如果你写 -t foo,bar 你怎么知道 "foo,bar" 是一个字符串还是多个?如您所见,它很容易变得复杂。
    猜你喜欢
    • 1970-01-01
    • 2017-03-14
    • 1970-01-01
    • 2017-09-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多