src/CLIParser.h
author František Kučera <franta-hg@frantovo.cz>
Fri, 16 Apr 2021 23:18:03 +0200
branchv_0
changeset 4 500ce0b934e7
parent 0 0a0cb749c10f
child 5 8f434dc38444
permissions -rw-r--r--
optional listing of polygon points (more accurate than rectangular box, but requires separate relation): --list-polygon-points
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
0a0cb749c10f projekt skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     1
/**
0a0cb749c10f projekt skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     2
 * Relational pipes
0a0cb749c10f projekt skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     3
 * Copyright © 2021 František Kučera (Frantovo.cz, GlobalCode.info)
0a0cb749c10f projekt skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     4
 *
0a0cb749c10f projekt skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     5
 * This program is free software: you can redistribute it and/or modify
0a0cb749c10f projekt skeleton
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
0a0cb749c10f projekt skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     7
 * the Free Software Foundation, version 3 of the License.
0a0cb749c10f projekt skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     8
 *
0a0cb749c10f projekt skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     9
 * This program is distributed in the hope that it will be useful,
0a0cb749c10f projekt skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
0a0cb749c10f projekt skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
0a0cb749c10f projekt skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    12
 * GNU General Public License for more details.
0a0cb749c10f projekt skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    13
 *
0a0cb749c10f projekt skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    14
 * You should have received a copy of the GNU General Public License
0a0cb749c10f projekt skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    15
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
0a0cb749c10f projekt skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    16
 */
0a0cb749c10f projekt skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    17
#pragma once
0a0cb749c10f projekt skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    18
0a0cb749c10f projekt skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    19
#include <vector>
0a0cb749c10f projekt skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    20
#include <iostream>
0a0cb749c10f projekt skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    21
0a0cb749c10f projekt skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    22
#include <relpipe/common/type/typedefs.h>
0a0cb749c10f projekt skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    23
#include <relpipe/cli/CLI.h>
0a0cb749c10f projekt skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    24
#include <relpipe/cli/RelpipeCLIException.h>
0a0cb749c10f projekt skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    25
0a0cb749c10f projekt skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    26
#include "Configuration.h"
0a0cb749c10f projekt skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    27
0a0cb749c10f projekt skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    28
namespace relpipe {
0a0cb749c10f projekt skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    29
namespace in {
0a0cb749c10f projekt skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    30
namespace qr {
0a0cb749c10f projekt skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    31
0a0cb749c10f projekt skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    32
class CLIParser {
0a0cb749c10f projekt skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    33
private:
0a0cb749c10f projekt skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    34
0a0cb749c10f projekt skeleton
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) {
0a0cb749c10f projekt skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    36
		if (i < arguments.size()) return arguments[i++];
0a0cb749c10f projekt skeleton
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);
0a0cb749c10f projekt skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    38
	}
0a0cb749c10f projekt skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    39
0a0cb749c10f projekt skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    40
	/**
0a0cb749c10f projekt skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    41
	 * TODO: use a common method
0a0cb749c10f projekt skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    42
	 */
0a0cb749c10f projekt skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    43
	bool parseBoolean(const relpipe::common::type::StringX& value) {
0a0cb749c10f projekt skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    44
		if (value == L"true") return true;
0a0cb749c10f projekt skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    45
		else if (value == L"false") return false;
0a0cb749c10f projekt skeleton
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);
0a0cb749c10f projekt skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    47
	}
0a0cb749c10f projekt skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    48
0a0cb749c10f projekt skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    49
public:
0a0cb749c10f projekt skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    50
4
500ce0b934e7 optional listing of polygon points (more accurate than rectangular box, but requires separate relation): --list-polygon-points
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    51
	static const relpipe::common::type::StringX OPTION_LIST_POLYGON_POINTS;
0
0a0cb749c10f projekt skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    52
	static const relpipe::common::type::StringX OPTION_LIST_ZZZ;
0a0cb749c10f projekt skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    53
0a0cb749c10f projekt skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    54
	Configuration parse(const std::vector<relpipe::common::type::StringX>& arguments) {
0a0cb749c10f projekt skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    55
		Configuration c;
0a0cb749c10f projekt skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    56
0a0cb749c10f projekt skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    57
		for (int i = 0; i < arguments.size();) {
0a0cb749c10f projekt skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    58
			relpipe::common::type::StringX option = readNext(arguments, i);
0a0cb749c10f projekt skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    59
4
500ce0b934e7 optional listing of polygon points (more accurate than rectangular box, but requires separate relation): --list-polygon-points
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    60
			if (option == OPTION_LIST_POLYGON_POINTS) {
500ce0b934e7 optional listing of polygon points (more accurate than rectangular box, but requires separate relation): --list-polygon-points
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    61
				c.listPolygonPoints = parseBoolean(readNext(arguments, i));
0
0a0cb749c10f projekt skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    62
			} else throw relpipe::cli::RelpipeCLIException(L"Unsupported CLI option: " + option, relpipe::cli::CLI::EXIT_CODE_BAD_CLI_ARGUMENTS);
0a0cb749c10f projekt skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    63
		}
0a0cb749c10f projekt skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    64
0a0cb749c10f projekt skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    65
		return c;
0a0cb749c10f projekt skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    66
	}
0a0cb749c10f projekt skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    67
0a0cb749c10f projekt skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    68
	virtual ~CLIParser() {
0a0cb749c10f projekt skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    69
	}
0a0cb749c10f projekt skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    70
};
0a0cb749c10f projekt skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    71
4
500ce0b934e7 optional listing of polygon points (more accurate than rectangular box, but requires separate relation): --list-polygon-points
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    72
const relpipe::common::type::StringX CLIParser::OPTION_LIST_POLYGON_POINTS = L"--list-polygon-points";
0
0a0cb749c10f projekt skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    73
0a0cb749c10f projekt skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    74
}
0a0cb749c10f projekt skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    75
}
0a0cb749c10f projekt skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    76
}