src/CLIParser.h
author František Kučera <franta-hg@frantovo.cz>
Thu, 24 Sep 2020 20:42:34 +0200
branchv_0
changeset 47 329757999664
parent 46 ab27422a34b0
permissions -rw-r--r--
fail-fast: report missing --relation and --attribute early and avoid crashing without any useful error message

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

#include "Configuration.h"

namespace relpipe {
namespace in {
namespace cli {

class CLIParser {
private:

	relpipe::writer::string_t readNext(const std::vector<relpipe::writer::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 readNextRecord(const std::vector<relpipe::writer::string_t>& arguments, int& i, RelationConfiguration& currentRelation) {
		for (const auto& a : currentRelation.attributes) currentRelation.values.emplace_back(readNext(arguments, i));
	}

	void addRelation(Configuration& c, RelationConfiguration& currentRelation) {
		if (currentRelation.relation.size()) {
			c.relationConfigurations.push_back(currentRelation);
			currentRelation = RelationConfiguration();
		}
	}

	/**
	 * TODO: use a common method
	 */
	bool parseBoolean(const relpipe::writer::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);
	}

	/**
	 * TODO: use a common method
	 */
	relpipe::writer::TypeId parseTypeId(const relpipe::writer::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 relpipe::writer::string_t OPTION_RELATION;
	static const relpipe::writer::string_t OPTION_WRITE_HEADER;
	static const relpipe::writer::string_t OPTION_ATTRIBUTE;
	static const relpipe::writer::string_t OPTION_RECORD;
	static const relpipe::writer::string_t OPTION_RECORDS;
	static const relpipe::writer::string_t OPTION_RECORDS_STDIN;

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

		for (int i = 0; i < arguments.size();) {
			relpipe::writer::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_HEADER) {
				currentRelation.writeHeader = parseBoolean(readNext(arguments, i));
			} else if (option == OPTION_ATTRIBUTE) {
				AttributeRecipe attribute;
				attribute.name = readNext(arguments, i);
				attribute.type = parseTypeId(readNext(arguments, i));
				currentRelation.attributes.push_back(attribute);
			} else if (option == OPTION_RECORD) {
				readNextRecord(arguments, i, currentRelation);
			} else if (option == OPTION_RECORDS) {
				while (i < arguments.size()) readNextRecord(arguments, i, currentRelation);
			} else if (option == OPTION_RECORDS_STDIN) {
				if (parseBoolean(readNext(arguments, i))) {
					for (RelationConfiguration r : c.relationConfigurations) if (r.valueStream) throw relpipe::cli::RelpipeCLIException(L"Only one relation can read data from STDIN.", relpipe::cli::CLI::EXIT_CODE_BAD_CLI_ARGUMENTS);
					currentRelation.valueStream = &std::cin;
				}
			} else throw relpipe::cli::RelpipeCLIException(L"Unsupported CLI option: " + option, relpipe::cli::CLI::EXIT_CODE_BAD_CLI_ARGUMENTS);
		}
		addRelation(c, currentRelation); // last relation

		if (c.relationConfigurations.size() == 0) throw relpipe::cli::RelpipeCLIException(L"There must be at least one relation. Use the " + OPTION_RELATION + L" option.", relpipe::cli::CLI::EXIT_CODE_BAD_CLI_ARGUMENTS);
		for (auto& rc : c.relationConfigurations) if (rc.attributes.size() == 0) throw relpipe::cli::RelpipeCLIException(L"There must be at least one attribute. Use the " + OPTION_ATTRIBUTE + L" option.", relpipe::cli::CLI::EXIT_CODE_BAD_CLI_ARGUMENTS);

		return c;
	}

	virtual ~CLIParser() {
	}
};

const relpipe::writer::string_t CLIParser::OPTION_RELATION = L"--relation";
const relpipe::writer::string_t CLIParser::OPTION_WRITE_HEADER = L"--write-header";
const relpipe::writer::string_t CLIParser::OPTION_ATTRIBUTE = L"--attribute";
const relpipe::writer::string_t CLIParser::OPTION_RECORD = L"--record";
const relpipe::writer::string_t CLIParser::OPTION_RECORDS = L"--records";
const relpipe::writer::string_t CLIParser::OPTION_RECORDS_STDIN = L"--records-on-stdin";

}
}
}