src/CLIParser.h
author František Kučera <franta-hg@frantovo.cz>
Wed, 23 Sep 2020 16:58:48 +0200
branchv_0
changeset 16 15ee963675af
child 20 90ae67de2f68
permissions -rw-r--r--
change CLI interface: options: --relation --attribute
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
16
15ee963675af change CLI interface: options: --relation --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     1
/**
15ee963675af change CLI interface: options: --relation --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     2
 * Relational pipes
15ee963675af change CLI interface: options: --relation --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     3
 * Copyright © 2020 František Kučera (Frantovo.cz, GlobalCode.info)
15ee963675af change CLI interface: options: --relation --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     4
 *
15ee963675af change CLI interface: options: --relation --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     5
 * This program is free software: you can redistribute it and/or modify
15ee963675af change CLI interface: options: --relation --attribute
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
15ee963675af change CLI interface: options: --relation --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     7
 * the Free Software Foundation, version 3 of the License.
15ee963675af change CLI interface: options: --relation --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     8
 *
15ee963675af change CLI interface: options: --relation --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     9
 * This program is distributed in the hope that it will be useful,
15ee963675af change CLI interface: options: --relation --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15ee963675af change CLI interface: options: --relation --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15ee963675af change CLI interface: options: --relation --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    12
 * GNU General Public License for more details.
15ee963675af change CLI interface: options: --relation --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    13
 *
15ee963675af change CLI interface: options: --relation --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    14
 * You should have received a copy of the GNU General Public License
15ee963675af change CLI interface: options: --relation --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    15
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15ee963675af change CLI interface: options: --relation --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    16
 */
15ee963675af change CLI interface: options: --relation --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    17
#pragma once
15ee963675af change CLI interface: options: --relation --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    18
15ee963675af change CLI interface: options: --relation --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    19
#include <vector>
15ee963675af change CLI interface: options: --relation --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    20
#include <iostream>
15ee963675af change CLI interface: options: --relation --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    21
15ee963675af change CLI interface: options: --relation --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    22
#include <relpipe/writer/typedefs.h>
15ee963675af change CLI interface: options: --relation --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    23
#include <relpipe/cli/CLI.h>
15ee963675af change CLI interface: options: --relation --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    24
#include <relpipe/cli/RelpipeCLIException.h>
15ee963675af change CLI interface: options: --relation --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    25
15ee963675af change CLI interface: options: --relation --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    26
#include "Configuration.h"
15ee963675af change CLI interface: options: --relation --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    27
15ee963675af change CLI interface: options: --relation --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    28
namespace relpipe {
15ee963675af change CLI interface: options: --relation --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    29
namespace in {
15ee963675af change CLI interface: options: --relation --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    30
namespace csv {
15ee963675af change CLI interface: options: --relation --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    31
15ee963675af change CLI interface: options: --relation --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    32
class CLIParser {
15ee963675af change CLI interface: options: --relation --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    33
private:
15ee963675af change CLI interface: options: --relation --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    34
15ee963675af change CLI interface: options: --relation --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    35
	relpipe::writer::string_t readNext(const std::vector<relpipe::writer::string_t>& arguments, int& i) {
15ee963675af change CLI interface: options: --relation --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    36
		if (i < arguments.size()) return arguments[i++];
15ee963675af change CLI interface: options: --relation --attribute
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);
15ee963675af change CLI interface: options: --relation --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    38
	}
15ee963675af change CLI interface: options: --relation --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    39
15ee963675af change CLI interface: options: --relation --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    40
	/**
15ee963675af change CLI interface: options: --relation --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    41
	 * TODO: use a common method
15ee963675af change CLI interface: options: --relation --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    42
	 */
15ee963675af change CLI interface: options: --relation --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    43
	bool parseBoolean(const relpipe::writer::string_t& value) {
15ee963675af change CLI interface: options: --relation --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    44
		if (value == L"true") return true;
15ee963675af change CLI interface: options: --relation --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    45
		else if (value == L"false") return false;
15ee963675af change CLI interface: options: --relation --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    46
		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);
15ee963675af change CLI interface: options: --relation --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    47
	}
15ee963675af change CLI interface: options: --relation --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    48
15ee963675af change CLI interface: options: --relation --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    49
	/**
15ee963675af change CLI interface: options: --relation --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    50
	 * TODO: use a common method
15ee963675af change CLI interface: options: --relation --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    51
	 */
15ee963675af change CLI interface: options: --relation --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    52
	relpipe::writer::TypeId parseTypeId(const relpipe::writer::string_t& value) {
15ee963675af change CLI interface: options: --relation --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    53
		using t = relpipe::writer::TypeId;
15ee963675af change CLI interface: options: --relation --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    54
		if (value == L"string") return t::STRING;
15ee963675af change CLI interface: options: --relation --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    55
		else if (value == L"integer") return t::INTEGER;
15ee963675af change CLI interface: options: --relation --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    56
		else if (value == L"boolean") return t::BOOLEAN;
15ee963675af change CLI interface: options: --relation --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    57
		else throw relpipe::cli::RelpipeCLIException(L"Unable to parse TypeId: " + value, relpipe::cli::CLI::EXIT_CODE_BAD_CLI_ARGUMENTS);
15ee963675af change CLI interface: options: --relation --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    58
	}
15ee963675af change CLI interface: options: --relation --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    59
15ee963675af change CLI interface: options: --relation --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    60
public:
15ee963675af change CLI interface: options: --relation --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    61
15ee963675af change CLI interface: options: --relation --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    62
	static const relpipe::writer::string_t OPTION_RELATION;
15ee963675af change CLI interface: options: --relation --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    63
	static const relpipe::writer::string_t OPTION_ATTRIBUTE;
15ee963675af change CLI interface: options: --relation --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    64
15ee963675af change CLI interface: options: --relation --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    65
	Configuration parse(const std::vector<relpipe::writer::string_t>& arguments) {
15ee963675af change CLI interface: options: --relation --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    66
		Configuration c;
15ee963675af change CLI interface: options: --relation --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    67
15ee963675af change CLI interface: options: --relation --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    68
		for (int i = 0; i < arguments.size();) {
15ee963675af change CLI interface: options: --relation --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    69
			relpipe::writer::string_t option = readNext(arguments, i);
15ee963675af change CLI interface: options: --relation --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    70
15ee963675af change CLI interface: options: --relation --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    71
			if (option == OPTION_RELATION) {
15ee963675af change CLI interface: options: --relation --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    72
				c.relation = readNext(arguments, i);
15ee963675af change CLI interface: options: --relation --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    73
			} else if (option == OPTION_ATTRIBUTE) {
15ee963675af change CLI interface: options: --relation --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    74
				AttributeRecipe attribute;
15ee963675af change CLI interface: options: --relation --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    75
				attribute.name = readNext(arguments, i);
15ee963675af change CLI interface: options: --relation --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    76
				attribute.type = parseTypeId(readNext(arguments, i));
15ee963675af change CLI interface: options: --relation --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    77
				c.attributes.push_back(attribute);
15ee963675af change CLI interface: options: --relation --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    78
			} else throw relpipe::cli::RelpipeCLIException(L"Unsupported CLI option: " + option, relpipe::cli::CLI::EXIT_CODE_BAD_CLI_ARGUMENTS);
15ee963675af change CLI interface: options: --relation --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    79
		}
15ee963675af change CLI interface: options: --relation --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    80
15ee963675af change CLI interface: options: --relation --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    81
		return c;
15ee963675af change CLI interface: options: --relation --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    82
	}
15ee963675af change CLI interface: options: --relation --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    83
15ee963675af change CLI interface: options: --relation --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    84
	virtual ~CLIParser() {
15ee963675af change CLI interface: options: --relation --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    85
	}
15ee963675af change CLI interface: options: --relation --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    86
};
15ee963675af change CLI interface: options: --relation --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    87
15ee963675af change CLI interface: options: --relation --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    88
const relpipe::writer::string_t CLIParser::OPTION_RELATION = L"--relation";
15ee963675af change CLI interface: options: --relation --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    89
const relpipe::writer::string_t CLIParser::OPTION_ATTRIBUTE = L"--attribute";
15ee963675af change CLI interface: options: --relation --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    90
15ee963675af change CLI interface: options: --relation --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    91
}
15ee963675af change CLI interface: options: --relation --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    92
}
15ee963675af change CLI interface: options: --relation --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    93
}