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