简体   繁体   中英

QCommandLineOption: How to parse the same parameter multiple times?

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 :/

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:

应用启动示例

Is that what you wanted?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM