src/CLIParser.h
branchv_0
changeset 16 15ee963675af
child 20 90ae67de2f68
equal deleted inserted replaced
15:157bb1d5e08a 16:15ee963675af
       
     1 /**
       
     2  * Relational pipes
       
     3  * Copyright © 2020 František Kučera (Frantovo.cz, GlobalCode.info)
       
     4  *
       
     5  * This program is free software: you can redistribute it and/or modify
       
     6  * it under the terms of the GNU General Public License as published by
       
     7  * the Free Software Foundation, version 3 of the License.
       
     8  *
       
     9  * This program is distributed in the hope that it will be useful,
       
    10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
       
    12  * GNU General Public License for more details.
       
    13  *
       
    14  * You should have received a copy of the GNU General Public License
       
    15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
       
    16  */
       
    17 #pragma once
       
    18 
       
    19 #include <vector>
       
    20 #include <iostream>
       
    21 
       
    22 #include <relpipe/writer/typedefs.h>
       
    23 #include <relpipe/cli/CLI.h>
       
    24 #include <relpipe/cli/RelpipeCLIException.h>
       
    25 
       
    26 #include "Configuration.h"
       
    27 
       
    28 namespace relpipe {
       
    29 namespace in {
       
    30 namespace csv {
       
    31 
       
    32 class CLIParser {
       
    33 private:
       
    34 
       
    35 	relpipe::writer::string_t readNext(const std::vector<relpipe::writer::string_t>& arguments, int& i) {
       
    36 		if (i < arguments.size()) return arguments[i++];
       
    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);
       
    38 	}
       
    39 
       
    40 	/**
       
    41 	 * TODO: use a common method
       
    42 	 */
       
    43 	bool parseBoolean(const relpipe::writer::string_t& value) {
       
    44 		if (value == L"true") return true;
       
    45 		else if (value == L"false") return false;
       
    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);
       
    47 	}
       
    48 
       
    49 	/**
       
    50 	 * TODO: use a common method
       
    51 	 */
       
    52 	relpipe::writer::TypeId parseTypeId(const relpipe::writer::string_t& value) {
       
    53 		using t = relpipe::writer::TypeId;
       
    54 		if (value == L"string") return t::STRING;
       
    55 		else if (value == L"integer") return t::INTEGER;
       
    56 		else if (value == L"boolean") return t::BOOLEAN;
       
    57 		else throw relpipe::cli::RelpipeCLIException(L"Unable to parse TypeId: " + value, relpipe::cli::CLI::EXIT_CODE_BAD_CLI_ARGUMENTS);
       
    58 	}
       
    59 
       
    60 public:
       
    61 
       
    62 	static const relpipe::writer::string_t OPTION_RELATION;
       
    63 	static const relpipe::writer::string_t OPTION_ATTRIBUTE;
       
    64 
       
    65 	Configuration parse(const std::vector<relpipe::writer::string_t>& arguments) {
       
    66 		Configuration c;
       
    67 
       
    68 		for (int i = 0; i < arguments.size();) {
       
    69 			relpipe::writer::string_t option = readNext(arguments, i);
       
    70 
       
    71 			if (option == OPTION_RELATION) {
       
    72 				c.relation = readNext(arguments, i);
       
    73 			} else if (option == OPTION_ATTRIBUTE) {
       
    74 				AttributeRecipe attribute;
       
    75 				attribute.name = readNext(arguments, i);
       
    76 				attribute.type = parseTypeId(readNext(arguments, i));
       
    77 				c.attributes.push_back(attribute);
       
    78 			} else throw relpipe::cli::RelpipeCLIException(L"Unsupported CLI option: " + option, relpipe::cli::CLI::EXIT_CODE_BAD_CLI_ARGUMENTS);
       
    79 		}
       
    80 
       
    81 		return c;
       
    82 	}
       
    83 
       
    84 	virtual ~CLIParser() {
       
    85 	}
       
    86 };
       
    87 
       
    88 const relpipe::writer::string_t CLIParser::OPTION_RELATION = L"--relation";
       
    89 const relpipe::writer::string_t CLIParser::OPTION_ATTRIBUTE = L"--attribute";
       
    90 
       
    91 }
       
    92 }
       
    93 }