src/CLIParser.h
branchv_0
changeset 0 5d1c556ff7db
child 3 9f2d6645ebe6
equal deleted inserted replaced
-1:000000000000 0:5d1c556ff7db
       
     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 infertypes {
       
    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 	MODE parseMode(const relpipe::common::type::StringX& value) {
       
    40 		if (value == L"auto") return MODE::AUTO;
       
    41 		else if (value == L"metadata") return MODE::METADATA;
       
    42 		else if (value == L"data") return MODE::DATA;
       
    43 		else throw relpipe::cli::RelpipeCLIException(L"Unable to parse mode value: " + value, relpipe::cli::CLI::EXIT_CODE_BAD_CLI_ARGUMENTS);
       
    44 	}
       
    45 
       
    46 	void addRelation(Configuration& c, RelationConfiguration& currentRelation) {
       
    47 		if (currentRelation.relation.size()) {
       
    48 			currentRelation.relationPattern = std::wregex(currentRelation.relation);
       
    49 			c.relationConfigurations.push_back(currentRelation);
       
    50 			currentRelation = RelationConfiguration();
       
    51 		}
       
    52 	}
       
    53 
       
    54 public:
       
    55 
       
    56 	static const relpipe::common::type::StringX OPTION_RELATION;
       
    57 	static const relpipe::common::type::StringX OPTION_MODE;
       
    58 
       
    59 	Configuration parse(const std::vector<relpipe::common::type::StringX>& arguments) {
       
    60 		Configuration c;
       
    61 		RelationConfiguration currentRelation;
       
    62 
       
    63 		for (int i = 0; i < arguments.size();) {
       
    64 			relpipe::common::type::StringX option = readNext(arguments, i);
       
    65 
       
    66 			if (option == OPTION_RELATION) {
       
    67 				addRelation(c, currentRelation); // previous relation
       
    68 				currentRelation.relation = readNext(arguments, i);
       
    69 			} else if (option == OPTION_MODE) {
       
    70 				currentRelation.mode = parseMode(readNext(arguments, i));
       
    71 			} else throw relpipe::cli::RelpipeCLIException(L"Unsupported CLI option: " + option, relpipe::cli::CLI::EXIT_CODE_BAD_CLI_ARGUMENTS);
       
    72 		}
       
    73 		addRelation(c, currentRelation); // last relation
       
    74 
       
    75 		return c;
       
    76 	}
       
    77 
       
    78 	virtual ~CLIParser() {
       
    79 	}
       
    80 };
       
    81 
       
    82 const relpipe::common::type::StringX CLIParser::OPTION_RELATION = L"--relation";
       
    83 const relpipe::common::type::StringX CLIParser::OPTION_MODE = L"--mode";
       
    84 
       
    85 }
       
    86 }
       
    87 }