src/CLIParser.h
author František Kučera <franta-hg@frantovo.cz>
Mon, 26 Jul 2021 23:41:18 +0200
branchv_0
changeset 6 523c9c39c996
parent 0 28294b895e5e
permissions -rw-r--r--
RelpipeASN1ContentHandler: parse one or more relations
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
28294b895e5e project skeleton + configuration
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     1
/**
28294b895e5e project skeleton + configuration
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     2
 * Relational pipes
28294b895e5e project skeleton + configuration
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     3
 * Copyright © 2021 František Kučera (Frantovo.cz, GlobalCode.info)
28294b895e5e project skeleton + configuration
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     4
 *
28294b895e5e project skeleton + configuration
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     5
 * This program is free software: you can redistribute it and/or modify
28294b895e5e project skeleton + configuration
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
28294b895e5e project skeleton + configuration
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     7
 * the Free Software Foundation, version 3 of the License.
28294b895e5e project skeleton + configuration
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     8
 *
28294b895e5e project skeleton + configuration
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     9
 * This program is distributed in the hope that it will be useful,
28294b895e5e project skeleton + configuration
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
28294b895e5e project skeleton + configuration
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28294b895e5e project skeleton + configuration
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    12
 * GNU General Public License for more details.
28294b895e5e project skeleton + configuration
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    13
 *
28294b895e5e project skeleton + configuration
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    14
 * You should have received a copy of the GNU General Public License
28294b895e5e project skeleton + configuration
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    15
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
28294b895e5e project skeleton + configuration
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    16
 */
28294b895e5e project skeleton + configuration
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    17
#pragma once
28294b895e5e project skeleton + configuration
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    18
28294b895e5e project skeleton + configuration
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    19
#include <vector>
28294b895e5e project skeleton + configuration
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    20
#include <iostream>
28294b895e5e project skeleton + configuration
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    21
28294b895e5e project skeleton + configuration
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    22
#include <relpipe/writer/typedefs.h>
28294b895e5e project skeleton + configuration
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    23
#include <relpipe/cli/CLI.h>
28294b895e5e project skeleton + configuration
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    24
#include <relpipe/cli/RelpipeCLIException.h>
28294b895e5e project skeleton + configuration
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    25
28294b895e5e project skeleton + configuration
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    26
#include "Configuration.h"
28294b895e5e project skeleton + configuration
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    27
28294b895e5e project skeleton + configuration
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    28
namespace relpipe {
28294b895e5e project skeleton + configuration
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    29
namespace in {
28294b895e5e project skeleton + configuration
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    30
namespace asn1 {
28294b895e5e project skeleton + configuration
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    31
28294b895e5e project skeleton + configuration
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    32
class CLIParser {
28294b895e5e project skeleton + configuration
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    33
private:
28294b895e5e project skeleton + configuration
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    34
28294b895e5e project skeleton + configuration
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    35
	relpipe::writer::string_t readNext(const std::vector<relpipe::writer::string_t>& arguments, int& i) {
28294b895e5e project skeleton + configuration
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    36
		if (i < arguments.size()) return arguments[i++];
28294b895e5e project skeleton + configuration
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);
28294b895e5e project skeleton + configuration
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    38
	}
28294b895e5e project skeleton + configuration
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    39
28294b895e5e project skeleton + configuration
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    40
	Mode parseMode(const relpipe::writer::string_t& value) {
28294b895e5e project skeleton + configuration
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    41
		if (value == L"relpipe") return Mode::Relpipe;
28294b895e5e project skeleton + configuration
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    42
		else if (value == L"freeform") return Mode::Freeform;
28294b895e5e project skeleton + configuration
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    43
		else throw relpipe::cli::RelpipeCLIException(L"Unable to parse Mode value: " + value + L" (expecting relpipe or freeform)", relpipe::cli::CLI::EXIT_CODE_BAD_CLI_ARGUMENTS);
28294b895e5e project skeleton + configuration
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    44
	}
28294b895e5e project skeleton + configuration
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    45
28294b895e5e project skeleton + configuration
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    46
public:
28294b895e5e project skeleton + configuration
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    47
28294b895e5e project skeleton + configuration
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    48
	static const relpipe::writer::string_t OPTION_RELATION;
28294b895e5e project skeleton + configuration
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    49
	static const relpipe::writer::string_t OPTION_PARSER_OPTION;
28294b895e5e project skeleton + configuration
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    50
	static const relpipe::writer::string_t OPTION_MODE;
28294b895e5e project skeleton + configuration
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    51
28294b895e5e project skeleton + configuration
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    52
	Configuration parse(const std::vector<relpipe::writer::string_t>& arguments) {
28294b895e5e project skeleton + configuration
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    53
		Configuration c;
28294b895e5e project skeleton + configuration
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    54
28294b895e5e project skeleton + configuration
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    55
		for (int i = 0; i < arguments.size();) {
28294b895e5e project skeleton + configuration
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    56
			relpipe::writer::string_t option = readNext(arguments, i);
28294b895e5e project skeleton + configuration
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    57
28294b895e5e project skeleton + configuration
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    58
			if (option == OPTION_RELATION) c.relation = readNext(arguments, i);
28294b895e5e project skeleton + configuration
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    59
			else if (option == OPTION_PARSER_OPTION) c.parserOptions.push_back({readNext(arguments, i), readNext(arguments, i)});
28294b895e5e project skeleton + configuration
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    60
			else if (option == OPTION_MODE) c.mode = parseMode(readNext(arguments, i));
28294b895e5e project skeleton + configuration
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    61
			else throw relpipe::cli::RelpipeCLIException(L"Unsupported CLI option: " + option, relpipe::cli::CLI::EXIT_CODE_BAD_CLI_ARGUMENTS);
28294b895e5e project skeleton + configuration
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    62
		}
28294b895e5e project skeleton + configuration
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    63
28294b895e5e project skeleton + configuration
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    64
		return c;
28294b895e5e project skeleton + configuration
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    65
	}
28294b895e5e project skeleton + configuration
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    66
28294b895e5e project skeleton + configuration
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    67
	virtual ~CLIParser() {
28294b895e5e project skeleton + configuration
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    68
	}
28294b895e5e project skeleton + configuration
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    69
};
28294b895e5e project skeleton + configuration
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    70
28294b895e5e project skeleton + configuration
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    71
const relpipe::writer::string_t CLIParser::OPTION_RELATION = L"--relation";
28294b895e5e project skeleton + configuration
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    72
const relpipe::writer::string_t CLIParser::OPTION_PARSER_OPTION = L"--parser-option";
28294b895e5e project skeleton + configuration
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    73
const relpipe::writer::string_t CLIParser::OPTION_MODE = L"--mode";
28294b895e5e project skeleton + configuration
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    74
28294b895e5e project skeleton + configuration
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    75
}
28294b895e5e project skeleton + configuration
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    76
}
28294b895e5e project skeleton + configuration
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    77
}