src/CLIParser.h
author František Kučera <franta-hg@frantovo.cz>
Wed, 07 Oct 2020 16:42:37 +0200
branchv_0
changeset 31 c22577615ce4
parent 30 3c6374467a82
child 38 2cc2d3f658f4
permissions -rw-r--r--
rename: --print-types, --print-relation-name, --print-record-count to --write-types, --write-relation-name, --write-record-count „write“ is more generic and can be used consistently across various output modules
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
30
3c6374467a82 change CLI interface: options: --relation --print-types --print-relation-name --print-record-count
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     1
/**
3c6374467a82 change CLI interface: options: --relation --print-types --print-relation-name --print-record-count
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     2
 * Relational pipes
3c6374467a82 change CLI interface: options: --relation --print-types --print-relation-name --print-record-count
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     3
 * Copyright © 2020 František Kučera (Frantovo.cz, GlobalCode.info)
3c6374467a82 change CLI interface: options: --relation --print-types --print-relation-name --print-record-count
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     4
 *
3c6374467a82 change CLI interface: options: --relation --print-types --print-relation-name --print-record-count
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     5
 * This program is free software: you can redistribute it and/or modify
3c6374467a82 change CLI interface: options: --relation --print-types --print-relation-name --print-record-count
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
3c6374467a82 change CLI interface: options: --relation --print-types --print-relation-name --print-record-count
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     7
 * the Free Software Foundation, version 3 of the License.
3c6374467a82 change CLI interface: options: --relation --print-types --print-relation-name --print-record-count
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     8
 *
3c6374467a82 change CLI interface: options: --relation --print-types --print-relation-name --print-record-count
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     9
 * This program is distributed in the hope that it will be useful,
3c6374467a82 change CLI interface: options: --relation --print-types --print-relation-name --print-record-count
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
3c6374467a82 change CLI interface: options: --relation --print-types --print-relation-name --print-record-count
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3c6374467a82 change CLI interface: options: --relation --print-types --print-relation-name --print-record-count
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    12
 * GNU General Public License for more details.
3c6374467a82 change CLI interface: options: --relation --print-types --print-relation-name --print-record-count
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    13
 *
3c6374467a82 change CLI interface: options: --relation --print-types --print-relation-name --print-record-count
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    14
 * You should have received a copy of the GNU General Public License
3c6374467a82 change CLI interface: options: --relation --print-types --print-relation-name --print-record-count
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    15
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
3c6374467a82 change CLI interface: options: --relation --print-types --print-relation-name --print-record-count
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    16
 */
3c6374467a82 change CLI interface: options: --relation --print-types --print-relation-name --print-record-count
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    17
#pragma once
3c6374467a82 change CLI interface: options: --relation --print-types --print-relation-name --print-record-count
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    18
3c6374467a82 change CLI interface: options: --relation --print-types --print-relation-name --print-record-count
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    19
#include <vector>
3c6374467a82 change CLI interface: options: --relation --print-types --print-relation-name --print-record-count
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    20
#include <iostream>
3c6374467a82 change CLI interface: options: --relation --print-types --print-relation-name --print-record-count
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    21
3c6374467a82 change CLI interface: options: --relation --print-types --print-relation-name --print-record-count
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    22
#include <relpipe/reader/typedefs.h>
3c6374467a82 change CLI interface: options: --relation --print-types --print-relation-name --print-record-count
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    23
#include <relpipe/cli/CLI.h>
3c6374467a82 change CLI interface: options: --relation --print-types --print-relation-name --print-record-count
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    24
#include <relpipe/cli/RelpipeCLIException.h>
3c6374467a82 change CLI interface: options: --relation --print-types --print-relation-name --print-record-count
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    25
3c6374467a82 change CLI interface: options: --relation --print-types --print-relation-name --print-record-count
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    26
#include "Configuration.h"
3c6374467a82 change CLI interface: options: --relation --print-types --print-relation-name --print-record-count
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    27
3c6374467a82 change CLI interface: options: --relation --print-types --print-relation-name --print-record-count
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    28
namespace relpipe {
3c6374467a82 change CLI interface: options: --relation --print-types --print-relation-name --print-record-count
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    29
namespace out {
3c6374467a82 change CLI interface: options: --relation --print-types --print-relation-name --print-record-count
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    30
namespace tabular {
3c6374467a82 change CLI interface: options: --relation --print-types --print-relation-name --print-record-count
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    31
3c6374467a82 change CLI interface: options: --relation --print-types --print-relation-name --print-record-count
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    32
class CLIParser {
3c6374467a82 change CLI interface: options: --relation --print-types --print-relation-name --print-record-count
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    33
private:
3c6374467a82 change CLI interface: options: --relation --print-types --print-relation-name --print-record-count
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    34
3c6374467a82 change CLI interface: options: --relation --print-types --print-relation-name --print-record-count
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    35
	relpipe::reader::string_t readNext(const std::vector<relpipe::reader::string_t>& arguments, int& i) {
3c6374467a82 change CLI interface: options: --relation --print-types --print-relation-name --print-record-count
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    36
		if (i < arguments.size()) return arguments[i++];
3c6374467a82 change CLI interface: options: --relation --print-types --print-relation-name --print-record-count
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    37
		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);
3c6374467a82 change CLI interface: options: --relation --print-types --print-relation-name --print-record-count
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    38
	}
3c6374467a82 change CLI interface: options: --relation --print-types --print-relation-name --print-record-count
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    39
3c6374467a82 change CLI interface: options: --relation --print-types --print-relation-name --print-record-count
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    40
	void addRelation(Configuration& c, RelationConfiguration& currentRelation) {
3c6374467a82 change CLI interface: options: --relation --print-types --print-relation-name --print-record-count
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    41
		if (currentRelation.relation.size()) {
3c6374467a82 change CLI interface: options: --relation --print-types --print-relation-name --print-record-count
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    42
			c.relationConfigurations.push_back(currentRelation);
3c6374467a82 change CLI interface: options: --relation --print-types --print-relation-name --print-record-count
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    43
		} else {
3c6374467a82 change CLI interface: options: --relation --print-types --print-relation-name --print-record-count
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    44
			// no relation name given → global configuration
31
c22577615ce4 rename: --print-types, --print-relation-name, --print-record-count to --write-types, --write-relation-name, --write-record-count
František Kučera <franta-hg@frantovo.cz>
parents: 30
diff changeset
    45
			c.writeTypes = currentRelation.writeTypes;
c22577615ce4 rename: --print-types, --print-relation-name, --print-record-count to --write-types, --write-relation-name, --write-record-count
František Kučera <franta-hg@frantovo.cz>
parents: 30
diff changeset
    46
			c.writeRelationName = currentRelation.writeRelationName;
c22577615ce4 rename: --print-types, --print-relation-name, --print-record-count to --write-types, --write-relation-name, --write-record-count
František Kučera <franta-hg@frantovo.cz>
parents: 30
diff changeset
    47
			c.writeRecordCount = currentRelation.writeRecordCount;
30
3c6374467a82 change CLI interface: options: --relation --print-types --print-relation-name --print-record-count
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    48
		}
3c6374467a82 change CLI interface: options: --relation --print-types --print-relation-name --print-record-count
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    49
		currentRelation = RelationConfiguration();
3c6374467a82 change CLI interface: options: --relation --print-types --print-relation-name --print-record-count
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    50
	}
3c6374467a82 change CLI interface: options: --relation --print-types --print-relation-name --print-record-count
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    51
3c6374467a82 change CLI interface: options: --relation --print-types --print-relation-name --print-record-count
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    52
	/**
3c6374467a82 change CLI interface: options: --relation --print-types --print-relation-name --print-record-count
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    53
	 * TODO: use a common method
3c6374467a82 change CLI interface: options: --relation --print-types --print-relation-name --print-record-count
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    54
	 */
3c6374467a82 change CLI interface: options: --relation --print-types --print-relation-name --print-record-count
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    55
	bool parseBoolean(const relpipe::reader::string_t& value) {
3c6374467a82 change CLI interface: options: --relation --print-types --print-relation-name --print-record-count
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    56
		if (value == L"true") return true;
3c6374467a82 change CLI interface: options: --relation --print-types --print-relation-name --print-record-count
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    57
		else if (value == L"false") return false;
3c6374467a82 change CLI interface: options: --relation --print-types --print-relation-name --print-record-count
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    58
		else throw relpipe::cli::RelpipeCLIException(L"Unable to parse boolean value: " + value + L" (expecting true or false)", relpipe::cli::CLI::EXIT_CODE_BAD_CLI_ARGUMENTS);
3c6374467a82 change CLI interface: options: --relation --print-types --print-relation-name --print-record-count
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    59
	}
3c6374467a82 change CLI interface: options: --relation --print-types --print-relation-name --print-record-count
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    60
3c6374467a82 change CLI interface: options: --relation --print-types --print-relation-name --print-record-count
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    61
public:
3c6374467a82 change CLI interface: options: --relation --print-types --print-relation-name --print-record-count
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    62
3c6374467a82 change CLI interface: options: --relation --print-types --print-relation-name --print-record-count
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    63
	static const relpipe::reader::string_t OPTION_RELATION;
31
c22577615ce4 rename: --print-types, --print-relation-name, --print-record-count to --write-types, --write-relation-name, --write-record-count
František Kučera <franta-hg@frantovo.cz>
parents: 30
diff changeset
    64
	static const relpipe::reader::string_t OPTION_WRITE_TYPES;
c22577615ce4 rename: --print-types, --print-relation-name, --print-record-count to --write-types, --write-relation-name, --write-record-count
František Kučera <franta-hg@frantovo.cz>
parents: 30
diff changeset
    65
	static const relpipe::reader::string_t OPTION_WRITE_RELATION_NAME;
c22577615ce4 rename: --print-types, --print-relation-name, --print-record-count to --write-types, --write-relation-name, --write-record-count
František Kučera <franta-hg@frantovo.cz>
parents: 30
diff changeset
    66
	static const relpipe::reader::string_t OPTION_WRITE_RECORD_COUNT;
30
3c6374467a82 change CLI interface: options: --relation --print-types --print-relation-name --print-record-count
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    67
3c6374467a82 change CLI interface: options: --relation --print-types --print-relation-name --print-record-count
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    68
	Configuration parse(const std::vector<relpipe::reader::string_t>& arguments) {
3c6374467a82 change CLI interface: options: --relation --print-types --print-relation-name --print-record-count
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    69
		Configuration c;
3c6374467a82 change CLI interface: options: --relation --print-types --print-relation-name --print-record-count
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    70
		RelationConfiguration currentRelation;
3c6374467a82 change CLI interface: options: --relation --print-types --print-relation-name --print-record-count
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    71
3c6374467a82 change CLI interface: options: --relation --print-types --print-relation-name --print-record-count
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    72
		for (int i = 0; i < arguments.size();) {
3c6374467a82 change CLI interface: options: --relation --print-types --print-relation-name --print-record-count
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    73
			relpipe::reader::string_t option = readNext(arguments, i);
3c6374467a82 change CLI interface: options: --relation --print-types --print-relation-name --print-record-count
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    74
3c6374467a82 change CLI interface: options: --relation --print-types --print-relation-name --print-record-count
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    75
			if (option == OPTION_RELATION) {
3c6374467a82 change CLI interface: options: --relation --print-types --print-relation-name --print-record-count
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    76
				addRelation(c, currentRelation); // previous relation
3c6374467a82 change CLI interface: options: --relation --print-types --print-relation-name --print-record-count
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    77
				currentRelation.relation = readNext(arguments, i);
31
c22577615ce4 rename: --print-types, --print-relation-name, --print-record-count to --write-types, --write-relation-name, --write-record-count
František Kučera <franta-hg@frantovo.cz>
parents: 30
diff changeset
    78
			} else if (option == OPTION_WRITE_TYPES) {
c22577615ce4 rename: --print-types, --print-relation-name, --print-record-count to --write-types, --write-relation-name, --write-record-count
František Kučera <franta-hg@frantovo.cz>
parents: 30
diff changeset
    79
				currentRelation.writeTypes = parseBoolean(readNext(arguments, i));
c22577615ce4 rename: --print-types, --print-relation-name, --print-record-count to --write-types, --write-relation-name, --write-record-count
František Kučera <franta-hg@frantovo.cz>
parents: 30
diff changeset
    80
			} else if (option == OPTION_WRITE_RELATION_NAME) {
c22577615ce4 rename: --print-types, --print-relation-name, --print-record-count to --write-types, --write-relation-name, --write-record-count
František Kučera <franta-hg@frantovo.cz>
parents: 30
diff changeset
    81
				currentRelation.writeRelationName = parseBoolean(readNext(arguments, i));
c22577615ce4 rename: --print-types, --print-relation-name, --print-record-count to --write-types, --write-relation-name, --write-record-count
František Kučera <franta-hg@frantovo.cz>
parents: 30
diff changeset
    82
			} else if (option == OPTION_WRITE_RECORD_COUNT) {
c22577615ce4 rename: --print-types, --print-relation-name, --print-record-count to --write-types, --write-relation-name, --write-record-count
František Kučera <franta-hg@frantovo.cz>
parents: 30
diff changeset
    83
				currentRelation.writeRecordCount = parseBoolean(readNext(arguments, i));
30
3c6374467a82 change CLI interface: options: --relation --print-types --print-relation-name --print-record-count
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    84
			} else throw relpipe::cli::RelpipeCLIException(L"Unsupported CLI option: " + option, relpipe::cli::CLI::EXIT_CODE_BAD_CLI_ARGUMENTS);
3c6374467a82 change CLI interface: options: --relation --print-types --print-relation-name --print-record-count
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    85
		}
3c6374467a82 change CLI interface: options: --relation --print-types --print-relation-name --print-record-count
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    86
3c6374467a82 change CLI interface: options: --relation --print-types --print-relation-name --print-record-count
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    87
		addRelation(c, currentRelation); // last relation
3c6374467a82 change CLI interface: options: --relation --print-types --print-relation-name --print-record-count
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    88
3c6374467a82 change CLI interface: options: --relation --print-types --print-relation-name --print-record-count
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    89
		return c;
3c6374467a82 change CLI interface: options: --relation --print-types --print-relation-name --print-record-count
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    90
	}
3c6374467a82 change CLI interface: options: --relation --print-types --print-relation-name --print-record-count
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    91
3c6374467a82 change CLI interface: options: --relation --print-types --print-relation-name --print-record-count
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    92
	virtual ~CLIParser() {
3c6374467a82 change CLI interface: options: --relation --print-types --print-relation-name --print-record-count
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    93
	}
3c6374467a82 change CLI interface: options: --relation --print-types --print-relation-name --print-record-count
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    94
};
3c6374467a82 change CLI interface: options: --relation --print-types --print-relation-name --print-record-count
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    95
3c6374467a82 change CLI interface: options: --relation --print-types --print-relation-name --print-record-count
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    96
const relpipe::reader::string_t CLIParser::OPTION_RELATION = L"--relation";
31
c22577615ce4 rename: --print-types, --print-relation-name, --print-record-count to --write-types, --write-relation-name, --write-record-count
František Kučera <franta-hg@frantovo.cz>
parents: 30
diff changeset
    97
const relpipe::reader::string_t CLIParser::OPTION_WRITE_TYPES = L"--write-types";
c22577615ce4 rename: --print-types, --print-relation-name, --print-record-count to --write-types, --write-relation-name, --write-record-count
František Kučera <franta-hg@frantovo.cz>
parents: 30
diff changeset
    98
const relpipe::reader::string_t CLIParser::OPTION_WRITE_RELATION_NAME = L"--write-relation-name";
c22577615ce4 rename: --print-types, --print-relation-name, --print-record-count to --write-types, --write-relation-name, --write-record-count
František Kučera <franta-hg@frantovo.cz>
parents: 30
diff changeset
    99
const relpipe::reader::string_t CLIParser::OPTION_WRITE_RECORD_COUNT = L"--write-record-count";
30
3c6374467a82 change CLI interface: options: --relation --print-types --print-relation-name --print-record-count
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   100
3c6374467a82 change CLI interface: options: --relation --print-types --print-relation-name --print-record-count
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   101
}
3c6374467a82 change CLI interface: options: --relation --print-types --print-relation-name --print-record-count
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   102
}
3c6374467a82 change CLI interface: options: --relation --print-types --print-relation-name --print-record-count
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   103
}