--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/CLIParser.h Sun Dec 27 21:57:52 2020 +0100
@@ -0,0 +1,126 @@
+/**
+ * Relational pipes
+ * Copyright © 2020 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 <relpipe/common/type/typedefs.h>
+#include <relpipe/cli/CLI.h>
+#include <relpipe/cli/RelpipeCLIException.h>
+
+#include "Configuration.h"
+
+namespace relpipe {
+namespace tr {
+namespace xpath {
+
+class CLIParser {
+private:
+
+ relpipe::common::type::StringX readNext(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);
+ }
+
+ /**
+ * 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);
+ }
+
+ void addRelation(Configuration& c, RelationConfiguration& currentRelation) {
+ if (currentRelation.relation.size()) {
+ c.relationConfigurations.push_back(currentRelation);
+ currentRelation = RelationConfiguration();
+ }
+ }
+
+ /**
+ * 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_NAMESPACE;
+ static const relpipe::common::type::StringX OPTION_RELATION;
+ static const relpipe::common::type::StringX OPTION_WHERE;
+ static const relpipe::common::type::StringX OPTION_INPUT_ATTRIBUTES;
+ static const relpipe::common::type::StringX OPTION_OUTPUT_ATTRIBUTE;
+ static const relpipe::common::type::StringX OPTION_XML_ATTRIBUTE;
+
+ 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_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_WHERE) {
+ currentRelation.where = readNext(arguments, i);
+ } else if (option == OPTION_XML_ATTRIBUTE) {
+ currentRelation.xmlAttributes.push_back(readNext(arguments, i));
+ } else if (option == OPTION_OUTPUT_ATTRIBUTE) {
+ AttributeRecipe attribute;
+ attribute.name = readNext(arguments, i);
+ attribute.type = parseTypeId(readNext(arguments, i));
+ attribute.xpath = readNext(arguments, i);
+ currentRelation.outputAttributes.push_back(attribute);
+ } else if (option == OPTION_INPUT_ATTRIBUTES) {
+ relpipe::common::type::StringX policyName = readNext(arguments, i);
+ InputAttributePolicy policy;
+ if (policyName == L"append") policy = InputAttributePolicy::Append;
+ else if (policyName == L"prepend") policy = InputAttributePolicy::Prepend;
+ else if (policyName == L"auto") policy = InputAttributePolicy::Auto;
+ else throw relpipe::cli::RelpipeCLIException(L"Unsupported input attributes policy: " + policyName, relpipe::cli::CLI::EXIT_CODE_BAD_CLI_ARGUMENTS);
+ currentRelation.inputAttributePolicy = policy;
+ } 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 relpipe::common::type::StringX CLIParser::OPTION_NAMESPACE = L"--namespace";
+const relpipe::common::type::StringX CLIParser::OPTION_RELATION = L"--relation";
+const relpipe::common::type::StringX CLIParser::OPTION_WHERE = L"--where";
+const relpipe::common::type::StringX CLIParser::OPTION_INPUT_ATTRIBUTES = L"--input-attributes";
+const relpipe::common::type::StringX CLIParser::OPTION_OUTPUT_ATTRIBUTE = L"--output-attribute";
+const relpipe::common::type::StringX CLIParser::OPTION_XML_ATTRIBUTE = L"--xml-attribute";
+
+}
+}
+}