From ebde32ae3d743ad1d3f70295317dcea9c0377314 Mon Sep 17 00:00:00 2001 From: Andreas Wilhelm Date: Fri, 15 Jul 2016 14:40:00 +0200 Subject: qdbusxml2cpp: Ported to QCommandLineParser Change-Id: I8a91a376ba60b110fff9eb84e1b02e3c6e8c5e30 Reviewed-by: Rolf Eike Beer Reviewed-by: Thiago Macieira --- src/tools/qdbusxml2cpp/qdbusxml2cpp.cpp | 203 ++++++++++++-------------------- 1 file changed, 74 insertions(+), 129 deletions(-) (limited to 'src/tools/qdbusxml2cpp') diff --git a/src/tools/qdbusxml2cpp/qdbusxml2cpp.cpp b/src/tools/qdbusxml2cpp/qdbusxml2cpp.cpp index 8fc54794bc..e58f194b72 100644 --- a/src/tools/qdbusxml2cpp/qdbusxml2cpp.cpp +++ b/src/tools/qdbusxml2cpp/qdbusxml2cpp.cpp @@ -27,6 +27,8 @@ ****************************************************************************/ #include +#include +#include #include #include #include @@ -59,30 +61,6 @@ static QString commandLine; static QStringList includes; static QStringList wantedInterfaces; -static const char help[] = - "Usage: " PROGRAMNAME " [options...] [xml-or-xml-file] [interfaces...]\n" - "Produces the C++ code to implement the interfaces defined in the input file.\n" - "\n" - "Options:\n" - " -a Write the adaptor code to \n" - " -c Use as the class name for the generated classes\n" - " -h Show this information\n" - " -i Add #include to the output\n" - " -l When generating an adaptor, use as the parent class\n" - " -m Generate #include \"filename.moc\" statements in the .cpp files\n" - " -N Don't use namespaces\n" - " -p Write the proxy code to \n" - " -v Be verbose.\n" - " -V Show the program version and quit.\n" - "\n" - "If the file name given to the options -a and -p does not end in .cpp or .h, the\n" - "program will automatically append the suffixes and produce both files.\n" - "You can also use a colon (:) to separate the header name from the source file\n" - "name, as in '-a filename_p.h:filename.cpp'.\n" - "\n" - "If you pass a dash (-) as the argument to either -p or -a, the output is written\n" - "to the standard output\n"; - static const char includeList[] = "#include \n" "#include \n" @@ -101,105 +79,6 @@ static const char forwardDeclarations[] = "class QVariant;\n" "QT_END_NAMESPACE\n"; -static void showHelp() -{ - printf("%s", help); - exit(0); -} - -static void showVersion() -{ - printf("%s version %s\n", PROGRAMNAME, PROGRAMVERSION); - printf("D-Bus binding tool for Qt\n"); - exit(0); -} - -static QString nextArg(QStringList &args, int i, char opt) -{ - QString arg = args.value(i); - if (arg.isEmpty()) { - printf("-%c needs at least one argument\n", opt); - exit(1); - } - return args.takeAt(i); -} - -static void parseCmdLine(QStringList args) -{ - args.takeFirst(); - - commandLine = QLatin1String(PROGRAMNAME " "); - commandLine += args.join(QLatin1Char(' ')); - - int i = 0; - while (i < args.count()) { - - if (!args.at(i).startsWith(QLatin1Char('-'))) { - ++i; - continue; - } - QString arg = args.takeAt(i); - - char c = '\0'; - if (arg.length() == 2) - c = arg.at(1).toLatin1(); - else if (arg == QLatin1String("--help")) - c = 'h'; - - switch (c) { - case 'a': - adaptorFile = nextArg(args, i, 'a'); - break; - - case 'c': - globalClassName = nextArg(args, i, 'c'); - break; - - case 'v': - verbose = true; - break; - - case 'i': - includes << nextArg(args, i, 'i'); - break; - - case 'l': - parentClassName = nextArg(args, i, 'l'); - break; - - case 'm': - includeMocs = true; - break; - - case 'N': - skipNamespaces = true; - break; - - case '?': - case 'h': - showHelp(); - break; - - case 'V': - showVersion(); - break; - - case 'p': - proxyFile = nextArg(args, i, 'p'); - break; - - default: - printf("unknown option: '%s'\n", qPrintable(arg)); - exit(1); - } - } - - if (!args.isEmpty()) - inputFile = args.takeFirst(); - - wantedInterfaces << args; -} - static QDBusIntrospection::Interfaces readInput() { QFile input(inputFile); @@ -1139,13 +1018,79 @@ static void writeAdaptor(const QString &filename, const QDBusIntrospection::Inte int main(int argc, char **argv) { - QStringList arguments; - arguments.reserve(argc); - for (int i = 0; i < argc; ++i) { - arguments.append(QString::fromLocal8Bit(argv[i])); - } + QCoreApplication app(argc, argv); + QCoreApplication::setApplicationName(QStringLiteral(PROGRAMNAME)); + QCoreApplication::setApplicationVersion(QStringLiteral(PROGRAMVERSION)); + + QCommandLineParser parser; + parser.setApplicationDescription(QLatin1String( + "Produces the C++ code to implement the interfaces defined in the input file.\n\n" + "If the file name given to the options -a and -p does not end in .cpp or .h, the\n" + "program will automatically append the suffixes and produce both files.\n" + "You can also use a colon (:) to separate the header name from the source file\n" + "name, as in '-a filename_p.h:filename.cpp'.\n\n" + "If you pass a dash (-) as the argument to either -p or -a, the output is written\n" + "to the standard output.")); + + parser.addHelpOption(); + parser.addVersionOption(); + parser.addPositionalArgument(QStringLiteral("xml-or-xml-file"), QStringLiteral("XML file to use.")); + parser.addPositionalArgument(QStringLiteral("interfaces"), QStringLiteral("List of interfaces to use."), + QStringLiteral("[interfaces ...]")); + + QCommandLineOption adapterCodeOption(QStringList() << QStringLiteral("a") << QStringLiteral("adaptor"), + QStringLiteral("Write the adaptor code to "), QStringLiteral("filename")); + parser.addOption(adapterCodeOption); + + QCommandLineOption classNameOption(QStringList() << QStringLiteral("c") << QStringLiteral("classname"), + QStringLiteral("Use as the class name for the generated classes"), QStringLiteral("classname")); + parser.addOption(classNameOption); + + QCommandLineOption addIncludeOption(QStringList() << QStringLiteral("i") << QStringLiteral("include"), + QStringLiteral("Add #include to the output"), QStringLiteral("filename")); + parser.addOption(addIncludeOption); + + QCommandLineOption adapterParentOption(QStringLiteral("l"), + QStringLiteral("When generating an adaptor, use as the parent class"), QStringLiteral("classname")); + parser.addOption(adapterParentOption); + + QCommandLineOption mocIncludeOption(QStringList() << QStringLiteral("m") << QStringLiteral("moc"), + QStringLiteral("Generate #include \"filename.moc\" statements in the .cpp files")); + parser.addOption(mocIncludeOption); + + QCommandLineOption noNamespaceOption(QStringList() << QStringLiteral("N") << QStringLiteral("no-namespaces"), + QStringLiteral("Don't use namespaces")); + parser.addOption(noNamespaceOption); + + QCommandLineOption proxyCodeOption(QStringList() << QStringLiteral("p") << QStringLiteral("proxy"), + QStringLiteral("Write the proxy code to "), QStringLiteral("filename")); + parser.addOption(proxyCodeOption); + + QCommandLineOption verboseOption(QStringList() << QStringLiteral("V") << QStringLiteral("verbose"), + QStringLiteral("Be verbose.")); + parser.addOption(verboseOption); + + parser.process(app); + + adaptorFile = parser.value(adapterCodeOption); + globalClassName = parser.value(classNameOption); + includes = parser.values(addIncludeOption); + parentClassName = parser.value(adapterParentOption); + includeMocs = parser.isSet(mocIncludeOption); + skipNamespaces = parser.isSet(noNamespaceOption); + proxyFile = parser.value(proxyCodeOption); + verbose = parser.isSet(verboseOption); + + wantedInterfaces = parser.positionalArguments(); + if (!wantedInterfaces.isEmpty()) { + inputFile = wantedInterfaces.takeFirst(); - parseCmdLine(arguments); + QFileInfo inputInfo(inputFile); + if (!inputInfo.exists() || !inputInfo.isFile() || !inputInfo.isReadable()) { + qCritical("Error: Input %s is not a file or cannot be accessed\n", qPrintable(inputFile)); + return 1; + } + } QDBusIntrospection::Interfaces interfaces = readInput(); cleanInterfaces(interfaces); -- cgit v1.2.3