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

Majork
  • 23
  • 6
  • 2
    Why would you ever want to parse the same thing more than once? – Jesper Juhl Jun 30 '19 at 17:12
  • Well it seems reasonable that sometimes a user needs to add multiple values ? It could be for anything, like multiple keywords. The only use case that came to mind was [this one](https://gnunet.org/en/use.html), under the section Filesharing – Majork Jun 30 '19 at 17:19
  • Would it be possible to use the low level C API? It is also cross platform and can parse what you want. https://www.geeksforgeeks.org/getopt-function-in-c-to-parse-command-line-arguments/ – Ali Tavakol Jun 30 '19 at 18:13
  • I mean yeah sure If QCommandLine doesn't allow this kind of thing I will be forced to try this :/ – Majork Jun 30 '19 at 18:15
  • can you parse single -d "208.67.222.222" – Vencat Nov 24 '20 at 16:47

1 Answers1

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?

MasterAler
  • 1,614
  • 3
  • 23
  • 35