QCommandLineOption:如何多次解析同一参数?

时间:2019-06-30 17:10:23

标签: c++ qt command-line-arguments command-line-parsing qcommandlineparser

我正在尝试使用QCommandLineParser多次解析同一参数。问题是,当我尝试解析它们时,它返回一个空的QStringList,这意味着解析器遇到了某种错误,即使这些错误设置正确。

我以这种方式运行程序:./dns-benchmark domains.txt -d "208.67.222.222" -d "208.67.220.220" 我有一个正确返回的位置参数,但是随后我的-d arent:/

QCoreApplication coreApplication(argc, argv);
QCoreApplication::setApplicationName("dns-benchmark");
QCoreApplication::setApplicationVersion("0.0.1");

QCommandLineParser parser;
parser.setApplicationDescription("Benchmarks a bunch of different DNS servers.");
parser.addHelpOption();
parser.addVersionOption();
parser.addPositionalArgument("source", QCoreApplication::translate("main", "Source file containing the domains to resolve."));

QCommandLineOption supplementaryDnsOption(QStringList() << "d" << "dns", "Supplementary DNS to benchmark.", "IPv4 or IPv6 address");
parser.addOption(supplementaryDnsOption);

parser.process(coreApplication);

const QStringList args = parser.positionalArguments();
QString domainsToResolveFilePath = args.at(0);
QLinkedList<QHostAddress> dnsList;

if (parser.isSet(supplementaryDnsOption)) {
   const QStringList supplementaryDns = parser.values(supplementaryDnsOption);

   for (qint8 i = 0; i < supplementaryDns.size() -1; i++) {
       dnsList.append(QHostAddress(supplementaryDns.at(i)));
   }
}

但是我的const QStringList supplementaryDns完全是空的:/

1 个答案:

答案 0 :(得分:0)

尝试QCommandLineParser::values

#include <QCoreApplication>
#include <QCommandLineParser>
#include <QDebug>

int main(int argc, char *argv[])
{
    QCoreApplication app(argc, argv);
    QCommandLineParser parser;

    QCommandLineOption someOption(QStringList() << "p" << "param"
                                              , "some param", "blabla");
    parser.addOption(someOption);

    parser.process(app);

    qDebug() << parser.values(someOption);

    return app.exec();
}

这是使用该参数启动的方式:

app launch example

那是你想要的吗?