src/CLIParser.h
author František Kučera <franta-hg@frantovo.cz>
Fri, 06 May 2022 21:54:32 +0200
branchv_0
changeset 0 7ef5ce9477c8
child 2 1a0fbd17db13
permissions -rw-r--r--
establish project
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
7ef5ce9477c8 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     1
/**
7ef5ce9477c8 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     2
 * Relational pipes
7ef5ce9477c8 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     3
 * Copyright © 2022 František Kučera (Frantovo.cz, GlobalCode.info)
7ef5ce9477c8 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     4
 *
7ef5ce9477c8 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
7ef5ce9477c8 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
7ef5ce9477c8 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     7
 * the Free Software Foundation, version 3 of the License.
7ef5ce9477c8 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     8
 *
7ef5ce9477c8 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,
7ef5ce9477c8 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
7ef5ce9477c8 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
7ef5ce9477c8 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    12
 * GNU General Public License for more details.
7ef5ce9477c8 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    13
 *
7ef5ce9477c8 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
7ef5ce9477c8 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/>.
7ef5ce9477c8 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    16
 */
7ef5ce9477c8 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    17
#pragma once
7ef5ce9477c8 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    18
7ef5ce9477c8 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    19
#include <vector>
7ef5ce9477c8 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    20
#include <iostream>
7ef5ce9477c8 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    21
7ef5ce9477c8 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    22
#include <relpipe/common/type/typedefs.h>
7ef5ce9477c8 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    23
#include <relpipe/cli/CLI.h>
7ef5ce9477c8 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    24
#include <relpipe/cli/RelpipeCLIException.h>
7ef5ce9477c8 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    25
7ef5ce9477c8 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    26
#include "Configuration.h"
7ef5ce9477c8 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    27
7ef5ce9477c8 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    28
namespace relpipe {
7ef5ce9477c8 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    29
namespace out {
7ef5ce9477c8 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    30
namespace mqtt {
7ef5ce9477c8 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    31
7ef5ce9477c8 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    32
class CLIParser {
7ef5ce9477c8 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    33
private:
7ef5ce9477c8 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    34
7ef5ce9477c8 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    35
	relpipe::common::type::StringX readNext(const std::vector<relpipe::common::type::StringX>& arguments, int& i) {
7ef5ce9477c8 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    36
		if (i < arguments.size()) return arguments[i++];
7ef5ce9477c8 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    37
		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);
7ef5ce9477c8 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    38
	}
7ef5ce9477c8 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    39
7ef5ce9477c8 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    40
	/**
7ef5ce9477c8 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    41
	 * TODO: use a common method
7ef5ce9477c8 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    42
	 */
7ef5ce9477c8 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    43
	bool parseBoolean(const relpipe::common::type::StringX& value) {
7ef5ce9477c8 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    44
		if (value == L"true") return true;
7ef5ce9477c8 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    45
		else if (value == L"false") return false;
7ef5ce9477c8 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    46
		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);
7ef5ce9477c8 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    47
	}
7ef5ce9477c8 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    48
7ef5ce9477c8 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    49
public:
7ef5ce9477c8 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    50
7ef5ce9477c8 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    51
	static const relpipe::common::type::StringX OPTION_RELATION;
7ef5ce9477c8 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    52
	static const relpipe::common::type::StringX OPTION_UNLINK_ON_CLOSE;
7ef5ce9477c8 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    53
	static const relpipe::common::type::StringX OPTION_QUEUE;
7ef5ce9477c8 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    54
7ef5ce9477c8 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    55
	Configuration parse(const std::vector<relpipe::common::type::StringX>& arguments) {
7ef5ce9477c8 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    56
		Configuration c;
7ef5ce9477c8 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    57
7ef5ce9477c8 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    58
		for (int i = 0; i < arguments.size();) {
7ef5ce9477c8 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    59
			relpipe::common::type::StringX option = readNext(arguments, i);
7ef5ce9477c8 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    60
7ef5ce9477c8 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    61
			if (option == OPTION_RELATION) {
7ef5ce9477c8 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    62
				c.relation = readNext(arguments, i);
7ef5ce9477c8 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    63
			} else if (option == OPTION_UNLINK_ON_CLOSE) {
7ef5ce9477c8 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    64
				c.unlinkOnClose = parseBoolean(readNext(arguments, i));
7ef5ce9477c8 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    65
			} else if (option == OPTION_QUEUE) {
7ef5ce9477c8 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    66
				c.queue = readNext(arguments, i);
7ef5ce9477c8 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    67
			} else throw relpipe::cli::RelpipeCLIException(L"Unsupported CLI option: " + option, relpipe::cli::CLI::EXIT_CODE_BAD_CLI_ARGUMENTS);
7ef5ce9477c8 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    68
		}
7ef5ce9477c8 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    69
7ef5ce9477c8 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    70
		return c;
7ef5ce9477c8 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    71
	}
7ef5ce9477c8 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    72
7ef5ce9477c8 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    73
	virtual ~CLIParser() {
7ef5ce9477c8 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    74
	}
7ef5ce9477c8 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    75
};
7ef5ce9477c8 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    76
7ef5ce9477c8 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    77
const relpipe::common::type::StringX CLIParser::OPTION_RELATION = L"--relation";
7ef5ce9477c8 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    78
const relpipe::common::type::StringX CLIParser::OPTION_UNLINK_ON_CLOSE = L"--unlink-on-close";
7ef5ce9477c8 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    79
const relpipe::common::type::StringX CLIParser::OPTION_QUEUE = L"--queue";
7ef5ce9477c8 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    80
7ef5ce9477c8 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    81
}
7ef5ce9477c8 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    82
}
7ef5ce9477c8 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    83
}