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