From 468b539935331e7cd2eda8e5d2eef07b8e5508e9 Mon Sep 17 00:00:00 2001 From: David Faure Date: Fri, 26 Jul 2013 13:10:08 +0200 Subject: Port uic to QCommandLineParser Before: ======= Qt User Interface Compiler version 5.2.0 Usage: uic [options] -h, -help display this help and exit -v, -version display version -d, -dependencies display the dependencies -o place the output into -tr 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 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 Place the output into -p, --no-protection Disable header protection. -n, --no-implicit-includes Disable generation of #include-directives. --postfix Postfix to add to all generated classnames. --tr, --translate Use for i18n. -g, --generator 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 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 Reviewed-by: Laszlo Papp Reviewed-by: Friedemann Kleint --- src/tools/uic/main.cpp | 139 +++++++++++++++++++++---------------------------- 1 file changed, 60 insertions(+), 79 deletions(-) (limited to 'src/tools/uic') diff --git a/src/tools/uic/main.cpp b/src/tools/uic/main.cpp index a25a40052e..c29292a99b 100644 --- a/src/tools/uic/main.cpp +++ b/src/tools/uic/main.cpp @@ -47,94 +47,75 @@ #include #include #include +#include +#include +#include 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] \n\n" - " -h, -help display this help and exit\n" - " -v, -version display version\n" - " -d, -dependencies display the dependencies\n" - " -o place the output into \n" - " -tr 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 change generator\n" - "\n", appName); -} - int runUic(int argc, char *argv[]) { - Driver driver; + QCoreApplication app(argc, argv); + QCoreApplication::setApplicationVersion(QString::fromLatin1(QT_VERSION_STR)); - const char *fileName = 0; - - 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; - } + Driver driver; - ++arg; - } + // 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(); + + QCommandLineOption dependenciesOption(QStringList() << QStringLiteral("d") << QStringLiteral("dependencies")); + dependenciesOption.setDescription(QStringLiteral("Display the dependencies.")); + parser.addOption(dependenciesOption); + + QCommandLineOption outputOption(QStringList() << QStringLiteral("o") << QStringLiteral("output")); + outputOption.setDescription(QStringLiteral("Place the output into ")); + 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 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) { -- cgit v1.2.3