src/CLIParser.h
author František Kučera <franta-hg@frantovo.cz>
Tue, 22 Oct 2019 19:53:46 +0200
branchv_0
changeset 4 a0689654b3c2
parent 2 0d3eb5129582
child 7 ff69af3c67a3
permissions -rw-r--r--
fix license version: GNU GPLv3

/**
 * 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 in {
namespace xmltable {

class CLIParser {
private:

	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_NAMESPACE;
	static const string_t OPTION_RELATION;
	static const string_t OPTION_NAME_IS_XPATH;
	static const string_t OPTION_RECORDS;
	static const string_t OPTION_ATTRIBUTE;

	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_NAMESPACE) {
				c.namespaceMappings.push_back(readNext(arguments, i));
				c.namespaceMappings.push_back(readNext(arguments, i));
			} else if (option == OPTION_RELATION) {
				addRelation(c, currentRelation); // previous relation
				currentRelation.relation = readNext(arguments, i);
			} else if (option == OPTION_NAME_IS_XPATH) {
				currentRelation.nameIsXPath = true;
			} else if (option == OPTION_RECORDS) {
				currentRelation.xpath = readNext(arguments, i);
			} else if (option == OPTION_ATTRIBUTE) {
				AttributeRecipe attribute;
				attribute.name = readNext(arguments, i);
				attribute.type = parseTypeId(readNext(arguments, i));
				attribute.xpath = readNext(arguments, i);
				currentRelation.attributes.push_back(attribute);
			} 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_NAMESPACE = L"--namespace";
const string_t CLIParser::OPTION_RELATION = L"--relation";
const string_t CLIParser::OPTION_NAME_IS_XPATH = L"--name-is-xpath";
const string_t CLIParser::OPTION_RECORDS = L"--records";
const string_t CLIParser::OPTION_ATTRIBUTE = L"--attribute";

}
}
}