src/CLIParser.h
author František Kučera <franta-hg@frantovo.cz>
Sun, 03 Feb 2019 01:44:07 +0100
branchv_0
changeset 6 4062b8436838
child 12 7977c1bdba1f
permissions -rw-r--r--
CLI parser: first version (only single relation is supported)
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
6
4062b8436838 CLI parser: first version (only single relation is supported)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     1
/**
4062b8436838 CLI parser: first version (only single relation is supported)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     2
 * Relational pipes
4062b8436838 CLI parser: first version (only single relation is supported)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     3
 * Copyright © 2019 František Kučera (Frantovo.cz, GlobalCode.info)
4062b8436838 CLI parser: first version (only single relation is supported)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     4
 *
4062b8436838 CLI parser: first version (only single relation is supported)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     5
 * This program is free software: you can redistribute it and/or modify
4062b8436838 CLI parser: first version (only single relation is supported)
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
4062b8436838 CLI parser: first version (only single relation is supported)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     7
 * the Free Software Foundation, either version 3 of the License, or
4062b8436838 CLI parser: first version (only single relation is supported)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     8
 * (at your option) any later version.
4062b8436838 CLI parser: first version (only single relation is supported)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     9
 *
4062b8436838 CLI parser: first version (only single relation is supported)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    10
 * This program is distributed in the hope that it will be useful,
4062b8436838 CLI parser: first version (only single relation is supported)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
4062b8436838 CLI parser: first version (only single relation is supported)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
4062b8436838 CLI parser: first version (only single relation is supported)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    13
 * GNU General Public License for more details.
4062b8436838 CLI parser: first version (only single relation is supported)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    14
 *
4062b8436838 CLI parser: first version (only single relation is supported)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License
4062b8436838 CLI parser: first version (only single relation is supported)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    16
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
4062b8436838 CLI parser: first version (only single relation is supported)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    17
 */
4062b8436838 CLI parser: first version (only single relation is supported)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    18
#pragma once
4062b8436838 CLI parser: first version (only single relation is supported)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    19
4062b8436838 CLI parser: first version (only single relation is supported)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    20
#include <vector>
4062b8436838 CLI parser: first version (only single relation is supported)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    21
4062b8436838 CLI parser: first version (only single relation is supported)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    22
#include <relpipe/writer/typedefs.h>
4062b8436838 CLI parser: first version (only single relation is supported)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    23
#include <relpipe/cli/CLI.h>
4062b8436838 CLI parser: first version (only single relation is supported)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    24
#include <relpipe/cli/RelpipeCLIException.h>
4062b8436838 CLI parser: first version (only single relation is supported)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    25
4062b8436838 CLI parser: first version (only single relation is supported)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    26
#include "Configuration.h"
4062b8436838 CLI parser: first version (only single relation is supported)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    27
4062b8436838 CLI parser: first version (only single relation is supported)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    28
namespace relpipe {
4062b8436838 CLI parser: first version (only single relation is supported)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    29
namespace tr {
4062b8436838 CLI parser: first version (only single relation is supported)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    30
namespace guile {
4062b8436838 CLI parser: first version (only single relation is supported)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    31
4062b8436838 CLI parser: first version (only single relation is supported)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    32
class CLIParser {
4062b8436838 CLI parser: first version (only single relation is supported)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    33
private:
4062b8436838 CLI parser: first version (only single relation is supported)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    34
4062b8436838 CLI parser: first version (only single relation is supported)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    35
	// TODO: move common methods/classes to relpipe-lib-cli
4062b8436838 CLI parser: first version (only single relation is supported)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    36
4062b8436838 CLI parser: first version (only single relation is supported)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    37
	string_t readNext(std::vector<string_t> arguments, int& i) {
4062b8436838 CLI parser: first version (only single relation is supported)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    38
		if (i < arguments.size()) return arguments[i++];
4062b8436838 CLI parser: first version (only single relation is supported)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    39
		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);
4062b8436838 CLI parser: first version (only single relation is supported)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    40
	}
4062b8436838 CLI parser: first version (only single relation is supported)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    41
4062b8436838 CLI parser: first version (only single relation is supported)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    42
	void addRelation(Configuration& c, RelationConfiguration& currentRelation) {
4062b8436838 CLI parser: first version (only single relation is supported)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    43
		if (currentRelation.relation.size()) {
4062b8436838 CLI parser: first version (only single relation is supported)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    44
			c.relationConfigurations.push_back(currentRelation);
4062b8436838 CLI parser: first version (only single relation is supported)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    45
			currentRelation = RelationConfiguration();
4062b8436838 CLI parser: first version (only single relation is supported)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    46
		}
4062b8436838 CLI parser: first version (only single relation is supported)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    47
	}
4062b8436838 CLI parser: first version (only single relation is supported)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    48
4062b8436838 CLI parser: first version (only single relation is supported)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    49
	bool parseBoolean(const string_t& value, const string_t& optionName) {
4062b8436838 CLI parser: first version (only single relation is supported)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    50
		if (value == L"true") return true;
4062b8436838 CLI parser: first version (only single relation is supported)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    51
		else if (value == L"false") return false;
4062b8436838 CLI parser: first version (only single relation is supported)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    52
		else throw relpipe::cli::RelpipeCLIException(L"Unable to parse boolean value of option: " + optionName + L" (expecting true or false)", relpipe::cli::CLI::EXIT_CODE_BAD_CLI_ARGUMENTS);
4062b8436838 CLI parser: first version (only single relation is supported)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    53
	}
4062b8436838 CLI parser: first version (only single relation is supported)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    54
4062b8436838 CLI parser: first version (only single relation is supported)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    55
public:
4062b8436838 CLI parser: first version (only single relation is supported)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    56
4062b8436838 CLI parser: first version (only single relation is supported)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    57
	static const string_t OPTION_RELATION;
4062b8436838 CLI parser: first version (only single relation is supported)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    58
	static const string_t OPTION_BEFORE_RECORDS;
4062b8436838 CLI parser: first version (only single relation is supported)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    59
	static const string_t OPTION_AFTER_RECORDS;
4062b8436838 CLI parser: first version (only single relation is supported)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    60
	static const string_t OPTION_FOR_EACH;
4062b8436838 CLI parser: first version (only single relation is supported)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    61
	static const string_t OPTION_WHERE;
4062b8436838 CLI parser: first version (only single relation is supported)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    62
	static const string_t OPTION_DROP;
4062b8436838 CLI parser: first version (only single relation is supported)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    63
	static const string_t OPTION_DEFINE;
4062b8436838 CLI parser: first version (only single relation is supported)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    64
4062b8436838 CLI parser: first version (only single relation is supported)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    65
	Configuration parse(const std::vector<string_t>& arguments) {
4062b8436838 CLI parser: first version (only single relation is supported)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    66
		Configuration c;
4062b8436838 CLI parser: first version (only single relation is supported)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    67
		RelationConfiguration currentRelation;
4062b8436838 CLI parser: first version (only single relation is supported)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    68
4062b8436838 CLI parser: first version (only single relation is supported)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    69
		{
4062b8436838 CLI parser: first version (only single relation is supported)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    70
			for (int i = 0; i < arguments.size();) {
4062b8436838 CLI parser: first version (only single relation is supported)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    71
				string_t option = readNext(arguments, i);
4062b8436838 CLI parser: first version (only single relation is supported)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    72
4062b8436838 CLI parser: first version (only single relation is supported)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    73
				if (option == OPTION_BEFORE_RECORDS) currentRelation.guileBeforeRecords = readNext(arguments, i);
4062b8436838 CLI parser: first version (only single relation is supported)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    74
				else if (option == OPTION_AFTER_RECORDS) currentRelation.guileAfterRecords = readNext(arguments, i);
4062b8436838 CLI parser: first version (only single relation is supported)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    75
				else if (option == OPTION_FOR_EACH) currentRelation.guileForEach = readNext(arguments, i);
4062b8436838 CLI parser: first version (only single relation is supported)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    76
				else if (option == OPTION_WHERE) currentRelation.guileWhere = readNext(arguments, i);
4062b8436838 CLI parser: first version (only single relation is supported)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    77
				else if (option == OPTION_DROP) currentRelation.drop = parseBoolean(readNext(arguments, i), option);
4062b8436838 CLI parser: first version (only single relation is supported)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    78
				else if (option == OPTION_RELATION) {
4062b8436838 CLI parser: first version (only single relation is supported)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    79
					addRelation(c, currentRelation); // previous relation
4062b8436838 CLI parser: first version (only single relation is supported)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    80
					currentRelation.relation = readNext(arguments, i);
4062b8436838 CLI parser: first version (only single relation is supported)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    81
				} else if (option == OPTION_DEFINE) {
4062b8436838 CLI parser: first version (only single relation is supported)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    82
					DefinitionRecipe definition;
4062b8436838 CLI parser: first version (only single relation is supported)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    83
					definition.name = readNext(arguments, i);
4062b8436838 CLI parser: first version (only single relation is supported)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    84
					definition.type = readNext(arguments, i);
4062b8436838 CLI parser: first version (only single relation is supported)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    85
					definition.value = readNext(arguments, i);
4062b8436838 CLI parser: first version (only single relation is supported)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    86
					currentRelation.definitions.push_back(definition);
4062b8436838 CLI parser: first version (only single relation is supported)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    87
				} else throw relpipe::cli::RelpipeCLIException(L"Unsupported CLI option: " + option, relpipe::cli::CLI::EXIT_CODE_BAD_CLI_ARGUMENTS);
4062b8436838 CLI parser: first version (only single relation is supported)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    88
4062b8436838 CLI parser: first version (only single relation is supported)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    89
			}
4062b8436838 CLI parser: first version (only single relation is supported)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    90
4062b8436838 CLI parser: first version (only single relation is supported)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    91
			addRelation(c, currentRelation); // last relation
4062b8436838 CLI parser: first version (only single relation is supported)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    92
		}
4062b8436838 CLI parser: first version (only single relation is supported)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    93
4062b8436838 CLI parser: first version (only single relation is supported)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    94
		return c;
4062b8436838 CLI parser: first version (only single relation is supported)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    95
	}
4062b8436838 CLI parser: first version (only single relation is supported)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    96
4062b8436838 CLI parser: first version (only single relation is supported)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    97
	virtual ~CLIParser() {
4062b8436838 CLI parser: first version (only single relation is supported)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    98
	}
4062b8436838 CLI parser: first version (only single relation is supported)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    99
};
4062b8436838 CLI parser: first version (only single relation is supported)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   100
4062b8436838 CLI parser: first version (only single relation is supported)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   101
const string_t CLIParser::OPTION_RELATION = L"--relation";
4062b8436838 CLI parser: first version (only single relation is supported)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   102
const string_t CLIParser::OPTION_BEFORE_RECORDS = L"--before-records";
4062b8436838 CLI parser: first version (only single relation is supported)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   103
const string_t CLIParser::OPTION_AFTER_RECORDS = L"--after-records";
4062b8436838 CLI parser: first version (only single relation is supported)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   104
const string_t CLIParser::OPTION_FOR_EACH = L"--for-each";
4062b8436838 CLI parser: first version (only single relation is supported)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   105
const string_t CLIParser::OPTION_WHERE = L"--where";
4062b8436838 CLI parser: first version (only single relation is supported)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   106
const string_t CLIParser::OPTION_DROP = L"--drop";
4062b8436838 CLI parser: first version (only single relation is supported)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   107
const string_t CLIParser::OPTION_DEFINE = L"--define";
4062b8436838 CLI parser: first version (only single relation is supported)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   108
4062b8436838 CLI parser: first version (only single relation is supported)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   109
}
4062b8436838 CLI parser: first version (only single relation is supported)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   110
}
4062b8436838 CLI parser: first version (only single relation is supported)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   111
}