src/CLIParser.h
author František Kučera <franta-hg@frantovo.cz>
Tue, 11 May 2021 22:03:45 +0200
branchv_0
changeset 28 bc15f5471b6a
permissions -rw-r--r--
new CLI interface: --relation, --attribute
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
28
bc15f5471b6a new CLI interface: --relation, --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     1
/**
bc15f5471b6a new CLI interface: --relation, --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     2
 * Relational pipes
bc15f5471b6a new CLI interface: --relation, --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     3
 * Copyright © 2021 František Kučera (Frantovo.cz, GlobalCode.info)
bc15f5471b6a new CLI interface: --relation, --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     4
 *
bc15f5471b6a new CLI interface: --relation, --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     5
 * This program is free software: you can redistribute it and/or modify
bc15f5471b6a new CLI interface: --relation, --attribute
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
bc15f5471b6a new CLI interface: --relation, --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     7
 * the Free Software Foundation, version 3 of the License.
bc15f5471b6a new CLI interface: --relation, --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     8
 *
bc15f5471b6a new CLI interface: --relation, --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     9
 * This program is distributed in the hope that it will be useful,
bc15f5471b6a new CLI interface: --relation, --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
bc15f5471b6a new CLI interface: --relation, --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
bc15f5471b6a new CLI interface: --relation, --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    12
 * GNU General Public License for more details.
bc15f5471b6a new CLI interface: --relation, --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    13
 *
bc15f5471b6a new CLI interface: --relation, --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    14
 * You should have received a copy of the GNU General Public License
bc15f5471b6a new CLI interface: --relation, --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    15
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
bc15f5471b6a new CLI interface: --relation, --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    16
 */
bc15f5471b6a new CLI interface: --relation, --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    17
#pragma once
bc15f5471b6a new CLI interface: --relation, --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    18
bc15f5471b6a new CLI interface: --relation, --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    19
#include <vector>
bc15f5471b6a new CLI interface: --relation, --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    20
bc15f5471b6a new CLI interface: --relation, --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    21
#include <relpipe/common/type/typedefs.h>
bc15f5471b6a new CLI interface: --relation, --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    22
#include <relpipe/cli/CLI.h>
bc15f5471b6a new CLI interface: --relation, --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    23
#include <relpipe/cli/RelpipeCLIException.h>
bc15f5471b6a new CLI interface: --relation, --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    24
bc15f5471b6a new CLI interface: --relation, --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    25
#include "Configuration.h"
bc15f5471b6a new CLI interface: --relation, --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    26
bc15f5471b6a new CLI interface: --relation, --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    27
namespace relpipe {
bc15f5471b6a new CLI interface: --relation, --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    28
namespace tr {
bc15f5471b6a new CLI interface: --relation, --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    29
namespace cut {
bc15f5471b6a new CLI interface: --relation, --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    30
bc15f5471b6a new CLI interface: --relation, --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    31
class CLIParser {
bc15f5471b6a new CLI interface: --relation, --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    32
private:
bc15f5471b6a new CLI interface: --relation, --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    33
bc15f5471b6a new CLI interface: --relation, --attribute
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) {
bc15f5471b6a new CLI interface: --relation, --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    35
		if (i < arguments.size()) return arguments[i++];
bc15f5471b6a new CLI interface: --relation, --attribute
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);
bc15f5471b6a new CLI interface: --relation, --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    37
	}
bc15f5471b6a new CLI interface: --relation, --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    38
bc15f5471b6a new CLI interface: --relation, --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    39
	/**
bc15f5471b6a new CLI interface: --relation, --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    40
	 * TODO: use a common method
bc15f5471b6a new CLI interface: --relation, --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    41
	 */
bc15f5471b6a new CLI interface: --relation, --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    42
	bool parseBoolean(const relpipe::common::type::StringX& value) {
bc15f5471b6a new CLI interface: --relation, --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    43
		if (value == L"true") return true;
bc15f5471b6a new CLI interface: --relation, --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    44
		else if (value == L"false") return false;
bc15f5471b6a new CLI interface: --relation, --attribute
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);
bc15f5471b6a new CLI interface: --relation, --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    46
	}
bc15f5471b6a new CLI interface: --relation, --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    47
bc15f5471b6a new CLI interface: --relation, --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    48
	RelationConfiguration::ENTITY parseEntity(const relpipe::common::type::StringX& value) {
bc15f5471b6a new CLI interface: --relation, --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    49
		if (value == L"relation") return RelationConfiguration::ENTITY::RELATION;
bc15f5471b6a new CLI interface: --relation, --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    50
		else if (value == L"attribute") return RelationConfiguration::ENTITY::ATTRIBUTE;
bc15f5471b6a new CLI interface: --relation, --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    51
		else throw relpipe::cli::RelpipeCLIException(L"Unable to parse entity value: " + value + L" (expecting „relation“, „attribute“ or „value“)", relpipe::cli::CLI::EXIT_CODE_BAD_CLI_ARGUMENTS);
bc15f5471b6a new CLI interface: --relation, --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    52
	}
bc15f5471b6a new CLI interface: --relation, --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    53
bc15f5471b6a new CLI interface: --relation, --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    54
	void addRelation(Configuration& c, RelationConfiguration& currentRelation) {
bc15f5471b6a new CLI interface: --relation, --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    55
		if (currentRelation.relation.size()) {
bc15f5471b6a new CLI interface: --relation, --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    56
			using E = RelationConfiguration::ENTITY;
bc15f5471b6a new CLI interface: --relation, --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    57
			currentRelation.relationPattern = currentRelation.caseSensitive[E::RELATION] ? std::wregex(currentRelation.relation) : std::wregex(currentRelation.relation, std::regex_constants::icase);
bc15f5471b6a new CLI interface: --relation, --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    58
			for (const relpipe::common::type::StringX& attribute : currentRelation.attributes) currentRelation.attributePatterns.push_back(currentRelation.caseSensitive[E::ATTRIBUTE] ? std::wregex(attribute) : std::wregex(attribute, std::regex_constants::icase));
bc15f5471b6a new CLI interface: --relation, --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    59
			c.relationConfigurations.push_back(currentRelation);
bc15f5471b6a new CLI interface: --relation, --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    60
			currentRelation = RelationConfiguration();
bc15f5471b6a new CLI interface: --relation, --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    61
		}
bc15f5471b6a new CLI interface: --relation, --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    62
	}
bc15f5471b6a new CLI interface: --relation, --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    63
bc15f5471b6a new CLI interface: --relation, --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    64
public:
bc15f5471b6a new CLI interface: --relation, --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    65
bc15f5471b6a new CLI interface: --relation, --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    66
	static const relpipe::common::type::StringX OPTION_RELATION;
bc15f5471b6a new CLI interface: --relation, --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    67
	static const relpipe::common::type::StringX OPTION_ATTRIBUTE;
bc15f5471b6a new CLI interface: --relation, --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    68
	static const relpipe::common::type::StringX OPTION_CASE_SENSITIVE;
bc15f5471b6a new CLI interface: --relation, --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    69
	static const relpipe::common::type::StringX OPTION_INVERT_MATCH;
bc15f5471b6a new CLI interface: --relation, --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    70
bc15f5471b6a new CLI interface: --relation, --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    71
	Configuration parse(const std::vector<relpipe::common::type::StringX>& arguments) {
bc15f5471b6a new CLI interface: --relation, --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    72
		Configuration c;
bc15f5471b6a new CLI interface: --relation, --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    73
		RelationConfiguration currentRelation;
bc15f5471b6a new CLI interface: --relation, --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    74
bc15f5471b6a new CLI interface: --relation, --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    75
		for (int i = 0; i < arguments.size();) {
bc15f5471b6a new CLI interface: --relation, --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    76
			relpipe::common::type::StringX option = readNext(arguments, i);
bc15f5471b6a new CLI interface: --relation, --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    77
bc15f5471b6a new CLI interface: --relation, --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    78
			if (option == OPTION_RELATION) {
bc15f5471b6a new CLI interface: --relation, --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    79
				addRelation(c, currentRelation); // previous relation
bc15f5471b6a new CLI interface: --relation, --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    80
				currentRelation.relation = readNext(arguments, i);
bc15f5471b6a new CLI interface: --relation, --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    81
			} else if (option == OPTION_ATTRIBUTE) {
bc15f5471b6a new CLI interface: --relation, --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    82
				currentRelation.attributes.push_back(readNext(arguments, i));
bc15f5471b6a new CLI interface: --relation, --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    83
			} else if (option == OPTION_CASE_SENSITIVE) {
bc15f5471b6a new CLI interface: --relation, --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    84
				RelationConfiguration::ENTITY entity = parseEntity(readNext(arguments, i));
bc15f5471b6a new CLI interface: --relation, --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    85
				currentRelation.caseSensitive[entity] = parseBoolean(readNext(arguments, i));
bc15f5471b6a new CLI interface: --relation, --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    86
			} else if (option == OPTION_INVERT_MATCH) {
bc15f5471b6a new CLI interface: --relation, --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    87
				RelationConfiguration::ENTITY entity = parseEntity(readNext(arguments, i));
bc15f5471b6a new CLI interface: --relation, --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    88
				currentRelation.invertMatch[entity] = parseBoolean(readNext(arguments, i));
bc15f5471b6a new CLI interface: --relation, --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    89
			} else throw relpipe::cli::RelpipeCLIException(L"Unsupported CLI option: " + option, relpipe::cli::CLI::EXIT_CODE_BAD_CLI_ARGUMENTS);
bc15f5471b6a new CLI interface: --relation, --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    90
		}
bc15f5471b6a new CLI interface: --relation, --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    91
		addRelation(c, currentRelation); // last relation
bc15f5471b6a new CLI interface: --relation, --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    92
bc15f5471b6a new CLI interface: --relation, --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    93
		return c;
bc15f5471b6a new CLI interface: --relation, --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    94
	}
bc15f5471b6a new CLI interface: --relation, --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    95
bc15f5471b6a new CLI interface: --relation, --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    96
	virtual ~CLIParser() {
bc15f5471b6a new CLI interface: --relation, --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    97
	}
bc15f5471b6a new CLI interface: --relation, --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    98
};
bc15f5471b6a new CLI interface: --relation, --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    99
bc15f5471b6a new CLI interface: --relation, --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   100
const relpipe::common::type::StringX CLIParser::OPTION_RELATION = L"--relation";
bc15f5471b6a new CLI interface: --relation, --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   101
const relpipe::common::type::StringX CLIParser::OPTION_ATTRIBUTE = L"--attribute";
bc15f5471b6a new CLI interface: --relation, --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   102
const relpipe::common::type::StringX CLIParser::OPTION_CASE_SENSITIVE = L"--case-sensitive";
bc15f5471b6a new CLI interface: --relation, --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   103
const relpipe::common::type::StringX CLIParser::OPTION_INVERT_MATCH = L"--invert-match";
bc15f5471b6a new CLI interface: --relation, --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   104
bc15f5471b6a new CLI interface: --relation, --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   105
}
bc15f5471b6a new CLI interface: --relation, --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   106
}
bc15f5471b6a new CLI interface: --relation, --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   107
}