src/CLIParser.h
author František Kučera <franta-hg@frantovo.cz>
Sun, 23 May 2021 15:51:43 +0200
branchv_0
changeset 3 9f2d6645ebe6
parent 0 5d1c556ff7db
permissions -rw-r--r--
use MODE::AUTO as default when no explicit configuration is present
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
5d1c556ff7db project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     1
/**
5d1c556ff7db project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     2
 * Relational pipes
5d1c556ff7db project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     3
 * Copyright © 2021 František Kučera (Frantovo.cz, GlobalCode.info)
5d1c556ff7db project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     4
 *
5d1c556ff7db 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
5d1c556ff7db 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
5d1c556ff7db project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     7
 * the Free Software Foundation, version 3 of the License.
5d1c556ff7db project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     8
 *
5d1c556ff7db 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,
5d1c556ff7db project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
5d1c556ff7db project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
5d1c556ff7db project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    12
 * GNU General Public License for more details.
5d1c556ff7db project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    13
 *
5d1c556ff7db 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
5d1c556ff7db 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/>.
5d1c556ff7db project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    16
 */
5d1c556ff7db project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    17
#pragma once
5d1c556ff7db project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    18
5d1c556ff7db project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    19
#include <vector>
5d1c556ff7db project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    20
5d1c556ff7db project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    21
#include <relpipe/common/type/typedefs.h>
5d1c556ff7db project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    22
#include <relpipe/cli/CLI.h>
5d1c556ff7db project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    23
#include <relpipe/cli/RelpipeCLIException.h>
5d1c556ff7db project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    24
5d1c556ff7db project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    25
#include "Configuration.h"
5d1c556ff7db project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    26
5d1c556ff7db project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    27
namespace relpipe {
5d1c556ff7db project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    28
namespace tr {
5d1c556ff7db project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    29
namespace infertypes {
5d1c556ff7db project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    30
5d1c556ff7db project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    31
class CLIParser {
5d1c556ff7db project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    32
private:
5d1c556ff7db project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    33
5d1c556ff7db 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) {
5d1c556ff7db project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    35
		if (i < arguments.size()) return arguments[i++];
5d1c556ff7db 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);
5d1c556ff7db project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    37
	}
5d1c556ff7db project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    38
5d1c556ff7db project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    39
	MODE parseMode(const relpipe::common::type::StringX& value) {
5d1c556ff7db project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    40
		if (value == L"auto") return MODE::AUTO;
5d1c556ff7db project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    41
		else if (value == L"metadata") return MODE::METADATA;
5d1c556ff7db project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    42
		else if (value == L"data") return MODE::DATA;
5d1c556ff7db project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    43
		else throw relpipe::cli::RelpipeCLIException(L"Unable to parse mode value: " + value, relpipe::cli::CLI::EXIT_CODE_BAD_CLI_ARGUMENTS);
5d1c556ff7db project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    44
	}
5d1c556ff7db project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    45
5d1c556ff7db project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    46
	void addRelation(Configuration& c, RelationConfiguration& currentRelation) {
5d1c556ff7db project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    47
		if (currentRelation.relation.size()) {
5d1c556ff7db project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    48
			currentRelation.relationPattern = std::wregex(currentRelation.relation);
5d1c556ff7db project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    49
			c.relationConfigurations.push_back(currentRelation);
5d1c556ff7db project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    50
			currentRelation = RelationConfiguration();
5d1c556ff7db project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    51
		}
5d1c556ff7db project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    52
	}
5d1c556ff7db project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    53
5d1c556ff7db project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    54
public:
5d1c556ff7db project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    55
5d1c556ff7db project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    56
	static const relpipe::common::type::StringX OPTION_RELATION;
5d1c556ff7db project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    57
	static const relpipe::common::type::StringX OPTION_MODE;
5d1c556ff7db project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    58
5d1c556ff7db project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    59
	Configuration parse(const std::vector<relpipe::common::type::StringX>& arguments) {
5d1c556ff7db project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    60
		Configuration c;
5d1c556ff7db project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    61
		RelationConfiguration currentRelation;
5d1c556ff7db project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    62
5d1c556ff7db project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    63
		for (int i = 0; i < arguments.size();) {
5d1c556ff7db project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    64
			relpipe::common::type::StringX option = readNext(arguments, i);
5d1c556ff7db project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    65
5d1c556ff7db project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    66
			if (option == OPTION_RELATION) {
5d1c556ff7db project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    67
				addRelation(c, currentRelation); // previous relation
5d1c556ff7db project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    68
				currentRelation.relation = readNext(arguments, i);
5d1c556ff7db project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    69
			} else if (option == OPTION_MODE) {
5d1c556ff7db project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    70
				currentRelation.mode = parseMode(readNext(arguments, i));
5d1c556ff7db project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    71
			} else throw relpipe::cli::RelpipeCLIException(L"Unsupported CLI option: " + option, relpipe::cli::CLI::EXIT_CODE_BAD_CLI_ARGUMENTS);
5d1c556ff7db project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    72
		}
5d1c556ff7db project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    73
		addRelation(c, currentRelation); // last relation
5d1c556ff7db project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    74
3
9f2d6645ebe6 use MODE::AUTO as default when no explicit configuration is present
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    75
		if (c.relationConfigurations.size() == 0) c.relationConfigurations.push_back({L".*", MODE::AUTO});
9f2d6645ebe6 use MODE::AUTO as default when no explicit configuration is present
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    76
0
5d1c556ff7db project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    77
		return c;
5d1c556ff7db project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    78
	}
5d1c556ff7db project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    79
5d1c556ff7db project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    80
	virtual ~CLIParser() {
5d1c556ff7db project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    81
	}
5d1c556ff7db project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    82
};
5d1c556ff7db project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    83
5d1c556ff7db project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    84
const relpipe::common::type::StringX CLIParser::OPTION_RELATION = L"--relation";
5d1c556ff7db project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    85
const relpipe::common::type::StringX CLIParser::OPTION_MODE = L"--mode";
5d1c556ff7db project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    86
5d1c556ff7db project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    87
}
5d1c556ff7db project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    88
}
5d1c556ff7db project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    89
}