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)

/**
 * Relational pipes
 * Copyright © 2020 František Kučera (Frantovo.cz, GlobalCode.info)
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, version 3 of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
#pragma once

#include <vector>
#include <iostream>

#include <relpipe/reader/typedefs.h>
#include <relpipe/cli/CLI.h>
#include <relpipe/cli/RelpipeCLIException.h>

#include "Configuration.h"

namespace relpipe {
namespace out {
namespace tabular {

class CLIParser {
private:

	std::wstring_convert<std::codecvt_utf8<wchar_t>> convertor; // TODO: support also other encodings.

	relpipe::reader::string_t readNext(const std::vector<relpipe::reader::string_t>& arguments, int& i) {
		if (i < arguments.size()) return arguments[i++];
		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);
	}

	void addRelation(Configuration& c, RelationConfiguration& currentRelation) {
		if (currentRelation.relation.size()) {
			c.relationConfigurations.push_back(currentRelation);
		} else {
			// no relation name given → global configuration
			c.writeTypes = currentRelation.writeTypes;
			c.writeRelationName = currentRelation.writeRelationName;
			c.writeRecordCount = currentRelation.writeRecordCount;
		}
		currentRelation = RelationConfiguration();
	}

	/**
	 * TODO: use a common method
	 */
	bool parseBoolean(const relpipe::reader::string_t& value) {
		if (value == L"true") return true;
		else if (value == L"false") return false;
		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);
	}

	Configuration::ColorScheme parseColorScheme(const relpipe::reader::string_t& value) {
		if (value == L"greenish") return Configuration::ColorScheme::Greenish;
		else if (value == L"amberish") return Configuration::ColorScheme::Amberish;
		else if (value == L"black-and-white") return Configuration::ColorScheme::BlackAndWhite;
		else if (value == L"midnight") return Configuration::ColorScheme::Midnight;
		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);
	}

	Configuration::TableStyle parseTableStyle(const relpipe::reader::string_t& value) {
		if (value == L"rounded") return Configuration::TableStyle::Rounded;
		else if (value == L"sharp") return Configuration::TableStyle::Sharp;
		else if (value == L"sharp-double") return Configuration::TableStyle::SharpDouble;
		else if (value == L"horizontal-only") return Configuration::TableStyle::HorizontalOnly;
		else if (value == L"ascii") return Configuration::TableStyle::Ascii;
		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);
	}

	/**
	 * TODO: move to common parent class in relpipe-lib-cli
	 * 
	 * @param cliArguments original CLI arguments
	 * @param moduleName name of this program e.g. "relpipe-out-tabular"
	 * @param supportedOptions options that could be specified also as environmental variables, not only CLI arguments, including the "--" prefix e.g. "--color-scheme"
	 * @return defaults found in ENV + given CLI arguments
	 */
	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) {
		std::vector<relpipe::reader::string_t> allArguments;

		auto toEnv = [](char ch) {
			return ch == '-' ? '_' : ::toupper(ch);
		};

		std::string envPrefix = convertor.to_bytes(moduleName);
		transform(envPrefix.begin(), envPrefix.end(), envPrefix.begin(), toEnv);
		envPrefix.append("_");

		for (auto o : supportedOptions) {
			if (o.substr(0, 2) == L"--") {
				std::string option = convertor.to_bytes(o);
				option.erase(0, 2);
				transform(option.begin(), option.end(), option.begin(), toEnv);

				// FIXME: check argument counts (should be specified alongside with supportedOptions) to avoid injection of unwanted options through ENV variables
				// TODO: allow repeated options?
				for (int i = -1; i < 10; i++) {
					std::string envName = i == -1 ? envPrefix + option : envPrefix + option + "_" + std::to_string(i);
					const char* value = std::getenv(envName.c_str());
					if (value) {
						if (i == -1 || i == 0) allArguments.push_back(o);
						allArguments.push_back(convertor.from_bytes(value));
						if (i == -1) break;
					} else {
						if (i > -1) break;
					}
				}
			} else {
				throw std::logic_error("supportedOptions must start with '--'");
			}
		}

		for (auto a : cliArguments) allArguments.push_back(a);

		return allArguments;
	}

public:

	static const relpipe::reader::string_t OPTION_RELATION;
	static const relpipe::reader::string_t OPTION_WRITE_TYPES;
	static const relpipe::reader::string_t OPTION_WRITE_RELATION_NAME;
	static const relpipe::reader::string_t OPTION_WRITE_RECORD_COUNT;
	static const relpipe::reader::string_t OPTION_COLOR_SCHEME;
	static const relpipe::reader::string_t OPTION_TABLE_STYLE;

	Configuration parse(const std::vector<relpipe::reader::string_t>& cliArguments) {
		Configuration c;
		RelationConfiguration currentRelation;

		const std::vector<relpipe::reader::string_t> arguments = addEnvDefaults(cliArguments, L"relpipe-out-tabular",{OPTION_COLOR_SCHEME, OPTION_TABLE_STYLE});

		// TODO: global configuration of writeTypes, writeRelationName, writeRecordCount

		for (int i = 0; i < arguments.size();) {
			relpipe::reader::string_t option = readNext(arguments, i);

			if (option == OPTION_RELATION) {
				addRelation(c, currentRelation); // previous relation
				currentRelation.relation = readNext(arguments, i);
			} else if (option == OPTION_WRITE_TYPES) {
				currentRelation.writeTypes = parseBoolean(readNext(arguments, i));
			} else if (option == OPTION_WRITE_RELATION_NAME) {
				currentRelation.writeRelationName = parseBoolean(readNext(arguments, i));
			} else if (option == OPTION_WRITE_RECORD_COUNT) {
				currentRelation.writeRecordCount = parseBoolean(readNext(arguments, i));
			} else if (option == OPTION_COLOR_SCHEME) {
				c.colorScheme = parseColorScheme(readNext(arguments, i));
			} else if (option == OPTION_TABLE_STYLE) {
				c.tableStyle = parseTableStyle(readNext(arguments, i));
			} else throw relpipe::cli::RelpipeCLIException(L"Unsupported CLI option: " + option, relpipe::cli::CLI::EXIT_CODE_BAD_CLI_ARGUMENTS);
		}

		addRelation(c, currentRelation); // last relation

		return c;
	}

	virtual ~CLIParser() {
	}
};

const relpipe::reader::string_t CLIParser::OPTION_RELATION = L"--relation";
const relpipe::reader::string_t CLIParser::OPTION_WRITE_TYPES = L"--write-types";
const relpipe::reader::string_t CLIParser::OPTION_WRITE_RELATION_NAME = L"--write-relation-name";
const relpipe::reader::string_t CLIParser::OPTION_WRITE_RECORD_COUNT = L"--write-record-count";
const relpipe::reader::string_t CLIParser::OPTION_COLOR_SCHEME = L"--color-scheme";
const relpipe::reader::string_t CLIParser::OPTION_TABLE_STYLE = L"--table-style";

}
}
}