Reputation: 55
I need to make my app scan for a number of ports given as command line arguments and so far I have this:
QCommandLineOption portScanOption(QStringList() << "p" << "target-port",
QCoreApplication::translate("main", "Scan <port> for other instances"),
QCoreApplication::translate("main", "port"));
parser.addOption(portScanOption);
parser.process(app);
auto ports = parser.values(portScanOption); // do something with the ports
In order for this to work correctly, I need to pass the arguments like this: -p 5000 -p 7000 -p 8000
.
I am looking for a way to make this work for the following input: -p 5000 7000 8000
, but I haven't been able to find anything, not even in the official documentation. It's something quite trivial, so I expect Qt to have something like this. Do you guys know how it should be done?
Upvotes: 1
Views: 1285
Reputation: 98445
This is already handled - the syntax you propose doesn't quite fit QCommandLineParser
. Either use -p 5000,7000,8000
or -p "5000 7000 8000"
. All those ports will be the value of the port option, and you can then separate them with split(QRegularExpression("[, ]"), Qt::SkipEmptyParts)
(that's QString::split
). E.g.:
auto ports = parser.value("target-port")
.split(QRegularExpression("[, ]"), Qt::SkipEmptyParts);
for (auto &portStr : ports) {
bool ok = false;
int port = portStr.toInt(&ok);
if (!ok) continue;
// use port value here
}
Alternatively, since the Qt source code is available, you could copy the command line parser source into your project, rename the class, and extend it to support the original syntax you had in mind.
Upvotes: 1