src/CLIParser.h
branchv_0
changeset 0 3493d6e7016e
child 1 3bbf848b3565
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/CLIParser.h	Sat Mar 20 19:18:10 2021 +0100
@@ -0,0 +1,129 @@
+/**
+ * Relational pipes
+ * Copyright © 2021 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, version 3 of the License.
+ *
+ * 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 <iostream>
+
+#include <relpipe/common/type/typedefs.h>
+#include <relpipe/cli/CLI.h>
+#include <relpipe/cli/RelpipeCLIException.h>
+
+#include "Configuration.h"
+
+namespace relpipe {
+namespace in {
+namespace x11 {
+
+class CLIParser {
+private:
+
+	relpipe::common::type::StringX readNext(const std::vector<relpipe::common::type::StringX>& 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 readNextRecord(const std::vector<relpipe::common::type::StringX>& arguments, int& i, RelationConfiguration& currentRelation) {
+		for (const auto& a : currentRelation.attributes) currentRelation.values.emplace_back(readNext(arguments, i));
+	}
+
+	void addRelation(Configuration& c, RelationConfiguration& currentRelation) {
+		if (currentRelation.relation.size()) {
+			c.relationConfigurations.push_back(currentRelation);
+			currentRelation = RelationConfiguration();
+		}
+	}
+
+	/**
+	 * TODO: use a common method
+	 */
+	bool parseBoolean(const relpipe::common::type::StringX& value) {
+		if (value == L"true") return true;
+		else if (value == L"false") return false;
+		else throw relpipe::cli::RelpipeCLIException(L"Unable to parse boolean value: " + value + L" (expecting true or false)", relpipe::cli::CLI::EXIT_CODE_BAD_CLI_ARGUMENTS);
+	}
+
+	/**
+	 * TODO: use a common method
+	 */
+	relpipe::writer::TypeId parseTypeId(const relpipe::common::type::StringX& 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 relpipe::common::type::StringX OPTION_RELATION;
+	static const relpipe::common::type::StringX OPTION_WRITE_HEADER;
+	static const relpipe::common::type::StringX OPTION_ATTRIBUTE;
+	static const relpipe::common::type::StringX OPTION_RECORD;
+	static const relpipe::common::type::StringX OPTION_RECORDS;
+	static const relpipe::common::type::StringX OPTION_RECORDS_STDIN;
+
+	Configuration parse(const std::vector<relpipe::common::type::StringX>& arguments) {
+		Configuration c;
+		RelationConfiguration currentRelation;
+
+		for (int i = 0; i < arguments.size();) {
+			relpipe::common::type::StringX option = readNext(arguments, i);
+
+			if (option == OPTION_RELATION) {
+				addRelation(c, currentRelation); // previous relation
+				currentRelation.relation = readNext(arguments, i);
+			} else if (option == OPTION_WRITE_HEADER) {
+				currentRelation.writeHeader = parseBoolean(readNext(arguments, i));
+			} else if (option == OPTION_ATTRIBUTE) {
+				AttributeRecipe attribute;
+				attribute.name = readNext(arguments, i);
+				attribute.type = parseTypeId(readNext(arguments, i));
+				currentRelation.attributes.push_back(attribute);
+			} else if (option == OPTION_RECORD) {
+				readNextRecord(arguments, i, currentRelation);
+			} else if (option == OPTION_RECORDS) {
+				while (i < arguments.size()) readNextRecord(arguments, i, currentRelation);
+			} else if (option == OPTION_RECORDS_STDIN) {
+				if (parseBoolean(readNext(arguments, i))) {
+					for (RelationConfiguration r : c.relationConfigurations) if (r.valueStream) throw relpipe::cli::RelpipeCLIException(L"Only one relation can read data from STDIN.", relpipe::cli::CLI::EXIT_CODE_BAD_CLI_ARGUMENTS);
+					currentRelation.valueStream = &std::cin;
+				}
+			} else throw relpipe::cli::RelpipeCLIException(L"Unsupported CLI option: " + option, relpipe::cli::CLI::EXIT_CODE_BAD_CLI_ARGUMENTS);
+		}
+		addRelation(c, currentRelation); // last relation
+
+		if (c.relationConfigurations.size() == 0) throw relpipe::cli::RelpipeCLIException(L"There must be at least one relation. Use the " + OPTION_RELATION + L" option.", relpipe::cli::CLI::EXIT_CODE_BAD_CLI_ARGUMENTS);
+		for (auto& rc : c.relationConfigurations) if (rc.attributes.size() == 0) throw relpipe::cli::RelpipeCLIException(L"There must be at least one attribute. Use the " + OPTION_ATTRIBUTE + L" option.", relpipe::cli::CLI::EXIT_CODE_BAD_CLI_ARGUMENTS);
+
+		return c;
+	}
+
+	virtual ~CLIParser() {
+	}
+};
+
+const relpipe::common::type::StringX CLIParser::OPTION_RELATION = L"--relation";
+const relpipe::common::type::StringX CLIParser::OPTION_WRITE_HEADER = L"--write-header";
+const relpipe::common::type::StringX CLIParser::OPTION_ATTRIBUTE = L"--attribute";
+const relpipe::common::type::StringX CLIParser::OPTION_RECORD = L"--record";
+const relpipe::common::type::StringX CLIParser::OPTION_RECORDS = L"--records";
+const relpipe::common::type::StringX CLIParser::OPTION_RECORDS_STDIN = L"--records-on-stdin";
+
+}
+}
+}