Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何在QCommandLineParser中使用文件名通配符_C++_Qt_Qt5_Qcommandlineparser - Fatal编程技术网

C++ 如何在QCommandLineParser中使用文件名通配符

C++ 如何在QCommandLineParser中使用文件名通配符,c++,qt,qt5,qcommandlineparser,C++,Qt,Qt5,Qcommandlineparser,Windows中的QCommandLineParser和文件名通配符是否存在问题 我在Windows上使用Qt5.8.0开源来构建一个控制台应用程序。我正在尝试构建一个接受文件名通配符的命令行实用程序。这似乎不起作用,因为它依赖于process()方法 main.cpp: #include <QCoreApplication> #include <QCommandLineParser> #include <iostream> #include <cstd

Windows中的QCommandLineParser和文件名通配符是否存在问题

我在Windows上使用Qt5.8.0开源来构建一个控制台应用程序。我正在尝试构建一个接受文件名通配符的命令行实用程序。这似乎不起作用,因为它依赖于process()方法

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 somefile.txt

我得到
somefile.txt

但这不适用于命令:

myapp*.txt


最好的解决方法是什么?

QCommandLineParser
只存储和检索命令行参数。它对文件系统一无所知;如果要展开通配符,需要使用
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';
       }
   }
}
#包括
#包括
#包括
#包括
int main(int argc,字符**argv)
{
QCore应用程序应用程序(argc、argv);
QCommandLineParser;
addPositionArgument(“文件”,“输入文件”,“文件]…”);
进程(app);
QStringList posargs=parser.positionarguments();
用于(自动常量&s:posargs){
自动d=QDir{};
d、 setNameFilters({s});
对于(常量自动(&N):d.entryList()){

std::cout
QCommandLineParser
只存储和检索命令行参数。它对文件系统一无所知;如果要扩展通配符,需要使用
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';
       }
   }
}
#包括
#包括
#包括
#包括
int main(int argc,字符**argv)
{
QCore应用程序应用程序(argc、argv);
QCommandLineParser;
addPositionArgument(“文件”,“输入文件”,“文件]…”);
进程(app);
QStringList posargs=parser.positionarguments();
用于(自动常量&s:posargs){
自动d=QDir{};
d、 setNameFilters({s});
对于(常量自动(&N):d.entryList()){

std::cout在修复之前,我的解决方案是确保使用通配符将参数用单引号括起来。然后以类似建议的方式解析通配符:

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;
}
int main(int argc,char**argv)
{
QCore应用程序应用程序(argc、argv);
QCommandLineParser;
addPositionArgument(“文件”,“输入文件”,“文件]…”);
进程(app);
QStringList posargs=parser.positionarguments();
用于(自动s:posargs)
{
如果(s.startWith(“”)和s.endsWith(“”)
s=s.mid(1,s.length()-2);
QFileInfo金融机构;

QDirIterator iter(fi.path(),QStringList()在修复此问题之前,我的解决方案是确保使用通配符将参数用单引号括起来。然后以类似建议的方式解析通配符:

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;
}
int main(int argc,char**argv)
{
QCore应用程序应用程序(argc、argv);
QCommandLineParser;
addPositionArgument(“文件”,“输入文件”,“文件]…”);
进程(app);
QStringList posargs=parser.positionarguments();
用于(自动s:posargs)
{
如果(s.startWith(“”)和s.endsWith(“”)
s=s.mid(1,s.length()-2);
QFileInfo金融机构;

QDirIterator iter(fi.path(),QStringList()
myapp*.txt
myapp*.txt
(无空格)?,第一个选项是:
myapp
*.txt
,第二个选项是:
myapp*.txt
yes,使用
myapp[space]*.txt
作为测试此功能的命令。您正在运行什么版本的Qt?LinuxI上的Qt 5.11.2将在5.11上尝试此功能,并查看它的运行情况…我认为这是一个错误,我建议报告:-)
myapp*.txt
myapp*.txt
(无空格)?使用第一个选项:
myapp
*.txt
,使用第二个选项:
myapp*.txt
是的,使用
myapp[space]*.txt
作为命令来测试。您运行的是什么版本的Qt?LinuxI上的Qt 5.11.2将在5.11上尝试一下,看看效果如何……我认为这是一个bug,我建议报告:-)我本来打算做这样的事情,但是在Windows上,程序停在
parser.process(app);
,这让我很难过。我没有访问Windows安装的权限,所以你已经完成了我的建议-抱歉!我本来打算做这样的事情,但是在Windows上,程序停在
parser.process(app)上;
,这让我很难过。我没有访问Windows安装的权限,所以您已经完成了我在这里的建议-抱歉!