From a2318f763a91bc7c47bb751e39036ab95f4b45fc Mon Sep 17 00:00:00 2001 From: Marcelo Lira Date: Mon, 10 Jan 2011 20:20:11 -0300 Subject: Added the "project-file" command line option. The project file is a XML that provides the same options that could be provided via command line. Command line arguments added by further modules will be converted into xml tags on the project file following this rule: GENERATOR --new-boolean-option --new-option=SOMETHING in XML turns into: #include #include +#include #include #include #include "generatorrunnerconfig.h" @@ -49,12 +50,95 @@ static void printOptions(QTextStream& s, const QMap& options) typedef void (*getGeneratorsFunc)(QLinkedList*); -QMap getCommandLineArgs(int argc, char** argv) +static QString getPathString(const QDomElement& element) +{ + QStringList path; + QDomNode n = element.firstChild(); + while (!n.isNull()) { + QDomElement e = n.toElement(); // try to convert the node to an element. + if (e.tagName() == "path") + path << QDir::toNativeSeparators(e.attribute("location")); + n = n.nextSibling(); + } + return path.join(PATH_SPLITTER); +} + +static QMap getInitializedArguments() { QMap args; + QStringList arguments = QCoreApplication::arguments(); + QString appName = arguments.first(); + arguments.removeFirst(); + + QString projectFileName; + foreach (const QString& arg, arguments) { + if (arg.startsWith("--project-file")) { + int split = arg.indexOf("="); + if (split > 0) + projectFileName = arg.mid(split + 1).trimmed(); + break; + } + } + + if (projectFileName.isNull()) + return args; + + if (!QFile::exists(projectFileName)) { + std::cerr << qPrintable(appName) << ": Project file \"" << qPrintable(projectFileName) << "\" not found." << std::endl; + return args; + } + + QFile projectFile(projectFileName); + if (!projectFile.open(QIODevice::ReadOnly)) + return args; + + QDomDocument doc("project-file"); + if (!doc.setContent(&projectFile)) { + projectFile.close(); + return args; + } + projectFile.close(); + + QDomElement docElem = doc.documentElement(); + QDomNode n = docElem.firstChild(); + while (!n.isNull()) { + QDomElement e = n.toElement(); // try to convert the node to an element. + if (!e.isNull()) { + QString tag = e.tagName(); + if (tag == "generator-set") + args[tag] = e.attribute("generator"); + else if (tag == "output-directory" || tag == "license-file") + args[tag] = e.attribute("location"); + else if (tag == "api-version") + args[tag] = e.attribute("version"); + else if (tag == "debug") + args[tag] = e.attribute("level"); + else if (tag == "documentation-only" || tag == "no-suppress-warnings" || tag == "silent") + args[tag] = QString(); + else if (tag == "include-paths" || tag == "typesystem-paths") + args[tag] = getPathString(e); + else if (tag == "header-file") + args["arg-1"] = e.attribute("location"); + else if (tag == "typesystem-file") + args["arg-2"] = e.attribute("location"); + else + args[tag] = e.attribute("value"); + } + n = n.nextSibling(); + } + + return args; +} + +static QMap getCommandLineArgs() +{ + QMap args = getInitializedArguments(); + + QStringList arguments = QCoreApplication::arguments(); + arguments.removeFirst(); + int argNum = 0; - for (int i = 1; i < argc; ++i) { - QString arg(argv[i]); + foreach (QString arg, arguments) { arg = arg.trimmed(); if (arg.startsWith("--")) { int split = arg.indexOf("="); @@ -79,6 +163,7 @@ void printUsage(const GeneratorList& generators) << "generator [options] header-file typesystem-file\n\n" "General options:\n"; QMap generalOptions; + generalOptions.insert("project-file=[file]", "XML file containing a description of the binding project. Replaces and overrides command line arguments"); generalOptions.insert("debug-level=[sparse|medium|full]", "Set the debug level"); generalOptions.insert("silent", "Avoid printing any message"); generalOptions.insert("help", "Display this help and exit"); @@ -108,7 +193,7 @@ int main(int argc, char *argv[]) QCoreApplication app(argc, argv); // Store command arguments in a map - QMap args = getCommandLineArgs(argc, argv); + QMap args = getCommandLineArgs(); GeneratorList generators; if (args.contains("version")) { -- cgit v1.2.3