[英]How to use filename wildcards with QCommandLineParser
Is there a problem with QCommandLineParser and filename wildcards in Windows? Windows中的QCommandLineParser和文件名通配符是否存在问题?
I'm using Qt 5.8.0 opensource on Windows to build a console application. 我正在Windows上使用Qt 5.8.0开源构建控制台应用程序。 I'm trying to build a command line utility that accepts filename wildcards. 我正在尝试构建一个接受文件名通配符的命令行实用程序。 This doesn't seem to work, as it bails on the process() method. 这似乎不起作用,因为它基于process()方法。
main.cpp : main.cpp :
#include <QCoreApplication>
#include <QCommandLineParser>
#include <iostream>
#include <cstdlib>
using namespace std;
int main(int argc, char** argv)
{
QCoreApplication app(argc, argv);
QCommandLineParser parser;
parser.addPositionalArgument("files", "input files", "[file]...");
parser.process(app);
QStringList posargs = parser.positionalArguments();
foreach(QString s, posargs)
cout << s.toStdString() << endl;
return EXIT_SUCCESS;
}
myapp.pro myapp.pro
CONFIG += c++14 console release
QT -= gui
sources = main.cpp
When I use the command: 当我使用命令时:
myapp somefile.txt
I get somefile.txt
我得到了somefile.txt
But this does not work with the command: 但这不适用于以下命令:
myapp *.txt
What's the best way around this? 最好的办法是什么?
QCommandLineParser
just stores and retrieves the command-line arguments. QCommandLineParser
只是存储和检索命令行参数。 It knows nothing about filesystem; 它对文件系统一无所知。 if you want to expand a wildcard, you'll need to take a QDir
and set its name filter yourself, like this: 如果要扩展通配符,则需要使用QDir
并QDir
设置其名称过滤器,如下所示:
#include <QCoreApplication>
#include <QCommandLineParser>
#include <QDir>
#include <iostream>
int main(int argc, char** argv)
{
QCoreApplication app(argc, argv);
QCommandLineParser parser;
parser.addPositionalArgument("files", "input files", "[file]...");
parser.process(app);
QStringList posargs = parser.positionalArguments();
for (auto const& s: posargs) {
auto d = QDir{};
d.setNameFilters({s});
for (const auto& name: d.entryList()) {
std::cout << name.toStdString() << '\n';
}
}
}
You'll need to be a bit cleverer to accept arbitrary path wildcards, of course - here, we assume there's no path separator within the argument. 当然,您需要更聪明地接受任意路径通配符-在这里,我们假设参数中没有路径分隔符。
Until this gets fixed, my solution is to make sure to enclose parameters with wildcards in them with single quotes. 在解决此问题之前,我的解决方案是确保参数中的通配符用单引号引起来。 Then parse out the wildcard in a manner similar to what Toby Speight suggested: 然后以类似于Toby Speight建议的方式解析通配符:
int main(int argc, char** argv)
{
QCoreApplication app(argc, argv);
QCommandLineParser parser;
parser.addPositionalArgument("files", "input files", "[file]...");
parser.process(app);
QStringList posargs = parser.positionalArguments();
for(auto s: posargs)
{
if(s.startsWith("'") && s.endsWith("'"))
s = s.mid(1,s.length()-2);
QFileInfo fi(s);
QDirIterator iter(fi.path(), QStringList() << fi.fileName(), QDir::Files);
while(iter.hasNext())
{
QString filename = iter.next();
cout << s.toStdString() << endl;
}
}
return EXIT_SUCCESS;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.