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