src/CLIParser.h
branchv_0
changeset 1 7d6ac51c0d48
child 2 0d3eb5129582
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/CLIParser.h	Wed Jul 24 15:53:42 2019 +0200
@@ -0,0 +1,101 @@
+/**
+ * Relational pipes
+ * Copyright © 2019 František Kučera (Frantovo.cz, GlobalCode.info)
+ *
+ * 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 3 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/>.
+ */
+#pragma once
+
+#include <vector>
+
+#include <relpipe/writer/typedefs.h>
+#include <relpipe/cli/CLI.h>
+#include <relpipe/cli/RelpipeCLIException.h>
+
+#include "Configuration.h"
+
+namespace relpipe {
+namespace in {
+namespace xmltable {
+
+class CLIParser {
+private:
+
+	string_t readNext(std::vector<string_t> arguments, int& i) {
+		if (i < arguments.size()) return arguments[i++];
+		else throw relpipe::cli::RelpipeCLIException(L"Missing CLI argument" + (i > 0 ? (L" after " + arguments[i - 1]) : L""), relpipe::cli::CLI::EXIT_CODE_BAD_CLI_ARGUMENTS);
+	}
+
+	void addRelation(Configuration& c, RelationConfiguration& currentRelation) {
+		if (currentRelation.relation.size()) {
+			c.relationConfigurations.push_back(currentRelation);
+			currentRelation = RelationConfiguration();
+		}
+	}
+
+	relpipe::writer::TypeId parseTypeId(const string_t& value) {
+		using t = relpipe::writer::TypeId;
+		if (value == L"string") return t::STRING;
+		else if (value == L"integer") return t::INTEGER;
+		else if (value == L"boolean") return t::BOOLEAN;
+		else throw relpipe::cli::RelpipeCLIException(L"Unable to parse TypeId: " + value, relpipe::cli::CLI::EXIT_CODE_BAD_CLI_ARGUMENTS);
+	}
+
+public:
+
+	static const string_t OPTION_NAMESPACE;
+	static const string_t OPTION_RELATION;
+	static const string_t OPTION_RECORDS;
+	static const string_t OPTION_ATTRIBUTE;
+
+	Configuration parse(const std::vector<string_t>& arguments) {
+		Configuration c;
+		RelationConfiguration currentRelation;
+
+		for (int i = 0; i < arguments.size();) {
+			string_t option = readNext(arguments, i);
+
+			if (option == OPTION_NAMESPACE) {
+				c.namespaceMappings.push_back(readNext(arguments, i));
+				c.namespaceMappings.push_back(readNext(arguments, i));
+			} else if (option == OPTION_RELATION) {
+				addRelation(c, currentRelation); // previous relation
+				currentRelation.relation = readNext(arguments, i);
+			} else if (option == OPTION_RECORDS) {
+				currentRelation.xpath = readNext(arguments, i);
+			} else if (option == OPTION_ATTRIBUTE) {
+				AttributeRecipe attribute;
+				attribute.name = readNext(arguments, i);
+				attribute.type = parseTypeId(readNext(arguments, i));
+				attribute.xpath = readNext(arguments, i);
+				currentRelation.attributes.push_back(attribute);
+			} else throw relpipe::cli::RelpipeCLIException(L"Unsupported CLI option: " + option, relpipe::cli::CLI::EXIT_CODE_BAD_CLI_ARGUMENTS);
+		}
+		addRelation(c, currentRelation); // last relation
+
+		return c;
+	}
+
+	virtual ~CLIParser() {
+	}
+};
+
+const string_t CLIParser::OPTION_NAMESPACE = L"--namespace";
+const string_t CLIParser::OPTION_RELATION = L"--relation";
+const string_t CLIParser::OPTION_RECORDS = L"--records";
+const string_t CLIParser::OPTION_ATTRIBUTE = L"--attribute";
+
+}
+}
+}