src/CLIParser.h
author František Kučera <franta-hg@frantovo.cz>
Tue, 05 Feb 2019 12:41:54 +0100
branchv_0
changeset 14 82bd0f57a889
parent 13 c9fece435aa2
child 15 051e58022783
permissions -rw-r--r--
add --drop option: all Guile code will be executed but not relational output for given relation will be generated
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
12
7977c1bdba1f add --output-attribute so output relation can have different attributes than the input relation
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
    35
	// FIXME: move common methods/classes to relpipe-lib-cli or relpipe-lib-helper
6
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
12
7977c1bdba1f add --output-attribute so output relation can have different attributes than the input relation
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
    49
	relpipe::writer::TypeId parseTypeId(const string_t& value) {
7977c1bdba1f add --output-attribute so output relation can have different attributes than the input relation
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
    50
		using t = relpipe::writer::TypeId;
7977c1bdba1f add --output-attribute so output relation can have different attributes than the input relation
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
    51
		if (value == L"string") return t::STRING;
7977c1bdba1f add --output-attribute so output relation can have different attributes than the input relation
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
    52
		else if (value == L"integer") return t::INTEGER;
7977c1bdba1f add --output-attribute so output relation can have different attributes than the input relation
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
    53
		else if (value == L"boolean") return t::BOOLEAN;
7977c1bdba1f add --output-attribute so output relation can have different attributes than the input relation
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
    54
		else throw relpipe::cli::RelpipeCLIException(L"Unable to parse TypeId: " + value, relpipe::cli::CLI::EXIT_CODE_BAD_CLI_ARGUMENTS);
7977c1bdba1f add --output-attribute so output relation can have different attributes than the input relation
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
    55
	}
7977c1bdba1f add --output-attribute so output relation can have different attributes than the input relation
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
    56
6
4062b8436838 CLI parser: first version (only single relation is supported)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    57
public:
4062b8436838 CLI parser: first version (only single relation is supported)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    58
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_RELATION;
12
7977c1bdba1f add --output-attribute so output relation can have different attributes than the input relation
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
    60
	static const string_t OPTION_OUTPUT_ATTRIBUTE;
6
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_BEFORE_RECORDS;
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_AFTER_RECORDS;
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_FOR_EACH;
4062b8436838 CLI parser: first version (only single relation is supported)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    64
	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
    65
	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
    66
	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
    67
4062b8436838 CLI parser: first version (only single relation is supported)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    68
	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
    69
		Configuration c;
4062b8436838 CLI parser: first version (only single relation is supported)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    70
		RelationConfiguration currentRelation;
4062b8436838 CLI parser: first version (only single relation is supported)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    71
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
			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
    74
				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
    75
4062b8436838 CLI parser: first version (only single relation is supported)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    76
				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
    77
				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
    78
				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
    79
				else if (option == OPTION_WHERE) currentRelation.guileWhere = readNext(arguments, i);
14
82bd0f57a889 add --drop option: all Guile code will be executed but not relational output for given relation will be generated
František Kučera <franta-hg@frantovo.cz>
parents: 13
diff changeset
    80
				else if (option == OPTION_DROP) currentRelation.drop = true;
6
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_RELATION) {
4062b8436838 CLI parser: first version (only single relation is supported)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    82
					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
    83
					currentRelation.relation = readNext(arguments, i);
12
7977c1bdba1f add --output-attribute so output relation can have different attributes than the input relation
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
    84
				} else if (option == OPTION_OUTPUT_ATTRIBUTE) {
7977c1bdba1f add --output-attribute so output relation can have different attributes than the input relation
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
    85
					relpipe::writer::AttributeMetadata attribute;
7977c1bdba1f add --output-attribute so output relation can have different attributes than the input relation
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
    86
					attribute.attributeName = readNext(arguments, i);
7977c1bdba1f add --output-attribute so output relation can have different attributes than the input relation
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
    87
					attribute.typeId = parseTypeId(readNext(arguments, i));
7977c1bdba1f add --output-attribute so output relation can have different attributes than the input relation
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
    88
					currentRelation.writerMetadata.push_back(attribute);
6
4062b8436838 CLI parser: first version (only single relation is supported)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    89
				} 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
    90
					DefinitionRecipe definition;
4062b8436838 CLI parser: first version (only single relation is supported)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    91
					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
    92
					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
    93
					definition.value = readNext(arguments, i);
13
c9fece435aa2 add --define for global variables (can be overridden by relation's ones)
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
    94
					if (currentRelation.relation.size()) currentRelation.definitions.push_back(definition);
c9fece435aa2 add --define for global variables (can be overridden by relation's ones)
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
    95
					else c.definitions.push_back(definition);
6
4062b8436838 CLI parser: first version (only single relation is supported)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    96
				} 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
    97
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
			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
   101
		}
4062b8436838 CLI parser: first version (only single relation is supported)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   102
4062b8436838 CLI parser: first version (only single relation is supported)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   103
		return c;
4062b8436838 CLI parser: first version (only single relation is supported)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   104
	}
4062b8436838 CLI parser: first version (only single relation is supported)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   105
4062b8436838 CLI parser: first version (only single relation is supported)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   106
	virtual ~CLIParser() {
4062b8436838 CLI parser: first version (only single relation is supported)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   107
	}
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
const string_t CLIParser::OPTION_RELATION = L"--relation";
12
7977c1bdba1f add --output-attribute so output relation can have different attributes than the input relation
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
   111
const string_t CLIParser::OPTION_OUTPUT_ATTRIBUTE = L"--output-attribute";
6
4062b8436838 CLI parser: first version (only single relation is supported)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   112
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
   113
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
   114
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
   115
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
   116
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
   117
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
   118
4062b8436838 CLI parser: first version (only single relation is supported)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   119
}
4062b8436838 CLI parser: first version (only single relation is supported)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   120
}
4062b8436838 CLI parser: first version (only single relation is supported)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   121
}