src/CLIParser.h
author František Kučera <franta-hg@frantovo.cz>
Tue, 11 May 2021 18:30:50 +0200
branchv_0
changeset 24 c69670b7b4ef
child 25 98be80d2e65b
permissions -rw-r--r--
new CLI interface: --relation, --attribute, --pattern
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
c69670b7b4ef new CLI interface: --relation, --attribute, --pattern
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    39
	void addRelation(Configuration& c, RelationConfiguration& currentRelation) {
c69670b7b4ef new CLI interface: --relation, --attribute, --pattern
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    40
		if (currentRelation.relation.size()) {
c69670b7b4ef new CLI interface: --relation, --attribute, --pattern
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    41
			c.relationConfigurations.push_back(currentRelation);
c69670b7b4ef new CLI interface: --relation, --attribute, --pattern
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    42
			currentRelation = RelationConfiguration();
c69670b7b4ef new CLI interface: --relation, --attribute, --pattern
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    43
		}
c69670b7b4ef new CLI interface: --relation, --attribute, --pattern
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    44
	}
c69670b7b4ef new CLI interface: --relation, --attribute, --pattern
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    45
c69670b7b4ef new CLI interface: --relation, --attribute, --pattern
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    46
public:
c69670b7b4ef new CLI interface: --relation, --attribute, --pattern
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    47
c69670b7b4ef new CLI interface: --relation, --attribute, --pattern
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    48
	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
    49
	static const relpipe::common::type::StringX OPTION_ATTRIBUTE;
c69670b7b4ef new CLI interface: --relation, --attribute, --pattern
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    50
	static const relpipe::common::type::StringX OPTION_PATTERN;
c69670b7b4ef new CLI interface: --relation, --attribute, --pattern
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    51
c69670b7b4ef new CLI interface: --relation, --attribute, --pattern
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    52
	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
    53
		Configuration c;
c69670b7b4ef new CLI interface: --relation, --attribute, --pattern
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    54
		RelationConfiguration currentRelation;
c69670b7b4ef new CLI interface: --relation, --attribute, --pattern
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    55
c69670b7b4ef new CLI interface: --relation, --attribute, --pattern
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    56
		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
    57
			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
    58
c69670b7b4ef new CLI interface: --relation, --attribute, --pattern
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    59
			if (option == OPTION_RELATION) {
c69670b7b4ef new CLI interface: --relation, --attribute, --pattern
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    60
				addRelation(c, currentRelation); // previous relation
c69670b7b4ef new CLI interface: --relation, --attribute, --pattern
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    61
				currentRelation.relation = readNext(arguments, i);
c69670b7b4ef new CLI interface: --relation, --attribute, --pattern
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    62
				currentRelation.relationPattern = std::wregex(currentRelation.relation);
c69670b7b4ef new CLI interface: --relation, --attribute, --pattern
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    63
			} else if (option == OPTION_ATTRIBUTE) {
c69670b7b4ef new CLI interface: --relation, --attribute, --pattern
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    64
				currentRelation.attributePattern = std::wregex(readNext(arguments, i));
c69670b7b4ef new CLI interface: --relation, --attribute, --pattern
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    65
			} else if (option == OPTION_PATTERN) {
c69670b7b4ef new CLI interface: --relation, --attribute, --pattern
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    66
				currentRelation.valuePattern = std::wregex(readNext(arguments, i));
c69670b7b4ef new CLI interface: --relation, --attribute, --pattern
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    67
			} 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
    68
		}
c69670b7b4ef new CLI interface: --relation, --attribute, --pattern
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    69
		addRelation(c, currentRelation); // last relation
c69670b7b4ef new CLI interface: --relation, --attribute, --pattern
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    70
c69670b7b4ef new CLI interface: --relation, --attribute, --pattern
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    71
		return c;
c69670b7b4ef new CLI interface: --relation, --attribute, --pattern
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    72
	}
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
	virtual ~CLIParser() {
c69670b7b4ef new CLI interface: --relation, --attribute, --pattern
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    75
	}
c69670b7b4ef new CLI interface: --relation, --attribute, --pattern
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    76
};
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
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
    79
const relpipe::common::type::StringX CLIParser::OPTION_ATTRIBUTE = L"--attribute";
c69670b7b4ef new CLI interface: --relation, --attribute, --pattern
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    80
const relpipe::common::type::StringX CLIParser::OPTION_PATTERN = L"--pattern";
c69670b7b4ef new CLI interface: --relation, --attribute, --pattern
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    81
c69670b7b4ef new CLI interface: --relation, --attribute, --pattern
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    82
}
c69670b7b4ef new CLI interface: --relation, --attribute, --pattern
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    83
}
c69670b7b4ef new CLI interface: --relation, --attribute, --pattern
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    84
}