Port rcc to QCommandLineParser.

Before:
=======
Qt resource compiler
Usage: rcc  [options] <inputs>

Options:
  -o file              write output to file rather than stdout
  -name name           create an external initialization function with name
  -threshold level     threshold to consider compressing files
  -compress level      compress input files by level
  -root path           prefix resource access path with root path
  -no-compress         disable all compression
  -binary              output a binary file for use as a dynamic resource
  -namespace           turn off namespace macros
  -project             Output a resource file containing all
                       files from the current directory
  -version             display version
  -help                display this information

Undocumented: -verbose and -list !

After:
======
Usage: rcc [options] inputs
Qt Resource Compiler version 5.2.0

Options:
  -h, --help          Displays this help.
  -v, --version       Displays version information.
  -o, --output <file> Write output to <file> rather than stdout.
  --name <name>       Create an external initialization function with <name>.
  --root <path>       Prefix resource access path with root path.
  --compress <level>  Compress input files by <level>.
  --no-compress       Disable all compression.
  --threshold <level> Threshold to consider compressing files.
  --binary            Output a binary file for use as a dynamic resource.
  --namespace         Turn off namespace macros.
  --verbose           Enable verbose mode.
  --list              Only list the files, do not generate code.
  --project           Output a resource file containing all files from the current directory.

Arguments:
  inputs              Input files (*.qrc).

Change-Id: If20958afd6c01df5d0d755e13e8581bc1cb9af51
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
David Faure 2013-08-08 12:37:00 +02:00 committed by The Qt Project
parent 6ee9baf044
commit 4774ff8eb1

View File

@ -49,32 +49,13 @@
#include <qtextstream.h>
#include <qatomic.h>
#include <qglobal.h>
#include <qcoreapplication.h>
#include <qcommandlineoption.h>
#include <qcommandlineparser.h>
QT_BEGIN_NAMESPACE
void showHelp(const QString &argv0, const QString &error)
{
fprintf(stderr, "Qt resource compiler\n");
if (!error.isEmpty())
fprintf(stderr, "%s: %s\n", qPrintable(argv0), qPrintable(error));
fprintf(stderr, "Usage: %s [options] <inputs>\n\n"
"Options:\n"
" -o file write output to file rather than stdout\n"
" -name name create an external initialization function with name\n"
" -threshold level threshold to consider compressing files\n"
" -compress level compress input files by level\n"
" -root path prefix resource access path with root path\n"
" -no-compress disable all compression\n"
" -binary output a binary file for use as a dynamic resource\n"
" -namespace turn off namespace macros\n"
" -project Output a resource file containing all\n"
" files from the current directory\n"
" -list lists .qrc file entries\n"
" -version display version\n"
" -help display this information\n",
qPrintable(argv0));
}
void dumpRecursive(const QDir &dir, QTextStream &out)
{
QFileInfoList entries = dir.entryInfoList(QDir::Files | QDir::Dirs | QDir::NoDotAndDotDot
@ -127,92 +108,103 @@ int createProject(const QString &outFileName)
int runRcc(int argc, char *argv[])
{
QString outFilename;
bool helpRequested = false;
bool list = false;
bool projectRequested = false;
QStringList filenamesIn;
QCoreApplication app(argc, argv);
QCoreApplication::setApplicationVersion(QString::fromLatin1(QT_VERSION_STR));
QStringList args = qCmdLineArgs(argc, argv);
// Note that rcc isn't translated.
// If you use this code as an example for a translated app, make sure to translate the strings.
QCommandLineParser parser;
parser.setSingleDashWordOptionMode(QCommandLineParser::ParseAsLongOptions);
parser.setApplicationDescription(QStringLiteral("Qt Resource Compiler version %1").arg(QString::fromLatin1(QT_VERSION_STR)));
parser.addHelpOption();
parser.addVersionOption();
QCommandLineOption outputOption(QStringList() << QStringLiteral("o") << QStringLiteral("output"));
outputOption.setDescription(QStringLiteral("Write output to <file> rather than stdout."));
outputOption.setValueName(QStringLiteral("file"));
parser.addOption(outputOption);
QCommandLineOption nameOption(QStringLiteral("name"), QStringLiteral("Create an external initialization function with <name>."), QStringLiteral("name"));
parser.addOption(nameOption);
QCommandLineOption rootOption(QStringLiteral("root"), QStringLiteral("Prefix resource access path with root path."), QStringLiteral("path"));
parser.addOption(rootOption);
QCommandLineOption compressOption(QStringLiteral("compress"), QStringLiteral("Compress input files by <level>."), QStringLiteral("level"));
parser.addOption(compressOption);
QCommandLineOption nocompressOption(QStringLiteral("no-compress"), QStringLiteral("Disable all compression."));
parser.addOption(nocompressOption);
QCommandLineOption thresholdOption(QStringLiteral("threshold"), QStringLiteral("Threshold to consider compressing files."), QStringLiteral("level"));
parser.addOption(thresholdOption);
QCommandLineOption binaryOption(QStringLiteral("binary"), QStringLiteral("Output a binary file for use as a dynamic resource."));
parser.addOption(binaryOption);
QCommandLineOption namespaceOption(QStringLiteral("namespace"), QStringLiteral("Turn off namespace macros."));
parser.addOption(namespaceOption);
QCommandLineOption verboseOption(QStringLiteral("verbose"), QStringLiteral("Enable verbose mode."));
parser.addOption(verboseOption);
QCommandLineOption listOption(QStringLiteral("list"), QStringLiteral("Only list .qrc file entries, do not generate code."));
parser.addOption(listOption);
QCommandLineOption projectOption(QStringLiteral("project"), QStringLiteral("Output a resource file containing all files from the current directory."));
parser.addOption(projectOption);
parser.addPositionalArgument(QStringLiteral("inputs"), QStringLiteral("Input files (*.qrc)."));
RCCResourceLibrary library;
//parse options
parser.process(app);
QString errorMsg;
for (int i = 1; i < args.count() && errorMsg.isEmpty(); i++) {
if (args[i].isEmpty())
continue;
if (args[i][0] == QLatin1Char('-')) { // option
QString opt = args[i];
if (opt == QLatin1String("-o")) {
if (!(i < argc-1)) {
errorMsg = QLatin1String("Missing output name");
break;
}
outFilename = args[++i];
} else if (opt == QLatin1String("-name")) {
if (!(i < argc-1)) {
errorMsg = QLatin1String("Missing target name");
break;
}
library.setInitName(args[++i]);
} else if (opt == QLatin1String("-root")) {
if (!(i < argc-1)) {
errorMsg = QLatin1String("Missing root path");
break;
}
library.setResourceRoot(QDir::cleanPath(args[++i]));
if (library.resourceRoot().isEmpty()
|| library.resourceRoot().at(0) != QLatin1Char('/'))
errorMsg = QLatin1String("Root must start with a /");
} else if (opt == QLatin1String("-compress")) {
if (!(i < argc-1)) {
errorMsg = QLatin1String("Missing compression level");
break;
}
library.setCompressLevel(args[++i].toInt());
} else if (opt == QLatin1String("-threshold")) {
if (!(i < argc-1)) {
errorMsg = QLatin1String("Missing compression threshold");
break;
}
library.setCompressThreshold(args[++i].toInt());
} else if (opt == QLatin1String("-binary")) {
library.setFormat(RCCResourceLibrary::Binary);
} else if (opt == QLatin1String("-namespace")) {
library.setUseNameSpace(!library.useNameSpace());
} else if (opt == QLatin1String("-verbose")) {
library.setVerbose(true);
} else if (opt == QLatin1String("-list")) {
list = true;
} else if (opt == QLatin1String("-version") || opt == QLatin1String("-v")) {
fprintf(stderr, "Qt Resource Compiler version %s\n", QT_VERSION_STR);
return 1;
} else if (opt == QLatin1String("-help") || opt == QLatin1String("-h")) {
helpRequested = true;
} else if (opt == QLatin1String("-no-compress")) {
library.setCompressLevel(-2);
} else if (opt == QLatin1String("-project")) {
projectRequested = true;
} else {
errorMsg = QString::fromLatin1("Unknown option: '%1'").arg(args[i]);
}
} else {
if (!QFile::exists(args[i])) {
qWarning("%s: File does not exist '%s'",
qPrintable(args[0]), qPrintable(args[i]));
return 1;
}
filenamesIn.append(args[i]);
RCCResourceLibrary library;
QString outFilename = parser.value(outputOption);
if (parser.isSet(nameOption))
library.setInitName(parser.value(nameOption));
if (parser.isSet(rootOption)) {
library.setResourceRoot(QDir::cleanPath(parser.value(rootOption)));
if (library.resourceRoot().isEmpty()
|| library.resourceRoot().at(0) != QLatin1Char('/'))
errorMsg = QLatin1String("Root must start with a /");
}
if (parser.isSet(compressOption))
library.setCompressLevel(parser.value(compressOption).toInt());
if (parser.isSet(nocompressOption))
library.setCompressLevel(-2);
if (parser.isSet(thresholdOption))
library.setCompressThreshold(parser.value(thresholdOption).toInt());
if (parser.isSet(binaryOption))
library.setFormat(RCCResourceLibrary::Binary);
if (parser.isSet(namespaceOption))
library.setUseNameSpace(!library.useNameSpace());
if (parser.isSet(verboseOption))
library.setVerbose(true);
const bool list = parser.isSet(listOption);
const bool projectRequested = parser.isSet(projectOption);
const QStringList filenamesIn = parser.positionalArguments();
foreach (const QString &file, filenamesIn) {
if (!QFile::exists(file)) {
qWarning("%s: File does not exist '%s'", argv[0], qPrintable(file));
return 1;
}
}
if (projectRequested && !helpRequested) {
if (projectRequested) {
return createProject(outFilename);
}
if (!filenamesIn.size() || !errorMsg.isEmpty() || helpRequested) {
showHelp(args[0], errorMsg);
if (filenamesIn.isEmpty())
errorMsg = QStringLiteral("No input files specified.");
if (!errorMsg.isEmpty()) {
fprintf(stderr, "%s: %s\n", argv[0], qPrintable(errorMsg));
parser.showHelp(1);
return 1;
}
QFile errorDevice;