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.

/**
 * Relational pipes
 * Copyright © 2019 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 <relpipe/writer/typedefs.h>
#include <relpipe/cli/CLI.h>
#include <relpipe/cli/RelpipeCLIException.h>

#include "Configuration.h"

namespace relpipe {
namespace tr {
namespace awk {

/**
 * This tr-awk CLI options are inspired by tr-guile options.
 * These configurations are independent and might diverge, but when possible, they should share same option names and same logic for common parts.
 */
class CLIParser {
private:

	// FIXME: move common methods/classes to relpipe-lib-cli or relpipe-lib-helper

	string_t readNext(std::vector<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);
			currentRelation = RelationConfiguration();
		}
	}

	relpipe::writer::TypeId parseTypeId(const string_t& value) {
		using t = relpipe::writer::TypeId;
		if (value == L"string") return t::STRING;
		else if (value == L"integer") return t::INTEGER;
		else if (value == L"boolean") return t::BOOLEAN;
		else throw relpipe::cli::RelpipeCLIException(L"Unable to parse TypeId: " + value, relpipe::cli::CLI::EXIT_CODE_BAD_CLI_ARGUMENTS);
	}

public:

	static const string_t OPTION_RELATION;
	static const string_t OPTION_OUTPUT_ATTRIBUTE;
	static const string_t OPTION_INPUT_ATTRIBUTES_APPEND;
	static const string_t OPTION_INPUT_ATTRIBUTES_PREPEND;
	static const string_t OPTION_DEBUG_VARIABLE_MAPPING;
	static const string_t OPTION_BEFORE_RECORDS;
	static const string_t OPTION_AFTER_RECORDS;
	static const string_t OPTION_FOR_EACH;
	static const string_t OPTION_WHERE;
	static const string_t OPTION_DROP;
	static const string_t OPTION_DEFINE;

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

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

			if (option == OPTION_BEFORE_RECORDS) currentRelation.awkBeforeRecords = readNext(arguments, i);
			else if (option == OPTION_AFTER_RECORDS) currentRelation.awkAfterRecords = readNext(arguments, i);
			else if (option == OPTION_FOR_EACH) currentRelation.awkForEach = readNext(arguments, i);
			else if (option == OPTION_WHERE) currentRelation.awkForEach = L"(" + readNext(arguments, i) + L")";
			else if (option == OPTION_DROP) currentRelation.drop = true;
			else if (option == OPTION_INPUT_ATTRIBUTES_APPEND) currentRelation.inputAttributesAppend = true;
			else if (option == OPTION_INPUT_ATTRIBUTES_PREPEND) currentRelation.inputAttributesPrepend = true;
			else if (option == OPTION_DEBUG_VARIABLE_MAPPING) currentRelation.debugVariableMapping = true;
			else if (option == OPTION_RELATION) {
				addRelation(c, currentRelation); // previous relation
				currentRelation.relation = readNext(arguments, i);
			} else if (option == OPTION_OUTPUT_ATTRIBUTE) {
				relpipe::writer::AttributeMetadata attribute;
				attribute.attributeName = readNext(arguments, i);
				attribute.typeId = parseTypeId(readNext(arguments, i));
				currentRelation.writerMetadata.push_back(attribute);
			} else if (option == OPTION_DEFINE) {
				DefinitionRecipe definition;
				definition.name = readNext(arguments, i);
				definition.type = readNext(arguments, i);
				definition.value = readNext(arguments, i);
				if (currentRelation.relation.size()) currentRelation.definitions.push_back(definition);
				else c.definitions.push_back(definition);
			} 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 string_t CLIParser::OPTION_RELATION = L"--relation";
const string_t CLIParser::OPTION_OUTPUT_ATTRIBUTE = L"--output-attribute";
const string_t CLIParser::OPTION_INPUT_ATTRIBUTES_APPEND = L"--input-attributes-append";
const string_t CLIParser::OPTION_INPUT_ATTRIBUTES_PREPEND = L"--input-attributes-prepend";
const string_t CLIParser::OPTION_DEBUG_VARIABLE_MAPPING = L"--debug-variable-mapping";
const string_t CLIParser::OPTION_BEFORE_RECORDS = L"--before-records";
const string_t CLIParser::OPTION_AFTER_RECORDS = L"--after-records";
const string_t CLIParser::OPTION_FOR_EACH = L"--for-each";
const string_t CLIParser::OPTION_WHERE = L"--where";
const string_t CLIParser::OPTION_DROP = L"--drop";
const string_t CLIParser::OPTION_DEFINE = L"--define";

}
}
}