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