src/CLIParser.h
author František Kučera <franta-hg@frantovo.cz>
Sun, 27 Dec 2020 21:57:52 +0100
branchv_0
changeset 0 73e60c77be23
permissions -rw-r--r--
project skeleton
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     1
/**
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     2
 * Relational pipes
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     3
 * Copyright © 2020 František Kučera (Frantovo.cz, GlobalCode.info)
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     4
 *
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     5
 * This program is free software: you can redistribute it and/or modify
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     6
 * it under the terms of the GNU General Public License as published by
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     7
 * the Free Software Foundation, version 3 of the License.
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     8
 *
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     9
 * This program is distributed in the hope that it will be useful,
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    12
 * GNU General Public License for more details.
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    13
 *
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    14
 * You should have received a copy of the GNU General Public License
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    15
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    16
 */
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    17
#pragma once
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    18
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    19
#include <vector>
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    20
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    21
#include <relpipe/common/type/typedefs.h>
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    22
#include <relpipe/cli/CLI.h>
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    23
#include <relpipe/cli/RelpipeCLIException.h>
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    24
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    25
#include "Configuration.h"
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    26
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    27
namespace relpipe {
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    28
namespace tr {
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    29
namespace xpath {
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    30
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    31
class CLIParser {
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    32
private:
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    33
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    34
	relpipe::common::type::StringX readNext(std::vector<relpipe::common::type::StringX> arguments, int& i) {
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    35
		if (i < arguments.size()) return arguments[i++];
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    36
		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);
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    37
	}
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    38
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    39
	/**
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    40
	 * TODO: use a common method
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    41
	 */
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    42
	bool parseBoolean(const relpipe::common::type::StringX& value) {
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    43
		if (value == L"true") return true;
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    44
		else if (value == L"false") return false;
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    45
		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);
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    46
	}
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    47
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    48
	void addRelation(Configuration& c, RelationConfiguration& currentRelation) {
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    49
		if (currentRelation.relation.size()) {
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    50
			c.relationConfigurations.push_back(currentRelation);
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    51
			currentRelation = RelationConfiguration();
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    52
		}
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    53
	}
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    54
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    55
	/**
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    56
	 * TODO: use a common method
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    57
	 */
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    58
	relpipe::writer::TypeId parseTypeId(const relpipe::common::type::StringX& value) {
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    59
		using t = relpipe::writer::TypeId;
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    60
		if (value == L"string") return t::STRING;
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    61
		else if (value == L"integer") return t::INTEGER;
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    62
		else if (value == L"boolean") return t::BOOLEAN;
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    63
		else throw relpipe::cli::RelpipeCLIException(L"Unable to parse TypeId: " + value, relpipe::cli::CLI::EXIT_CODE_BAD_CLI_ARGUMENTS);
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    64
	}
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    65
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    66
public:
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    67
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    68
	static const relpipe::common::type::StringX OPTION_NAMESPACE;
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    69
	static const relpipe::common::type::StringX OPTION_RELATION;
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    70
	static const relpipe::common::type::StringX OPTION_WHERE;
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    71
	static const relpipe::common::type::StringX OPTION_INPUT_ATTRIBUTES;
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    72
	static const relpipe::common::type::StringX OPTION_OUTPUT_ATTRIBUTE;
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    73
	static const relpipe::common::type::StringX OPTION_XML_ATTRIBUTE;
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    74
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    75
	Configuration parse(const std::vector<relpipe::common::type::StringX>& arguments) {
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    76
		Configuration c;
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    77
		RelationConfiguration currentRelation;
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    78
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    79
		for (int i = 0; i < arguments.size();) {
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    80
			relpipe::common::type::StringX option = readNext(arguments, i);
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    81
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    82
			if (option == OPTION_NAMESPACE) {
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    83
				c.namespaceMappings.push_back(readNext(arguments, i));
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    84
				c.namespaceMappings.push_back(readNext(arguments, i));
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    85
			} else if (option == OPTION_RELATION) {
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    86
				addRelation(c, currentRelation); // previous relation
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    87
				currentRelation.relation = readNext(arguments, i);
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    88
			} else if (option == OPTION_WHERE) {
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    89
				currentRelation.where = readNext(arguments, i);
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    90
			} else if (option == OPTION_XML_ATTRIBUTE) {
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    91
				currentRelation.xmlAttributes.push_back(readNext(arguments, i));
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    92
			} else if (option == OPTION_OUTPUT_ATTRIBUTE) {
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    93
				AttributeRecipe attribute;
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    94
				attribute.name = readNext(arguments, i);
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    95
				attribute.type = parseTypeId(readNext(arguments, i));
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    96
				attribute.xpath = readNext(arguments, i);
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    97
				currentRelation.outputAttributes.push_back(attribute);
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    98
			} else if (option == OPTION_INPUT_ATTRIBUTES) {
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    99
				relpipe::common::type::StringX policyName = readNext(arguments, i);
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   100
				InputAttributePolicy policy;
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   101
				if (policyName == L"append") policy = InputAttributePolicy::Append;
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   102
				else if (policyName == L"prepend") policy = InputAttributePolicy::Prepend;
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   103
				else if (policyName == L"auto") policy = InputAttributePolicy::Auto;
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   104
				else throw relpipe::cli::RelpipeCLIException(L"Unsupported input attributes policy: " + policyName, relpipe::cli::CLI::EXIT_CODE_BAD_CLI_ARGUMENTS);
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   105
				currentRelation.inputAttributePolicy = policy;
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   106
			} else throw relpipe::cli::RelpipeCLIException(L"Unsupported CLI option: " + option, relpipe::cli::CLI::EXIT_CODE_BAD_CLI_ARGUMENTS);
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   107
		}
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   108
		addRelation(c, currentRelation); // last relation
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   109
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   110
		return c;
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   111
	}
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   112
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   113
	virtual ~CLIParser() {
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   114
	}
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   115
};
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   116
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   117
const relpipe::common::type::StringX CLIParser::OPTION_NAMESPACE = L"--namespace";
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   118
const relpipe::common::type::StringX CLIParser::OPTION_RELATION = L"--relation";
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   119
const relpipe::common::type::StringX CLIParser::OPTION_WHERE = L"--where";
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   120
const relpipe::common::type::StringX CLIParser::OPTION_INPUT_ATTRIBUTES = L"--input-attributes";
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   121
const relpipe::common::type::StringX CLIParser::OPTION_OUTPUT_ATTRIBUTE = L"--output-attribute";
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   122
const relpipe::common::type::StringX CLIParser::OPTION_XML_ATTRIBUTE = L"--xml-attribute";
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   123
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   124
}
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   125
}
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   126
}