src/CLIParser.h
author František Kučera <franta-hg@frantovo.cz>
Sun, 17 Apr 2022 21:24:32 +0200
branchv_0
changeset 0 7e986fcf0d8f
permissions -rw-r--r--
establish project
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
7e986fcf0d8f establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     1
/**
7e986fcf0d8f establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     2
 * Relational pipes
7e986fcf0d8f establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     3
 * Copyright © 2022 František Kučera (Frantovo.cz, GlobalCode.info)
7e986fcf0d8f establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     4
 *
7e986fcf0d8f establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     5
 * This program is free software: you can redistribute it and/or modify
7e986fcf0d8f establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     6
 * it under the terms of the GNU General Public License as published by
7e986fcf0d8f establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     7
 * the Free Software Foundation, version 3 of the License.
7e986fcf0d8f establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     8
 *
7e986fcf0d8f establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     9
 * This program is distributed in the hope that it will be useful,
7e986fcf0d8f establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
7e986fcf0d8f establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
7e986fcf0d8f establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    12
 * GNU General Public License for more details.
7e986fcf0d8f establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    13
 *
7e986fcf0d8f establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    14
 * You should have received a copy of the GNU General Public License
7e986fcf0d8f establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    15
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
7e986fcf0d8f establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    16
 */
7e986fcf0d8f establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    17
#pragma once
7e986fcf0d8f establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    18
7e986fcf0d8f establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    19
#include <vector>
7e986fcf0d8f establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    20
7e986fcf0d8f establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    21
#include <relpipe/common/type/typedefs.h>
7e986fcf0d8f establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    22
#include <relpipe/cli/CLI.h>
7e986fcf0d8f establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    23
#include <relpipe/cli/RelpipeCLIException.h>
7e986fcf0d8f establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    24
7e986fcf0d8f establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    25
#include "Configuration.h"
7e986fcf0d8f establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    26
7e986fcf0d8f establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    27
namespace relpipe {
7e986fcf0d8f establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    28
namespace tr {
7e986fcf0d8f establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    29
namespace deserialize {
7e986fcf0d8f establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    30
7e986fcf0d8f establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    31
class CLIParser {
7e986fcf0d8f establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    32
private:
7e986fcf0d8f establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    33
7e986fcf0d8f establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    34
	relpipe::common::type::StringX readNext(std::vector<relpipe::common::type::StringX> arguments, int& i) {
7e986fcf0d8f establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    35
		if (i < arguments.size()) return arguments[i++];
7e986fcf0d8f establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    36
		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);
7e986fcf0d8f establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    37
	}
7e986fcf0d8f establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    38
7e986fcf0d8f establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    39
public:
7e986fcf0d8f establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    40
7e986fcf0d8f establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    41
	static const relpipe::common::type::StringX OPTION_TCP_PORT;
7e986fcf0d8f establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    42
7e986fcf0d8f establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    43
	Configuration parse(const std::vector<relpipe::common::type::StringX>& arguments) {
7e986fcf0d8f establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    44
		Configuration c;
7e986fcf0d8f establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    45
7e986fcf0d8f establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    46
		for (int i = 0; i < arguments.size();) {
7e986fcf0d8f establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    47
			relpipe::common::type::StringX option = readNext(arguments, i);
7e986fcf0d8f establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    48
7e986fcf0d8f establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    49
			if (option == OPTION_TCP_PORT) {
7e986fcf0d8f establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    50
				c.tcpPort = stoi(readNext(arguments, i));
7e986fcf0d8f establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    51
			} else throw relpipe::cli::RelpipeCLIException(L"Unsupported CLI option: " + option, relpipe::cli::CLI::EXIT_CODE_BAD_CLI_ARGUMENTS);
7e986fcf0d8f establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    52
		}
7e986fcf0d8f establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    53
7e986fcf0d8f establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    54
		return c;
7e986fcf0d8f establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    55
	}
7e986fcf0d8f establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    56
7e986fcf0d8f establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    57
	virtual ~CLIParser() {
7e986fcf0d8f establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    58
	}
7e986fcf0d8f establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    59
};
7e986fcf0d8f establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    60
7e986fcf0d8f establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    61
const relpipe::common::type::StringX CLIParser::OPTION_TCP_PORT = L"--tcp-port";
7e986fcf0d8f establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    62
7e986fcf0d8f establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    63
}
7e986fcf0d8f establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    64
}
7e986fcf0d8f establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    65
}