From 63f7a0685f4edad88d29325c30a3ad5a00f06ce4 Mon Sep 17 00:00:00 2001 From: Florian Geyer Date: Thu, 18 Apr 2013 21:21:30 +0200 Subject: [PATCH] Small ArgumentParser refactoring. --- src/core/ArgumentParser.cpp | 35 ++++++++++++++++++++--------------- src/core/ArgumentParser.h | 4 +++- tests/TestArgumentParser.cpp | 7 +++++++ tests/TestArgumentParser.h | 1 + 4 files changed, 31 insertions(+), 16 deletions(-) diff --git a/src/core/ArgumentParser.cpp b/src/core/ArgumentParser.cpp index d76df7562..4a579a46c 100644 --- a/src/core/ArgumentParser.cpp +++ b/src/core/ArgumentParser.cpp @@ -17,27 +17,15 @@ #include "ArgumentParser.h" +QStringList ArgumentParser::argumentKeys = QStringList() << "password" << "config" << "filename"; + QHash ArgumentParser::parseArguments(const QStringList& args) { - QStringList argumentKeys = QStringList() << "password" << "config" << "filename"; - QHash argumentMap; - // TODO: needs refactoring, too much nesting for (int i = 1; i < args.size(); i++) { if (args[i].startsWith("--")) { - if (args.size() > (i + 1)) { - QString argument(args[i].mid(2)); - if (argumentKeys.contains(argument)) { - argumentMap.insert(argument, args[i + 1]); - } - else { - qWarning("Unknown option \"%s\" with value \"%s\"", qPrintable(args[i]), qPrintable(args[i+1])); - } - i++; - } else { - qWarning("No value given for option \"%s\"", qPrintable(args[i])); - } + parseOption(i, argumentMap, args); } else if (!args[i].startsWith("-")) { argumentMap.insert("filename", args[i]); @@ -49,3 +37,20 @@ QHash ArgumentParser::parseArguments(const QStringList& args) return argumentMap; } + +void ArgumentParser::parseOption(int& i, QHash& argumentMap, const QStringList& args) +{ + if (args.size() < (i + 2)) { + qWarning("No value given for option \"%s\"", qPrintable(args[i])); + return; + } + + QString argument(args[i].mid(2)); + if (argumentKeys.contains(argument)) { + argumentMap.insert(argument, args[i + 1]); + } + else { + qWarning("Unknown option \"%s\" with value \"%s\"", qPrintable(args[i]), qPrintable(args[i+1])); + } + i++; +} diff --git a/src/core/ArgumentParser.h b/src/core/ArgumentParser.h index c594d51a6..a9dac52a4 100644 --- a/src/core/ArgumentParser.h +++ b/src/core/ArgumentParser.h @@ -28,7 +28,9 @@ class ArgumentParser public: static QHash parseArguments(const QStringList& args); - +private: + static QStringList argumentKeys; + static void parseOption(int& i, QHash& argumentMap, const QStringList& args); }; #endif // KEEPASSX_ARGUMENT_PARSER_H diff --git a/tests/TestArgumentParser.cpp b/tests/TestArgumentParser.cpp index 95bfb3420..60bd63444 100644 --- a/tests/TestArgumentParser.cpp +++ b/tests/TestArgumentParser.cpp @@ -31,6 +31,13 @@ void TestArgumentParser::testNoArguments() QVERIFY(argumentMap.isEmpty()); } +void TestArgumentParser::testMissingOptionValue() +{ + parse(QStringList() << "--foo"); + + QVERIFY(argumentMap.isEmpty()); +} + void TestArgumentParser::testUnknownArgument() { parse(QStringList() << "--foo" << "bar"); diff --git a/tests/TestArgumentParser.h b/tests/TestArgumentParser.h index 7b4940e54..8fb1e7a0a 100644 --- a/tests/TestArgumentParser.h +++ b/tests/TestArgumentParser.h @@ -27,6 +27,7 @@ class TestArgumentParser : public QObject private Q_SLOTS: void testNoArguments(); + void testMissingOptionValue(); void testUnknownArgument(); void testFilename(); void testMultipleArguments();