如何将QCommandLineParser用于具有多个参数的参数? [英] How to use QCommandLineParser for arguments with multiple params?

查看:519
本文介绍了如何将QCommandLineParser用于具有多个参数的参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何与 QCommandLineParser ? 例如:

I wonder, how can I use multiple- or sub-arguments with QCommandLineParser? For example:

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

推荐答案

尝试类似-I /my/include/path1 -I /my/include/path2的方法:

 --my_option_with_two_params first_param --my_option_with_two_params second_param

...,然后您可以使用此方法可以访问这些值:

... and then you can use this method to have access to the values:

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

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

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

Returns a list of option values found for the given option name optionName, or an empty list if not found.

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

The name provided can be any long or short name of any option that was added with addOption().

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

Here you can find a simple test case that works:

#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

运行和输出

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

这篇关于如何将QCommandLineParser用于具有多个参数的参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆