From 1228bd7c87f4e203883086d6884280653a8d1777 Mon Sep 17 00:00:00 2001 From: Thiago Macieira Date: Mon, 24 Dec 2007 10:48:00 -0200 Subject: [PATCH] Add a better option-parser --- src/main.cpp | 12 ++--- src/options.cpp | 126 ++++++++++++++++++++++++++++++++++++++++++++++++ src/options.h | 44 +++++++++++++++++ src/src.pro | 4 +- 4 files changed, 177 insertions(+), 9 deletions(-) create mode 100644 src/options.cpp create mode 100644 src/options.h 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 +#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 + * + * 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 . + */ + +#include "options.h" + +#include +#include + +#include +#include + +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 validOptions; + validOptions << "help"; + + QHash 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 + * + * 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 . + */ + +#ifndef OPTIONS_H +#define OPTIONS_H + +#include +#include + +class Options +{ +public: + // mandatory non-option arguments + QString ruleFile; + QString pathToRepository; + + // optional extras + QHash options; + QHash 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 -- 2.45.0