src/CLIParser.h
author František Kučera <franta-hg@frantovo.cz>
Wed, 09 Dec 2020 17:55:03 +0100
branchv_0
changeset 0 1bb084f84eb9
child 1 e04e5bbc147b
permissions -rw-r--r--
project skeleton
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     1
/**
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     2
 * Relational pipes
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     3
 * Copyright © 2020 František Kučera (Frantovo.cz, GlobalCode.info)
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     4
 *
1bb084f84eb9 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
1bb084f84eb9 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
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     7
 * the Free Software Foundation, version 3 of the License.
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     8
 *
1bb084f84eb9 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,
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    12
 * GNU General Public License for more details.
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    13
 *
1bb084f84eb9 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
1bb084f84eb9 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/>.
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    16
 */
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    17
#pragma once
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    18
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    19
#include <vector>
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    20
#include <iostream>
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    21
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    22
#include <relpipe/common/type/typedefs.h>
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    23
#include <relpipe/cli/CLI.h>
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    24
#include <relpipe/cli/RelpipeCLIException.h>
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    25
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    26
#include "Configuration.h"
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    27
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    28
namespace relpipe {
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    29
namespace out {
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    30
namespace ini {
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    31
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    32
class CLIParser {
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    33
private:
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    34
1bb084f84eb9 project 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) {
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    36
		if (i < arguments.size()) return arguments[i++];
1bb084f84eb9 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);
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    38
	}
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    39
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    40
	/**
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    41
	 * TODO: use a common method
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    42
	 */
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    43
	bool parseBoolean(const relpipe::common::type::StringX& value) {
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    44
		if (value == L"true") return true;
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    45
		else if (value == L"false") return false;
1bb084f84eb9 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);
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    47
	}
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    48
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    49
	void addRelation(Configuration& c, RelationConfiguration& currentRelation) {
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    50
		if (currentRelation.relation.size()) {
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    51
			c.relationConfigurations.push_back(currentRelation);
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    52
			currentRelation = RelationConfiguration();
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    53
		}
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    54
	}
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    55
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    56
	Style parseStyle(relpipe::common::type::StringX style) {
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    57
		if (style == L"auto") return Style::Automatic;
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    58
		else if (style == L"dropped") return Style::Dropped;
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    59
		else if (style == L"literal") return Style::Literal;
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    60
		else if (style == L"literal-with-section-attribute") return Style::LiteralWithSectionAttribute; // TODO: better names
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    61
		else if (style == L"standard") return Style::Standard;
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    62
		else throw relpipe::cli::RelpipeCLIException(L"Unsupported style option: " + style, relpipe::cli::CLI::EXIT_CODE_BAD_CLI_ARGUMENTS);
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    63
	}
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    64
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    65
public:
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    66
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    67
	static const relpipe::common::type::StringX OPTION_RELATION;
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    68
	static const relpipe::common::type::StringX OPTION_STYLE;
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    69
	static const relpipe::common::type::StringX OPTION_WRITER_OPTION;
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    70
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    71
	Configuration parse(const std::vector<relpipe::common::type::StringX>& arguments) {
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    72
		Configuration c;
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    73
		RelationConfiguration currentRelation;
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    74
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    75
		for (int i = 0; i < arguments.size();) {
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    76
			relpipe::common::type::StringX option = readNext(arguments, i);
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    77
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    78
			if (option == OPTION_RELATION) {
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    79
				addRelation(c, currentRelation); // previous relation
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    80
				currentRelation.relation = readNext(arguments, i);
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    81
			} else if (option == OPTION_STYLE) {
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    82
				currentRelation.style = parseStyle(readNext(arguments, i));
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    83
			} else if (option == OPTION_WRITER_OPTION) {
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    84
				c.writerOptions.push_back({readNext(arguments, i), readNext(arguments, i)});
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    85
			} else throw relpipe::cli::RelpipeCLIException(L"Unsupported CLI option: " + option, relpipe::cli::CLI::EXIT_CODE_BAD_CLI_ARGUMENTS);
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    86
		}
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    87
		addRelation(c, currentRelation); // last relation
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    88
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    89
		return c;
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    90
	}
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    91
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    92
	virtual ~CLIParser() {
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    93
	}
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    94
};
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    95
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    96
const relpipe::common::type::StringX CLIParser::OPTION_RELATION = L"--relation";
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    97
const relpipe::common::type::StringX CLIParser::OPTION_STYLE = L"--style";
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    98
const relpipe::common::type::StringX CLIParser::OPTION_WRITER_OPTION = L"--writer-option";
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    99
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   100
}
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   101
}
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   102
}