src/CLIParser.h
branchv_0
changeset 30 3c6374467a82
child 31 c22577615ce4
equal deleted inserted replaced
29:249a66b6e34e 30:3c6374467a82
       
     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/reader/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 out {
       
    30 namespace tabular {
       
    31 
       
    32 class CLIParser {
       
    33 private:
       
    34 
       
    35 	relpipe::reader::string_t readNext(const std::vector<relpipe::reader::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 	void addRelation(Configuration& c, RelationConfiguration& currentRelation) {
       
    41 		if (currentRelation.relation.size()) {
       
    42 			c.relationConfigurations.push_back(currentRelation);
       
    43 		} else {
       
    44 			// no relation name given → global configuration
       
    45 			c.printTypes = currentRelation.printTypes;
       
    46 			c.printRelationName = currentRelation.printRelationName;
       
    47 			c.printRecordCount = currentRelation.printRecordCount;
       
    48 		}
       
    49 		currentRelation = RelationConfiguration();
       
    50 	}
       
    51 
       
    52 	/**
       
    53 	 * TODO: use a common method
       
    54 	 */
       
    55 	bool parseBoolean(const relpipe::reader::string_t& value) {
       
    56 		if (value == L"true") return true;
       
    57 		else if (value == L"false") return false;
       
    58 		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);
       
    59 	}
       
    60 
       
    61 public:
       
    62 
       
    63 	static const relpipe::reader::string_t OPTION_RELATION;
       
    64 	static const relpipe::reader::string_t OPTION_PRINT_TYPES;
       
    65 	static const relpipe::reader::string_t OPTION_PRINT_RELATION_NAME;
       
    66 	static const relpipe::reader::string_t OPTION_PRINT_RECORD_COUNT;
       
    67 
       
    68 	Configuration parse(const std::vector<relpipe::reader::string_t>& arguments) {
       
    69 		Configuration c;
       
    70 		RelationConfiguration currentRelation;
       
    71 
       
    72 		for (int i = 0; i < arguments.size();) {
       
    73 			relpipe::reader::string_t option = readNext(arguments, i);
       
    74 
       
    75 			if (option == OPTION_RELATION) {
       
    76 				addRelation(c, currentRelation); // previous relation
       
    77 				currentRelation.relation = readNext(arguments, i);
       
    78 			} else if (option == OPTION_PRINT_TYPES) {
       
    79 				currentRelation.printTypes = parseBoolean(readNext(arguments, i));
       
    80 			} else if (option == OPTION_PRINT_RELATION_NAME) {
       
    81 				currentRelation.printRelationName = parseBoolean(readNext(arguments, i));
       
    82 			} else if (option == OPTION_PRINT_RECORD_COUNT) {
       
    83 				currentRelation.printRecordCount = parseBoolean(readNext(arguments, i));
       
    84 			} else throw relpipe::cli::RelpipeCLIException(L"Unsupported CLI option: " + option, relpipe::cli::CLI::EXIT_CODE_BAD_CLI_ARGUMENTS);
       
    85 		}
       
    86 
       
    87 		addRelation(c, currentRelation); // last relation
       
    88 
       
    89 		return c;
       
    90 	}
       
    91 
       
    92 	virtual ~CLIParser() {
       
    93 	}
       
    94 };
       
    95 
       
    96 const relpipe::reader::string_t CLIParser::OPTION_RELATION = L"--relation";
       
    97 const relpipe::reader::string_t CLIParser::OPTION_PRINT_TYPES = L"--print-types";
       
    98 const relpipe::reader::string_t CLIParser::OPTION_PRINT_RELATION_NAME = L"--print-relation-name";
       
    99 const relpipe::reader::string_t CLIParser::OPTION_PRINT_RECORD_COUNT = L"--print-record-count";
       
   100 
       
   101 }
       
   102 }
       
   103 }