src/CLIParser.h
branchv_0
changeset 24 c69670b7b4ef
child 25 98be80d2e65b
equal deleted inserted replaced
23:87d22d525a3e 24:c69670b7b4ef
       
     1 /**
       
     2  * Relational pipes
       
     3  * Copyright © 2021 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 
       
    21 #include <relpipe/common/type/typedefs.h>
       
    22 #include <relpipe/cli/CLI.h>
       
    23 #include <relpipe/cli/RelpipeCLIException.h>
       
    24 
       
    25 #include "Configuration.h"
       
    26 
       
    27 namespace relpipe {
       
    28 namespace tr {
       
    29 namespace grep {
       
    30 
       
    31 class CLIParser {
       
    32 private:
       
    33 
       
    34 	relpipe::common::type::StringX readNext(std::vector<relpipe::common::type::StringX> arguments, int& i) {
       
    35 		if (i < arguments.size()) return arguments[i++];
       
    36 		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);
       
    37 	}
       
    38 
       
    39 	void addRelation(Configuration& c, RelationConfiguration& currentRelation) {
       
    40 		if (currentRelation.relation.size()) {
       
    41 			c.relationConfigurations.push_back(currentRelation);
       
    42 			currentRelation = RelationConfiguration();
       
    43 		}
       
    44 	}
       
    45 
       
    46 public:
       
    47 
       
    48 	static const relpipe::common::type::StringX OPTION_RELATION;
       
    49 	static const relpipe::common::type::StringX OPTION_ATTRIBUTE;
       
    50 	static const relpipe::common::type::StringX OPTION_PATTERN;
       
    51 
       
    52 	Configuration parse(const std::vector<relpipe::common::type::StringX>& arguments) {
       
    53 		Configuration c;
       
    54 		RelationConfiguration currentRelation;
       
    55 
       
    56 		for (int i = 0; i < arguments.size();) {
       
    57 			relpipe::common::type::StringX option = readNext(arguments, i);
       
    58 
       
    59 			if (option == OPTION_RELATION) {
       
    60 				addRelation(c, currentRelation); // previous relation
       
    61 				currentRelation.relation = readNext(arguments, i);
       
    62 				currentRelation.relationPattern = std::wregex(currentRelation.relation);
       
    63 			} else if (option == OPTION_ATTRIBUTE) {
       
    64 				currentRelation.attributePattern = std::wregex(readNext(arguments, i));
       
    65 			} else if (option == OPTION_PATTERN) {
       
    66 				currentRelation.valuePattern = std::wregex(readNext(arguments, i));
       
    67 			} else throw relpipe::cli::RelpipeCLIException(L"Unsupported CLI option: " + option, relpipe::cli::CLI::EXIT_CODE_BAD_CLI_ARGUMENTS);
       
    68 		}
       
    69 		addRelation(c, currentRelation); // last relation
       
    70 
       
    71 		return c;
       
    72 	}
       
    73 
       
    74 	virtual ~CLIParser() {
       
    75 	}
       
    76 };
       
    77 
       
    78 const relpipe::common::type::StringX CLIParser::OPTION_RELATION = L"--relation";
       
    79 const relpipe::common::type::StringX CLIParser::OPTION_ATTRIBUTE = L"--attribute";
       
    80 const relpipe::common::type::StringX CLIParser::OPTION_PATTERN = L"--pattern";
       
    81 
       
    82 }
       
    83 }
       
    84 }