]> andersk Git - svn-all-fast-export.git/commitdiff
Add a better option-parser
authorThiago Macieira <thiago@cassini.local.lan>
Mon, 24 Dec 2007 12:48:00 +0000 (10:48 -0200)
committerThiago Macieira <thiago@cassini.local.lan>
Mon, 24 Dec 2007 12:48:00 +0000 (10:48 -0200)
src/main.cpp
src/options.cpp [new file with mode: 0644]
src/options.h [new file with mode: 0644]
src/src.pro

index 4415bca5ec8a4b4eec7e1f0525c94d7b0cf70a6c..a8f4069fb217f38a55d6519cf7140926398c8d30 100644 (file)
@@ -20,6 +20,7 @@
 
 #include <stdio.h>
 
+#include "options.h"
 #include "ruleparser.h"
 #include "repository.h"
 #include "svn.h"
@@ -28,14 +29,11 @@ int main(int argc, char **argv)
 {
     QCoreApplication app(argc, argv);
 
-    QStringList arguments = app.arguments();
-    if (arguments.count() < 3) {
-        printf("Usage: svn-all-fast-export configfile path-to-svn\n");
-        return 0;
-    }
+    Options options;
+    options.parseArguments(app.arguments());
 
     // Load the configuration
-    Rules rules(arguments.at(1));
+    Rules rules(options.ruleFile);
     rules.load();
 
     // create the repository list
@@ -44,7 +42,7 @@ int main(int argc, char **argv)
         repositories.insert(rule.name, new Repository(rule));
 
     Svn::initialize();
-    Svn svn(arguments.at(2));
+    Svn svn(options.pathToRepository);
     svn.setMatchRules(rules.matchRules());
     svn.setRepositories(repositories);
 
diff --git a/src/options.cpp b/src/options.cpp
new file mode 100644 (file)
index 0000000..69ae719
--- /dev/null
@@ -0,0 +1,126 @@
+/*
+ *  Copyright (C) 2007 Thiago Macieira <thiago@kde.org>
+ *
+ *  This program is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation, either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "options.h"
+
+#include <QSet>
+#include <QStringList>
+
+#include <stdio.h>
+#include <stdlib.h>
+
+Options* Options::globalOptions = 0;
+
+Options::Options()
+{
+    globalOptions = this;
+}
+
+Options::~Options()
+{
+}
+
+void Options::showHelp()
+{
+    printf("Usage: svn-all-fast-export configfile path-to-svn\n");
+}
+
+void Options::parseArguments(const QStringList &argumentList)
+{
+    QSet<QString> validOptions;
+    validOptions << "help";
+
+    QHash<QString, QString> optionsWithComplement;
+    optionsWithComplement.insert("resume-from", QString());
+    optionsWithComplement.insert("identity-map", QString());
+
+    QStringList arguments = argumentList;
+    arguments.takeFirst();           // the first one is the executable name; drop it
+    while (!arguments.isEmpty()) {
+        QString arg = arguments.takeFirst();
+        QString complement;
+
+        if (arg == "--")
+            break;
+
+        if (arg.startsWith("--"))
+            arg = arg.mid(1);   // drop double dashes to single
+
+        if (arg.startsWith("-no-")) {
+            complement = "no";
+            arg = arg.mid(4);
+        } else if (!arg.startsWith("-")) {
+            // non-option arg
+            break;
+        } else { // starts with "-"
+            arg = arg.mid(1);
+        }
+
+        if (arg.contains('=') && complement.isEmpty()) {
+            int pos = arg.indexOf('=');
+            complement = arg.mid(pos + 1);
+            arg.truncate(pos);
+        }
+
+        if (optionsWithComplement.contains(arg)) {
+            if (arguments.isEmpty()) {
+                fprintf(stderr, "Option -%s requires an argument", qPrintable(arg));
+                exit(2);
+            }
+
+            QString &setting = optionsWithComplement[arg];
+            if (!setting.isNull()) {
+                fprintf(stderr, "Option -%s given more than once", qPrintable(arg));
+                exit(2);
+            }
+
+            if (!complement.isEmpty())
+                setting = complement;
+            else if (!arguments.isEmpty())
+                setting = arguments.takeFirst();
+            else {
+                fprintf(stderr, "Option -%s requires an argument", qPrintable(arg));
+                exit(2);
+            }
+            continue;
+        } else if (validOptions.contains(arg)) {
+            if (switches.contains(arg)) {
+                fprintf(stderr, "Option -%s given more than once", qPrintable(arg));
+                exit(2);
+            }
+
+            switches[arg] = !(complement == "no");
+        } else {
+            if (complement == "no")
+                fprintf(stderr, "Invalid option: -no-%s", qPrintable(arg));
+            else
+                fprintf(stderr, "Invalid option: -%s", qPrintable(arg));
+            exit(2);
+        }
+    }
+
+    if (switches.value("help")) {
+        showHelp();
+        exit(0);
+    } else if (arguments.count() < 2) {
+        showHelp();
+        exit(2);
+    }
+
+    ruleFile = arguments.takeFirst();
+    pathToRepository = arguments.takeFirst();
+}
diff --git a/src/options.h b/src/options.h
new file mode 100644 (file)
index 0000000..30d1aab
--- /dev/null
@@ -0,0 +1,44 @@
+/*
+ *  Copyright (C) 2007 Thiago Macieira <thiago@kde.org>
+ *
+ *  This program is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation, either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef OPTIONS_H
+#define OPTIONS_H
+
+#include <QHash>
+#include <QString>
+
+class Options
+{
+public:
+    // mandatory non-option arguments
+    QString ruleFile;
+    QString pathToRepository;
+
+    // optional extras
+    QHash<QString, QString> options;
+    QHash<QString, bool> switches;
+
+    Options();
+    ~Options();
+
+    void showHelp();
+    void parseArguments(const QStringList &arguments);
+
+    static Options *globalOptions;
+};
+
+#endif
index 2a2783f6384bf209a688b1bb80b7484c4d87f3db..9c1118e691085d4ef5a6d567b1d8d8163829ee62 100644 (file)
@@ -16,5 +16,5 @@ INCLUDEPATH += . $$SVN_INCLUDE $$APR_INCLUDE
 LIBS += -lsvn_fs-1 -lsvn_repos-1
 
 # Input
-SOURCES += ruleparser.cpp repository.cpp svn.cpp main.cpp
-HEADERS += ruleparser.h repository.h svn.h
+SOURCES += options.cpp ruleparser.cpp repository.cpp svn.cpp main.cpp
+HEADERS += options.h ruleparser.h repository.h svn.h
This page took 0.037059 seconds and 5 git commands to generate.