configuration and CLI parser v_0
authorFrantišek Kučera <franta-hg@frantovo.cz>
Wed, 24 Jul 2019 15:53:42 +0200
branchv_0
changeset 1 7d6ac51c0d48
parent 0 a37196931f63
child 2 0d3eb5129582
configuration and CLI parser
src/CLIParser.h
src/Configuration.h
src/relpipe-in-xmltable.cpp
--- /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";
+
+}
+}
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/Configuration.h	Wed Jul 24 15:53:42 2019 +0200
@@ -0,0 +1,63 @@
+/**
+ * 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>
+
+
+namespace relpipe {
+namespace in {
+namespace xmltable {
+
+class AttributeRecipe {
+public:
+
+	virtual ~AttributeRecipe() {
+	}
+
+	relpipe::writer::string_t name;
+	relpipe::writer::TypeId type;
+	relpipe::writer::string_t xpath;
+};
+
+class RelationConfiguration {
+public:
+
+	virtual ~RelationConfiguration() {
+	}
+
+	relpipe::writer::string_t relation;
+	relpipe::writer::string_t xpath;
+	std::vector<AttributeRecipe> attributes;
+	
+};
+
+class Configuration {
+public:
+	std::vector<RelationConfiguration> relationConfigurations;
+	std::vector<relpipe::writer::string_t> namespaceMappings;
+
+	virtual ~Configuration() {
+	}
+};
+
+}
+}
+}
\ No newline at end of file
--- a/src/relpipe-in-xmltable.cpp	Wed Jul 24 12:15:10 2019 +0200
+++ b/src/relpipe-in-xmltable.cpp	Wed Jul 24 15:53:42 2019 +0200
@@ -32,6 +32,8 @@
 #include <relpipe/cli/CLI.h>
 
 #include "XMLTableCommand.h"
+#include "CLIParser.h"
+#include "Configuration.h"
 
 using namespace relpipe::cli;
 using namespace relpipe::writer;
@@ -45,6 +47,8 @@
 	int resultCode = CLI::EXIT_CODE_UNEXPECTED_ERROR;
 
 	try {
+		CLIParser cliParser;
+		Configuration configuration = cliParser.parse(cli.arguments());
 		XMLCommand command;
 		command.process(cin, cout);
 		resultCode = CLI::EXIT_CODE_SUCCESS;
@@ -52,6 +56,10 @@
 		fwprintf(stderr, L"Caught Writer exception: %ls\n", e.getMessge().c_str());
 		fwprintf(stderr, L"Debug: Input stream: eof=%ls, lastRead=%d\n", (cin.eof() ? L"true" : L"false"), cin.gcount());
 		resultCode = CLI::EXIT_CODE_DATA_ERROR;
+	} catch (RelpipeCLIException& e) {
+		fwprintf(stderr, L"Caught CLI exception: %ls\n", e.getMessge().c_str());
+		fwprintf(stderr, L"Debug: Input stream: eof=%ls, lastRead=%d\n", (cin.eof() ? L"true" : L"false"), cin.gcount());
+		resultCode = e.getExitCode();
 	} // TODO: catch xmlpp::exception
 
 	return resultCode;