src/CLIParser.h
author František Kučera <franta-hg@frantovo.cz>
Tue, 29 Oct 2019 12:09:38 +0100
branchv_0
changeset 37 0c050899c77f
parent 35 eafffeea6a3e
permissions -rw-r--r--
add --where option: similar to --for-each but better expresses the intention: filtering The --for-each is more universal and can be used for both transformations and filtering.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
644fd2ce2580 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     1
/**
644fd2ce2580 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     2
 * Relational pipes
644fd2ce2580 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     3
 * Copyright © 2019 František Kučera (Frantovo.cz, GlobalCode.info)
644fd2ce2580 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     4
 *
644fd2ce2580 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
644fd2ce2580 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
35
eafffeea6a3e fix license version: GNU GPLv3
František Kučera <franta-hg@frantovo.cz>
parents: 26
diff changeset
     7
 * the Free Software Foundation, version 3 of the License.
0
644fd2ce2580 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     8
 *
644fd2ce2580 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,
644fd2ce2580 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
644fd2ce2580 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
644fd2ce2580 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    12
 * GNU General Public License for more details.
644fd2ce2580 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    13
 *
644fd2ce2580 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
644fd2ce2580 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/>.
644fd2ce2580 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    16
 */
644fd2ce2580 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    17
#pragma once
644fd2ce2580 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    18
644fd2ce2580 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    19
#include <vector>
644fd2ce2580 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    20
644fd2ce2580 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    21
#include <relpipe/writer/typedefs.h>
644fd2ce2580 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    22
#include <relpipe/cli/CLI.h>
644fd2ce2580 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    23
#include <relpipe/cli/RelpipeCLIException.h>
644fd2ce2580 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    24
644fd2ce2580 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    25
#include "Configuration.h"
644fd2ce2580 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    26
644fd2ce2580 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    27
namespace relpipe {
644fd2ce2580 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    28
namespace tr {
644fd2ce2580 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    29
namespace awk {
644fd2ce2580 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    30
8
513ea30d2fa3 more configuration/options
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    31
/**
513ea30d2fa3 more configuration/options
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    32
 * This tr-awk CLI options are inspired by tr-guile options.
26
cf57e8c78492 add option: --debug-variable-mapping
František Kučera <franta-hg@frantovo.cz>
parents: 19
diff changeset
    33
 * These configurations are independent and might diverge, but when possible, they should share same option names and same logic for common parts.
8
513ea30d2fa3 more configuration/options
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    34
 */
0
644fd2ce2580 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    35
class CLIParser {
644fd2ce2580 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    36
private:
644fd2ce2580 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    37
644fd2ce2580 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    38
	// FIXME: move common methods/classes to relpipe-lib-cli or relpipe-lib-helper
644fd2ce2580 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    39
644fd2ce2580 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    40
	string_t readNext(std::vector<string_t> arguments, int& i) {
644fd2ce2580 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    41
		if (i < arguments.size()) return arguments[i++];
644fd2ce2580 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    42
		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);
644fd2ce2580 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    43
	}
644fd2ce2580 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    44
8
513ea30d2fa3 more configuration/options
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    45
	void addRelation(Configuration& c, RelationConfiguration& currentRelation) {
513ea30d2fa3 more configuration/options
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    46
		if (currentRelation.relation.size()) {
513ea30d2fa3 more configuration/options
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    47
			c.relationConfigurations.push_back(currentRelation);
513ea30d2fa3 more configuration/options
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    48
			currentRelation = RelationConfiguration();
513ea30d2fa3 more configuration/options
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    49
		}
513ea30d2fa3 more configuration/options
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    50
	}
513ea30d2fa3 more configuration/options
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    51
513ea30d2fa3 more configuration/options
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    52
	relpipe::writer::TypeId parseTypeId(const string_t& value) {
513ea30d2fa3 more configuration/options
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    53
		using t = relpipe::writer::TypeId;
513ea30d2fa3 more configuration/options
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    54
		if (value == L"string") return t::STRING;
513ea30d2fa3 more configuration/options
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    55
		else if (value == L"integer") return t::INTEGER;
513ea30d2fa3 more configuration/options
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    56
		else if (value == L"boolean") return t::BOOLEAN;
513ea30d2fa3 more configuration/options
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    57
		else throw relpipe::cli::RelpipeCLIException(L"Unable to parse TypeId: " + value, relpipe::cli::CLI::EXIT_CODE_BAD_CLI_ARGUMENTS);
513ea30d2fa3 more configuration/options
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    58
	}
513ea30d2fa3 more configuration/options
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    59
0
644fd2ce2580 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    60
public:
644fd2ce2580 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    61
8
513ea30d2fa3 more configuration/options
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    62
	static const string_t OPTION_RELATION;
513ea30d2fa3 more configuration/options
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    63
	static const string_t OPTION_OUTPUT_ATTRIBUTE;
513ea30d2fa3 more configuration/options
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    64
	static const string_t OPTION_INPUT_ATTRIBUTES_APPEND;
513ea30d2fa3 more configuration/options
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    65
	static const string_t OPTION_INPUT_ATTRIBUTES_PREPEND;
26
cf57e8c78492 add option: --debug-variable-mapping
František Kučera <franta-hg@frantovo.cz>
parents: 19
diff changeset
    66
	static const string_t OPTION_DEBUG_VARIABLE_MAPPING;
8
513ea30d2fa3 more configuration/options
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    67
	static const string_t OPTION_BEFORE_RECORDS;
513ea30d2fa3 more configuration/options
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    68
	static const string_t OPTION_AFTER_RECORDS;
513ea30d2fa3 more configuration/options
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    69
	static const string_t OPTION_FOR_EACH;
37
0c050899c77f add --where option: similar to --for-each but better expresses the intention: filtering
František Kučera <franta-hg@frantovo.cz>
parents: 35
diff changeset
    70
	static const string_t OPTION_WHERE;
19
e4558df9ba2d drop – run AWK code but generate no output for given relation
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    71
	static const string_t OPTION_DROP;
8
513ea30d2fa3 more configuration/options
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    72
	static const string_t OPTION_DEFINE;
0
644fd2ce2580 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    73
644fd2ce2580 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    74
	Configuration parse(const std::vector<string_t>& arguments) {
644fd2ce2580 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    75
		Configuration c;
8
513ea30d2fa3 more configuration/options
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    76
		RelationConfiguration currentRelation;
0
644fd2ce2580 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    77
9
b064f1b3676e omit superfluous {}
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
    78
		for (int i = 0; i < arguments.size();) {
b064f1b3676e omit superfluous {}
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
    79
			string_t option = readNext(arguments, i);
8
513ea30d2fa3 more configuration/options
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    80
9
b064f1b3676e omit superfluous {}
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
    81
			if (option == OPTION_BEFORE_RECORDS) currentRelation.awkBeforeRecords = readNext(arguments, i);
b064f1b3676e omit superfluous {}
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
    82
			else if (option == OPTION_AFTER_RECORDS) currentRelation.awkAfterRecords = readNext(arguments, i);
b064f1b3676e omit superfluous {}
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
    83
			else if (option == OPTION_FOR_EACH) currentRelation.awkForEach = readNext(arguments, i);
37
0c050899c77f add --where option: similar to --for-each but better expresses the intention: filtering
František Kučera <franta-hg@frantovo.cz>
parents: 35
diff changeset
    84
			else if (option == OPTION_WHERE) currentRelation.awkForEach = L"(" + readNext(arguments, i) + L")";
19
e4558df9ba2d drop – run AWK code but generate no output for given relation
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    85
			else if (option == OPTION_DROP) currentRelation.drop = true;
9
b064f1b3676e omit superfluous {}
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
    86
			else if (option == OPTION_INPUT_ATTRIBUTES_APPEND) currentRelation.inputAttributesAppend = true;
b064f1b3676e omit superfluous {}
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
    87
			else if (option == OPTION_INPUT_ATTRIBUTES_PREPEND) currentRelation.inputAttributesPrepend = true;
26
cf57e8c78492 add option: --debug-variable-mapping
František Kučera <franta-hg@frantovo.cz>
parents: 19
diff changeset
    88
			else if (option == OPTION_DEBUG_VARIABLE_MAPPING) currentRelation.debugVariableMapping = true;
9
b064f1b3676e omit superfluous {}
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
    89
			else if (option == OPTION_RELATION) {
b064f1b3676e omit superfluous {}
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
    90
				addRelation(c, currentRelation); // previous relation
b064f1b3676e omit superfluous {}
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
    91
				currentRelation.relation = readNext(arguments, i);
b064f1b3676e omit superfluous {}
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
    92
			} else if (option == OPTION_OUTPUT_ATTRIBUTE) {
b064f1b3676e omit superfluous {}
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
    93
				relpipe::writer::AttributeMetadata attribute;
b064f1b3676e omit superfluous {}
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
    94
				attribute.attributeName = readNext(arguments, i);
b064f1b3676e omit superfluous {}
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
    95
				attribute.typeId = parseTypeId(readNext(arguments, i));
b064f1b3676e omit superfluous {}
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
    96
				currentRelation.writerMetadata.push_back(attribute);
b064f1b3676e omit superfluous {}
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
    97
			} else if (option == OPTION_DEFINE) {
b064f1b3676e omit superfluous {}
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
    98
				DefinitionRecipe definition;
b064f1b3676e omit superfluous {}
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
    99
				definition.name = readNext(arguments, i);
b064f1b3676e omit superfluous {}
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   100
				definition.type = readNext(arguments, i);
b064f1b3676e omit superfluous {}
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   101
				definition.value = readNext(arguments, i);
b064f1b3676e omit superfluous {}
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   102
				if (currentRelation.relation.size()) currentRelation.definitions.push_back(definition);
b064f1b3676e omit superfluous {}
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   103
				else c.definitions.push_back(definition);
b064f1b3676e omit superfluous {}
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   104
			} else throw relpipe::cli::RelpipeCLIException(L"Unsupported CLI option: " + option, relpipe::cli::CLI::EXIT_CODE_BAD_CLI_ARGUMENTS);
0
644fd2ce2580 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   105
		}
14
f407f2a2871d omit superfluous {} fixed
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   106
		addRelation(c, currentRelation); // last relation
0
644fd2ce2580 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   107
644fd2ce2580 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   108
		return c;
644fd2ce2580 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   109
	}
644fd2ce2580 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   110
644fd2ce2580 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   111
	virtual ~CLIParser() {
644fd2ce2580 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   112
	}
644fd2ce2580 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   113
};
644fd2ce2580 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   114
8
513ea30d2fa3 more configuration/options
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   115
const string_t CLIParser::OPTION_RELATION = L"--relation";
513ea30d2fa3 more configuration/options
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   116
const string_t CLIParser::OPTION_OUTPUT_ATTRIBUTE = L"--output-attribute";
513ea30d2fa3 more configuration/options
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   117
const string_t CLIParser::OPTION_INPUT_ATTRIBUTES_APPEND = L"--input-attributes-append";
513ea30d2fa3 more configuration/options
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   118
const string_t CLIParser::OPTION_INPUT_ATTRIBUTES_PREPEND = L"--input-attributes-prepend";
26
cf57e8c78492 add option: --debug-variable-mapping
František Kučera <franta-hg@frantovo.cz>
parents: 19
diff changeset
   119
const string_t CLIParser::OPTION_DEBUG_VARIABLE_MAPPING = L"--debug-variable-mapping";
8
513ea30d2fa3 more configuration/options
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   120
const string_t CLIParser::OPTION_BEFORE_RECORDS = L"--before-records";
513ea30d2fa3 more configuration/options
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   121
const string_t CLIParser::OPTION_AFTER_RECORDS = L"--after-records";
513ea30d2fa3 more configuration/options
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   122
const string_t CLIParser::OPTION_FOR_EACH = L"--for-each";
37
0c050899c77f add --where option: similar to --for-each but better expresses the intention: filtering
František Kučera <franta-hg@frantovo.cz>
parents: 35
diff changeset
   123
const string_t CLIParser::OPTION_WHERE = L"--where";
19
e4558df9ba2d drop – run AWK code but generate no output for given relation
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   124
const string_t CLIParser::OPTION_DROP = L"--drop";
8
513ea30d2fa3 more configuration/options
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   125
const string_t CLIParser::OPTION_DEFINE = L"--define";
0
644fd2ce2580 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   126
644fd2ce2580 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   127
}
644fd2ce2580 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   128
}
644fd2ce2580 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   129
}