2

I'm trying to parse multiple times the same parameter with QCommandLineParser. The thing is that when I try to parse them, it returns me an empty QStringList, which means that parser encountered some kind of error, even though those are correctly set up.

I run my program this way : ./dns-benchmark domains.txt -d "208.67.222.222" -d "208.67.220.220" I've got a positional argument that is correctly returned, but then my -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)));
   }
}

but then my const QStringList supplementaryDns is totally empty :/

4

1 回答 1

3

Try 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();
}

This is how it launchs with that param:

app launch example

Is that what you wanted?

于 2019-06-30T19:06:01.253 回答