diff options
author | Thiago Macieira <thiago@cassini.local.lan> | 2007-12-24 10:48:00 -0200 |
---|---|---|
committer | Thiago Macieira <thiago@cassini.local.lan> | 2007-12-24 10:48:00 -0200 |
commit | 1228bd7c87f4e203883086d6884280653a8d1777 (patch) | |
tree | 659996b6c17d520e07ee4362307a0711d8745562 /src | |
parent | 14ddd2a51aa3a917bbb7fbade842818ab09802bc (diff) | |
download | svn2git-1228bd7c87f4e203883086d6884280653a8d1777.tar svn2git-1228bd7c87f4e203883086d6884280653a8d1777.tar.gz svn2git-1228bd7c87f4e203883086d6884280653a8d1777.tar.bz2 svn2git-1228bd7c87f4e203883086d6884280653a8d1777.tar.xz svn2git-1228bd7c87f4e203883086d6884280653a8d1777.zip |
Add a better option-parser
Diffstat (limited to 'src')
-rw-r--r-- | src/main.cpp | 12 | ||||
-rw-r--r-- | src/options.cpp | 126 | ||||
-rw-r--r-- | src/options.h | 44 | ||||
-rw-r--r-- | src/src.pro | 4 |
4 files changed, 177 insertions, 9 deletions
diff --git a/src/main.cpp b/src/main.cpp index 4415bca..a8f4069 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -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 index 0000000..69ae719 --- /dev/null +++ b/src/options.cpp @@ -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 index 0000000..30d1aab --- /dev/null +++ b/src/options.h @@ -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 diff --git a/src/src.pro b/src/src.pro index 2a2783f..9c1118e 100644 --- a/src/src.pro +++ b/src/src.pro @@ -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 |