Port uic to QCommandLineParser

Before:
=======
Qt User Interface Compiler version 5.2.0
Usage: uic [options] <uifile>

  -h, -help                 display this help and exit
  -v, -version              display version
  -d, -dependencies         display the dependencies
  -o <file>                 place the output into <file>
  -tr <func>                use func() for i18n
  -p, -no-protection        disable header protection
  -n, -no-implicit-includes disable generation of #include-directives
                            for forms generated by uic3
  -g <name>                 change generator

After:
======
Usage: uic [options] [uifile]
Qt User Interface Compiler version 5.2.0

Options:
  -h, --help                    Displays this help.
  -v, --version                 Displays version information.
  -d, --dependencies            Display the dependencies.
  -o, --output <file>           Place the output into <file>
  -p, --no-protection           Disable header protection.
  -n, --no-implicit-includes    Disable generation of #include-directives.
  --postfix <postfix>           Postfix to add to all generated classnames.
  --tr, --translate <function>  Use <function> for i18n.
  -g, --generator <java|cpp>    Select generator.

Arguments:
  [uifile]                      Input file (*.ui), otherwise stdin.

Notes:
* "-dependencies" etc. still work.
* -n option still has effect, but technically not only for ui3 files
* the fact that the <uifile> parameter is optional wasn't documented
* -postfix option was undocumented
* -translate alternative for -tr was undocumented
The last two points show the benefit of using QCommandLineParser.

Change-Id: Ie05cfb9bbe50f4ac2788aa7b6011b2daa1acde6a
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Reviewed-by: Laszlo Papp <lpapp@kde.org>
Reviewed-by: Friedemann Kleint <Friedemann.Kleint@digia.com>
This commit is contained in:
David Faure 2013-07-26 13:10:08 +02:00 committed by The Qt Project
parent 5eb91476f1
commit 468b539935

View File

@ -47,94 +47,75 @@
#include <qdir.h>
#include <qtextstream.h>
#include <qtextcodec.h>
#include <qcoreapplication.h>
#include <qcommandlineoption.h>
#include <qcommandlineparser.h>
QT_BEGIN_NAMESPACE
static const char *error = 0;
void showHelp(const char *appName)
{
fprintf(stderr, "Qt User Interface Compiler version %s\n", QT_VERSION_STR);
if (error)
fprintf(stderr, "%s: %s\n", appName, error);
fprintf(stderr, "Usage: %s [options] <uifile>\n\n"
" -h, -help display this help and exit\n"
" -v, -version display version\n"
" -d, -dependencies display the dependencies\n"
" -o <file> place the output into <file>\n"
" -tr <func> use func() for i18n\n"
" -p, -no-protection disable header protection\n"
" -n, -no-implicit-includes disable generation of #include-directives\n"
" for forms generated by uic3\n"
" -g <name> change generator\n"
"\n", appName);
}
int runUic(int argc, char *argv[])
{
QCoreApplication app(argc, argv);
QCoreApplication::setApplicationVersion(QString::fromLatin1(QT_VERSION_STR));
Driver driver;
const char *fileName = 0;
// Note that uic 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 User Interface Compiler version %1").arg(QString::fromLatin1(QT_VERSION_STR)));
parser.addHelpOption();
parser.addVersionOption();
int arg = 1;
while (arg < argc) {
QString opt = QString::fromLocal8Bit(argv[arg]);
if (opt == QLatin1String("-h") || opt == QLatin1String("-help")) {
showHelp(argv[0]);
return 0;
} else if (opt == QLatin1String("-d") || opt == QLatin1String("-dependencies")) {
driver.option().dependencies = true;
} else if (opt == QLatin1String("-v") || opt == QLatin1String("-version")) {
fprintf(stderr, "Qt User Interface Compiler version %s\n", QT_VERSION_STR);
return 0;
} else if (opt == QLatin1String("-o") || opt == QLatin1String("-output")) {
++arg;
if (!argv[arg]) {
showHelp(argv[0]);
return 1;
}
driver.option().outputFile = QFile::decodeName(argv[arg]);
} else if (opt == QLatin1String("-p") || opt == QLatin1String("-no-protection")) {
driver.option().headerProtection = false;
} else if (opt == QLatin1String("-n") || opt == QLatin1String("-no-implicit-includes")) {
driver.option().implicitIncludes = false;
} else if (opt == QLatin1String("-postfix")) {
++arg;
if (!argv[arg]) {
showHelp(argv[0]);
return 1;
}
driver.option().postfix = QLatin1String(argv[arg]);
} else if (opt == QLatin1String("-tr") || opt == QLatin1String("-translate")) {
++arg;
if (!argv[arg]) {
showHelp(argv[0]);
return 1;
}
driver.option().translateFunction = QLatin1String(argv[arg]);
} else if (opt == QLatin1String("-g") || opt == QLatin1String("-generator")) {
++arg;
if (!argv[arg]) {
showHelp(argv[0]);
return 1;
}
QString name = QString::fromLocal8Bit(argv[arg]).toLower ();
driver.option().generator = (name == QLatin1String ("java")) ? Option::JavaGenerator : Option::CppGenerator;
} else if (!fileName) {
fileName = argv[arg];
} else {
showHelp(argv[0]);
return 1;
}
QCommandLineOption dependenciesOption(QStringList() << QStringLiteral("d") << QStringLiteral("dependencies"));
dependenciesOption.setDescription(QStringLiteral("Display the dependencies."));
parser.addOption(dependenciesOption);
++arg;
}
QCommandLineOption outputOption(QStringList() << QStringLiteral("o") << QStringLiteral("output"));
outputOption.setDescription(QStringLiteral("Place the output into <file>"));
outputOption.setValueName(QStringLiteral("file"));
parser.addOption(outputOption);
QCommandLineOption noProtOption(QStringList() << QStringLiteral("p") << QStringLiteral("no-protection"));
noProtOption.setDescription(QStringLiteral("Disable header protection."));
parser.addOption(noProtOption);
QCommandLineOption noImplicitIncludesOption(QStringList() << QStringLiteral("n") << QStringLiteral("no-implicit-includes"));
noImplicitIncludesOption.setDescription(QStringLiteral("Disable generation of #include-directives."));
parser.addOption(noImplicitIncludesOption);
QCommandLineOption postfixOption(QStringLiteral("postfix"));
postfixOption.setDescription(QStringLiteral("Postfix to add to all generated classnames."));
postfixOption.setValueName(QStringLiteral("postfix"));
parser.addOption(postfixOption);
QCommandLineOption translateOption(QStringList() << QStringLiteral("tr") << QStringLiteral("translate"));
translateOption.setDescription(QStringLiteral("Use <function> for i18n."));
translateOption.setValueName(QStringLiteral("function"));
parser.addOption(translateOption);
QCommandLineOption generatorOption(QStringList() << QStringLiteral("g") << QStringLiteral("generator"));
generatorOption.setDescription(QStringLiteral("Select generator."));
generatorOption.setValueName(QStringLiteral("java|cpp"));
parser.addOption(generatorOption);
parser.addPositionalArgument(QStringLiteral("[uifile]"), QStringLiteral("Input file (*.ui), otherwise stdin."));
parser.process(app);
driver.option().dependencies = parser.isSet(dependenciesOption);
driver.option().outputFile = parser.value(outputOption);
driver.option().headerProtection = !parser.isSet(noProtOption);
driver.option().implicitIncludes = !parser.isSet(noImplicitIncludesOption);
driver.option().postfix = parser.value(postfixOption);
driver.option().translateFunction = parser.value(translateOption);
driver.option().generator = (parser.value(generatorOption).toLower() == QLatin1String("java")) ? Option::JavaGenerator : Option::CppGenerator;
QString inputFile;
if (fileName)
inputFile = QString::fromLocal8Bit(fileName);
else
if (!parser.positionalArguments().isEmpty())
inputFile = parser.positionalArguments().first();
else // reading from stdin
driver.option().headerProtection = false;
if (driver.option().dependencies) {