src/CLIParser.h
author František Kučera <franta-hg@frantovo.cz>
Sun, 08 May 2022 21:42:25 +0200
branchv_0
changeset 2 1a0fbd17db13
parent 0 7ef5ce9477c8
child 3 62ced7c41c67
permissions -rw-r--r--
first version
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
public:
7ef5ce9477c8 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    41
7ef5ce9477c8 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    42
	static const relpipe::common::type::StringX OPTION_RELATION;
2
1a0fbd17db13 first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    43
	static const relpipe::common::type::StringX OPTION_STREAM;
0
7ef5ce9477c8 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    44
7ef5ce9477c8 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    45
	Configuration parse(const std::vector<relpipe::common::type::StringX>& arguments) {
7ef5ce9477c8 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    46
		Configuration c;
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
		for (int i = 0; i < arguments.size();) {
7ef5ce9477c8 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    49
			relpipe::common::type::StringX option = readNext(arguments, i);
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
			if (option == OPTION_RELATION) {
7ef5ce9477c8 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    52
				c.relation = readNext(arguments, i);
2
1a0fbd17db13 first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    53
			} else if (option == OPTION_STREAM) {
1a0fbd17db13 first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    54
				c.stream = readNext(arguments, i);
0
7ef5ce9477c8 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    55
			} 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
    56
		}
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
		return c;
7ef5ce9477c8 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    59
	}
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
	virtual ~CLIParser() {
7ef5ce9477c8 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    62
	}
7ef5ce9477c8 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    63
};
7ef5ce9477c8 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    64
7ef5ce9477c8 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    65
const relpipe::common::type::StringX CLIParser::OPTION_RELATION = L"--relation";
2
1a0fbd17db13 first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    66
const relpipe::common::type::StringX CLIParser::OPTION_STREAM = L"--stream";
0
7ef5ce9477c8 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    67
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
}