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