src/CLIParser.h
author František Kučera <franta-hg@frantovo.cz>
Sat, 30 Jul 2022 00:16:40 +0200
branchv_0
changeset 5 e57e2a2798b2
parent 4 8d036e5e5fcc
child 6 f0c51bc4d164
permissions -rw-r--r--
configuration: protocol, role, mode, host, port, path

/**
 * Relational pipes
 * Copyright © 2022 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 <codecvt>

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

#include "Configuration.h"

namespace relpipe {
namespace out {
namespace socket {

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

	relpipe::common::type::StringX readNext(const std::vector<relpipe::common::type::StringX>& 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);
	}

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

	void setIfMissing(std::vector<Configuration::SocketOption>& options, const std::string& name, const std::string& value) {
		auto n = convertor.from_bytes(name);
		auto v = convertor.from_bytes(value);
		for (auto o : options) if (o.name == n) return;
		options.push_back({n, v});
	}

public:

	static const relpipe::common::type::StringX OPTION_RELATION;
	static const relpipe::common::type::StringX OPTION_CONNECTION_STRING;
	static const relpipe::common::type::StringX OPTION_CONNECTION_OPTION;

	Configuration parse(const std::vector<relpipe::common::type::StringX>& arguments) {
		Configuration c;

		for (int i = 0; i < arguments.size();) {
			relpipe::common::type::StringX option = readNext(arguments, i);

			if (option == OPTION_RELATION) {
				c.relation = readNext(arguments, i);
			} else if (option == OPTION_CONNECTION_STRING) {
				auto connectionString = readNext(arguments, i);
				// FIXME: parse connection string and convert it to options
			} else if (option == OPTION_CONNECTION_OPTION) {
				auto name = readNext(arguments, i);
				auto value = readNext(arguments, i);
				c.options.push_back({name, value});
			} else throw relpipe::cli::RelpipeCLIException(L"Unsupported CLI option: " + option, relpipe::cli::CLI::EXIT_CODE_BAD_CLI_ARGUMENTS);
		}

		using namespace options;
		setIfMissing(c.options, OPTION_PROTOCOL, PROTOCOL_UDP);
		setIfMissing(c.options, OPTION_ROLE, ROLE_CLIENT);
		setIfMissing(c.options, OPTION_MODE, MODE_DATAGRAM);
		setIfMissing(c.options, OPTION_HOST, "127.0.0.1");
		setIfMissing(c.options, OPTION_PORT, "64000");

		return c;
	}

	virtual ~CLIParser() {
	}
};

const relpipe::common::type::StringX CLIParser::OPTION_RELATION = L"--relation";
const relpipe::common::type::StringX CLIParser::OPTION_CONNECTION_STRING = L"--connection-string";
const relpipe::common::type::StringX CLIParser::OPTION_CONNECTION_OPTION = L"--connection-option";

}
}
}