src/CLIParser.h
author František Kučera <franta-hg@frantovo.cz>
Sat, 02 Jul 2022 19:45:18 +0200
branchv_0
changeset 42 ca216de56ef0
parent 41 e1339b8e838e
permissions -rw-r--r--
allow setting some options through ENV variables (not only CLI arguments)
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
42
ca216de56ef0 allow setting some options through ENV variables (not only CLI arguments)
František Kučera <franta-hg@frantovo.cz>
parents: 41
diff changeset
    35
	std::wstring_convert<std::codecvt_utf8<wchar_t>> convertor; // TODO: support also other encodings.
ca216de56ef0 allow setting some options through ENV variables (not only CLI arguments)
František Kučera <franta-hg@frantovo.cz>
parents: 41
diff changeset
    36
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
    37
	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
    38
		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
    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);
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
	}
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
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
	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
    43
		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
    44
			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
    45
		} 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
    46
			// 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
    47
			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
    48
			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
    49
			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
    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
		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
    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
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
	 * 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
    56
	 */
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
	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
    58
		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
    59
		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
    60
		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
    61
	}
42
ca216de56ef0 allow setting some options through ENV variables (not only CLI arguments)
František Kučera <franta-hg@frantovo.cz>
parents: 41
diff changeset
    62
38
2cc2d3f658f4 configurable table style: rounded, sharp, sharp-double, horizontal-only, ascii
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
    63
	Configuration::ColorScheme parseColorScheme(const relpipe::reader::string_t& value) {
39
f33464965693 prepare for configurable color schemes
František Kučera <franta-hg@frantovo.cz>
parents: 38
diff changeset
    64
		if (value == L"greenish") return Configuration::ColorScheme::Greenish;
f33464965693 prepare for configurable color schemes
František Kučera <franta-hg@frantovo.cz>
parents: 38
diff changeset
    65
		else if (value == L"amberish") return Configuration::ColorScheme::Amberish;
41
e1339b8e838e configurable color schemes: rename monochrome to black-and-white
František Kučera <franta-hg@frantovo.cz>
parents: 39
diff changeset
    66
		else if (value == L"black-and-white") return Configuration::ColorScheme::BlackAndWhite;
38
2cc2d3f658f4 configurable table style: rounded, sharp, sharp-double, horizontal-only, ascii
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
    67
		else if (value == L"midnight") return Configuration::ColorScheme::Midnight;
41
e1339b8e838e configurable color schemes: rename monochrome to black-and-white
František Kučera <franta-hg@frantovo.cz>
parents: 39
diff changeset
    68
		else throw relpipe::cli::RelpipeCLIException(L"Unable to parse ColorScheme value: " + value + L" (expecting greenish, amberish, midnight or black-and-white)", relpipe::cli::CLI::EXIT_CODE_BAD_CLI_ARGUMENTS);
38
2cc2d3f658f4 configurable table style: rounded, sharp, sharp-double, horizontal-only, ascii
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
    69
	}
42
ca216de56ef0 allow setting some options through ENV variables (not only CLI arguments)
František Kučera <franta-hg@frantovo.cz>
parents: 41
diff changeset
    70
38
2cc2d3f658f4 configurable table style: rounded, sharp, sharp-double, horizontal-only, ascii
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
    71
	Configuration::TableStyle parseTableStyle(const relpipe::reader::string_t& value) {
2cc2d3f658f4 configurable table style: rounded, sharp, sharp-double, horizontal-only, ascii
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
    72
		if (value == L"rounded") return Configuration::TableStyle::Rounded;
2cc2d3f658f4 configurable table style: rounded, sharp, sharp-double, horizontal-only, ascii
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
    73
		else if (value == L"sharp") return Configuration::TableStyle::Sharp;
2cc2d3f658f4 configurable table style: rounded, sharp, sharp-double, horizontal-only, ascii
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
    74
		else if (value == L"sharp-double") return Configuration::TableStyle::SharpDouble;
2cc2d3f658f4 configurable table style: rounded, sharp, sharp-double, horizontal-only, ascii
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
    75
		else if (value == L"horizontal-only") return Configuration::TableStyle::HorizontalOnly;
2cc2d3f658f4 configurable table style: rounded, sharp, sharp-double, horizontal-only, ascii
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
    76
		else if (value == L"ascii") return Configuration::TableStyle::Ascii;
2cc2d3f658f4 configurable table style: rounded, sharp, sharp-double, horizontal-only, ascii
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
    77
		else throw relpipe::cli::RelpipeCLIException(L"Unable to parse TableStyle value: " + value + L" (expecting rounded, sharp, ascii)", relpipe::cli::CLI::EXIT_CODE_BAD_CLI_ARGUMENTS);
2cc2d3f658f4 configurable table style: rounded, sharp, sharp-double, horizontal-only, ascii
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
    78
	}
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
    79
42
ca216de56ef0 allow setting some options through ENV variables (not only CLI arguments)
František Kučera <franta-hg@frantovo.cz>
parents: 41
diff changeset
    80
	/**
ca216de56ef0 allow setting some options through ENV variables (not only CLI arguments)
František Kučera <franta-hg@frantovo.cz>
parents: 41
diff changeset
    81
	 * TODO: move to common parent class in relpipe-lib-cli
ca216de56ef0 allow setting some options through ENV variables (not only CLI arguments)
František Kučera <franta-hg@frantovo.cz>
parents: 41
diff changeset
    82
	 * 
ca216de56ef0 allow setting some options through ENV variables (not only CLI arguments)
František Kučera <franta-hg@frantovo.cz>
parents: 41
diff changeset
    83
	 * @param cliArguments original CLI arguments
ca216de56ef0 allow setting some options through ENV variables (not only CLI arguments)
František Kučera <franta-hg@frantovo.cz>
parents: 41
diff changeset
    84
	 * @param moduleName name of this program e.g. "relpipe-out-tabular"
ca216de56ef0 allow setting some options through ENV variables (not only CLI arguments)
František Kučera <franta-hg@frantovo.cz>
parents: 41
diff changeset
    85
	 * @param supportedOptions options that could be specified also as environmental variables, not only CLI arguments, including the "--" prefix e.g. "--color-scheme"
ca216de56ef0 allow setting some options through ENV variables (not only CLI arguments)
František Kučera <franta-hg@frantovo.cz>
parents: 41
diff changeset
    86
	 * @return defaults found in ENV + given CLI arguments
ca216de56ef0 allow setting some options through ENV variables (not only CLI arguments)
František Kučera <franta-hg@frantovo.cz>
parents: 41
diff changeset
    87
	 */
ca216de56ef0 allow setting some options through ENV variables (not only CLI arguments)
František Kučera <franta-hg@frantovo.cz>
parents: 41
diff changeset
    88
	const std::vector<relpipe::reader::string_t> addEnvDefaults(const std::vector<relpipe::reader::string_t>& cliArguments, const relpipe::reader::string_t moduleName, const std::vector<relpipe::reader::string_t>& supportedOptions) {
ca216de56ef0 allow setting some options through ENV variables (not only CLI arguments)
František Kučera <franta-hg@frantovo.cz>
parents: 41
diff changeset
    89
		std::vector<relpipe::reader::string_t> allArguments;
ca216de56ef0 allow setting some options through ENV variables (not only CLI arguments)
František Kučera <franta-hg@frantovo.cz>
parents: 41
diff changeset
    90
ca216de56ef0 allow setting some options through ENV variables (not only CLI arguments)
František Kučera <franta-hg@frantovo.cz>
parents: 41
diff changeset
    91
		auto toEnv = [](char ch) {
ca216de56ef0 allow setting some options through ENV variables (not only CLI arguments)
František Kučera <franta-hg@frantovo.cz>
parents: 41
diff changeset
    92
			return ch == '-' ? '_' : ::toupper(ch);
ca216de56ef0 allow setting some options through ENV variables (not only CLI arguments)
František Kučera <franta-hg@frantovo.cz>
parents: 41
diff changeset
    93
		};
ca216de56ef0 allow setting some options through ENV variables (not only CLI arguments)
František Kučera <franta-hg@frantovo.cz>
parents: 41
diff changeset
    94
ca216de56ef0 allow setting some options through ENV variables (not only CLI arguments)
František Kučera <franta-hg@frantovo.cz>
parents: 41
diff changeset
    95
		std::string envPrefix = convertor.to_bytes(moduleName);
ca216de56ef0 allow setting some options through ENV variables (not only CLI arguments)
František Kučera <franta-hg@frantovo.cz>
parents: 41
diff changeset
    96
		transform(envPrefix.begin(), envPrefix.end(), envPrefix.begin(), toEnv);
ca216de56ef0 allow setting some options through ENV variables (not only CLI arguments)
František Kučera <franta-hg@frantovo.cz>
parents: 41
diff changeset
    97
		envPrefix.append("_");
ca216de56ef0 allow setting some options through ENV variables (not only CLI arguments)
František Kučera <franta-hg@frantovo.cz>
parents: 41
diff changeset
    98
ca216de56ef0 allow setting some options through ENV variables (not only CLI arguments)
František Kučera <franta-hg@frantovo.cz>
parents: 41
diff changeset
    99
		for (auto o : supportedOptions) {
ca216de56ef0 allow setting some options through ENV variables (not only CLI arguments)
František Kučera <franta-hg@frantovo.cz>
parents: 41
diff changeset
   100
			if (o.substr(0, 2) == L"--") {
ca216de56ef0 allow setting some options through ENV variables (not only CLI arguments)
František Kučera <franta-hg@frantovo.cz>
parents: 41
diff changeset
   101
				std::string option = convertor.to_bytes(o);
ca216de56ef0 allow setting some options through ENV variables (not only CLI arguments)
František Kučera <franta-hg@frantovo.cz>
parents: 41
diff changeset
   102
				option.erase(0, 2);
ca216de56ef0 allow setting some options through ENV variables (not only CLI arguments)
František Kučera <franta-hg@frantovo.cz>
parents: 41
diff changeset
   103
				transform(option.begin(), option.end(), option.begin(), toEnv);
ca216de56ef0 allow setting some options through ENV variables (not only CLI arguments)
František Kučera <franta-hg@frantovo.cz>
parents: 41
diff changeset
   104
ca216de56ef0 allow setting some options through ENV variables (not only CLI arguments)
František Kučera <franta-hg@frantovo.cz>
parents: 41
diff changeset
   105
				// FIXME: check argument counts (should be specified alongside with supportedOptions) to avoid injection of unwanted options through ENV variables
ca216de56ef0 allow setting some options through ENV variables (not only CLI arguments)
František Kučera <franta-hg@frantovo.cz>
parents: 41
diff changeset
   106
				// TODO: allow repeated options?
ca216de56ef0 allow setting some options through ENV variables (not only CLI arguments)
František Kučera <franta-hg@frantovo.cz>
parents: 41
diff changeset
   107
				for (int i = -1; i < 10; i++) {
ca216de56ef0 allow setting some options through ENV variables (not only CLI arguments)
František Kučera <franta-hg@frantovo.cz>
parents: 41
diff changeset
   108
					std::string envName = i == -1 ? envPrefix + option : envPrefix + option + "_" + std::to_string(i);
ca216de56ef0 allow setting some options through ENV variables (not only CLI arguments)
František Kučera <franta-hg@frantovo.cz>
parents: 41
diff changeset
   109
					const char* value = std::getenv(envName.c_str());
ca216de56ef0 allow setting some options through ENV variables (not only CLI arguments)
František Kučera <franta-hg@frantovo.cz>
parents: 41
diff changeset
   110
					if (value) {
ca216de56ef0 allow setting some options through ENV variables (not only CLI arguments)
František Kučera <franta-hg@frantovo.cz>
parents: 41
diff changeset
   111
						if (i == -1 || i == 0) allArguments.push_back(o);
ca216de56ef0 allow setting some options through ENV variables (not only CLI arguments)
František Kučera <franta-hg@frantovo.cz>
parents: 41
diff changeset
   112
						allArguments.push_back(convertor.from_bytes(value));
ca216de56ef0 allow setting some options through ENV variables (not only CLI arguments)
František Kučera <franta-hg@frantovo.cz>
parents: 41
diff changeset
   113
						if (i == -1) break;
ca216de56ef0 allow setting some options through ENV variables (not only CLI arguments)
František Kučera <franta-hg@frantovo.cz>
parents: 41
diff changeset
   114
					} else {
ca216de56ef0 allow setting some options through ENV variables (not only CLI arguments)
František Kučera <franta-hg@frantovo.cz>
parents: 41
diff changeset
   115
						if (i > -1) break;
ca216de56ef0 allow setting some options through ENV variables (not only CLI arguments)
František Kučera <franta-hg@frantovo.cz>
parents: 41
diff changeset
   116
					}
ca216de56ef0 allow setting some options through ENV variables (not only CLI arguments)
František Kučera <franta-hg@frantovo.cz>
parents: 41
diff changeset
   117
				}
ca216de56ef0 allow setting some options through ENV variables (not only CLI arguments)
František Kučera <franta-hg@frantovo.cz>
parents: 41
diff changeset
   118
			} else {
ca216de56ef0 allow setting some options through ENV variables (not only CLI arguments)
František Kučera <franta-hg@frantovo.cz>
parents: 41
diff changeset
   119
				throw std::logic_error("supportedOptions must start with '--'");
ca216de56ef0 allow setting some options through ENV variables (not only CLI arguments)
František Kučera <franta-hg@frantovo.cz>
parents: 41
diff changeset
   120
			}
ca216de56ef0 allow setting some options through ENV variables (not only CLI arguments)
František Kučera <franta-hg@frantovo.cz>
parents: 41
diff changeset
   121
		}
ca216de56ef0 allow setting some options through ENV variables (not only CLI arguments)
František Kučera <franta-hg@frantovo.cz>
parents: 41
diff changeset
   122
ca216de56ef0 allow setting some options through ENV variables (not only CLI arguments)
František Kučera <franta-hg@frantovo.cz>
parents: 41
diff changeset
   123
		for (auto a : cliArguments) allArguments.push_back(a);
ca216de56ef0 allow setting some options through ENV variables (not only CLI arguments)
František Kučera <franta-hg@frantovo.cz>
parents: 41
diff changeset
   124
ca216de56ef0 allow setting some options through ENV variables (not only CLI arguments)
František Kučera <franta-hg@frantovo.cz>
parents: 41
diff changeset
   125
		return allArguments;
ca216de56ef0 allow setting some options through ENV variables (not only CLI arguments)
František Kučera <franta-hg@frantovo.cz>
parents: 41
diff changeset
   126
	}
ca216de56ef0 allow setting some options through ENV variables (not only CLI arguments)
František Kučera <franta-hg@frantovo.cz>
parents: 41
diff changeset
   127
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
   128
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
   129
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
   130
	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
   131
	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
   132
	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
   133
	static const relpipe::reader::string_t OPTION_WRITE_RECORD_COUNT;
38
2cc2d3f658f4 configurable table style: rounded, sharp, sharp-double, horizontal-only, ascii
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
   134
	static const relpipe::reader::string_t OPTION_COLOR_SCHEME;
2cc2d3f658f4 configurable table style: rounded, sharp, sharp-double, horizontal-only, ascii
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
   135
	static const relpipe::reader::string_t OPTION_TABLE_STYLE;
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
   136
42
ca216de56ef0 allow setting some options through ENV variables (not only CLI arguments)
František Kučera <franta-hg@frantovo.cz>
parents: 41
diff changeset
   137
	Configuration parse(const std::vector<relpipe::reader::string_t>& cliArguments) {
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
   138
		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
   139
		RelationConfiguration currentRelation;
42
ca216de56ef0 allow setting some options through ENV variables (not only CLI arguments)
František Kučera <franta-hg@frantovo.cz>
parents: 41
diff changeset
   140
ca216de56ef0 allow setting some options through ENV variables (not only CLI arguments)
František Kučera <franta-hg@frantovo.cz>
parents: 41
diff changeset
   141
		const std::vector<relpipe::reader::string_t> arguments = addEnvDefaults(cliArguments, L"relpipe-out-tabular",{OPTION_COLOR_SCHEME, OPTION_TABLE_STYLE});
ca216de56ef0 allow setting some options through ENV variables (not only CLI arguments)
František Kučera <franta-hg@frantovo.cz>
parents: 41
diff changeset
   142
38
2cc2d3f658f4 configurable table style: rounded, sharp, sharp-double, horizontal-only, ascii
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
   143
		// TODO: global configuration of writeTypes, writeRelationName, 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
   144
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
   145
		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
   146
			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
   147
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
   148
			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
   149
				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
   150
				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
   151
			} 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
   152
				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
   153
			} 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
   154
				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
   155
			} 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
   156
				currentRelation.writeRecordCount = parseBoolean(readNext(arguments, i));
38
2cc2d3f658f4 configurable table style: rounded, sharp, sharp-double, horizontal-only, ascii
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
   157
			} else if (option == OPTION_COLOR_SCHEME) {
2cc2d3f658f4 configurable table style: rounded, sharp, sharp-double, horizontal-only, ascii
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
   158
				c.colorScheme = parseColorScheme(readNext(arguments, i));
2cc2d3f658f4 configurable table style: rounded, sharp, sharp-double, horizontal-only, ascii
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
   159
			} else if (option == OPTION_TABLE_STYLE) {
2cc2d3f658f4 configurable table style: rounded, sharp, sharp-double, horizontal-only, ascii
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
   160
				c.tableStyle = parseTableStyle(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
   161
			} 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
   162
		}
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
   163
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
   164
		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
   165
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
   166
		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
   167
	}
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
   168
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
   169
	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
   170
	}
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
   171
};
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
   172
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
   173
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
   174
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
   175
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
   176
const relpipe::reader::string_t CLIParser::OPTION_WRITE_RECORD_COUNT = L"--write-record-count";
38
2cc2d3f658f4 configurable table style: rounded, sharp, sharp-double, horizontal-only, ascii
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
   177
const relpipe::reader::string_t CLIParser::OPTION_COLOR_SCHEME = L"--color-scheme";
2cc2d3f658f4 configurable table style: rounded, sharp, sharp-double, horizontal-only, ascii
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
   178
const relpipe::reader::string_t CLIParser::OPTION_TABLE_STYLE = L"--table-style";
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
   179
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
   180
}
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
   181
}
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
   182
}