src/CLIParser.h
branchv_0
changeset 0 644fd2ce2580
child 8 513ea30d2fa3
equal deleted inserted replaced
-1:000000000000 0:644fd2ce2580
       
     1 /**
       
     2  * Relational pipes
       
     3  * Copyright © 2019 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, either version 3 of the License, or
       
     8  * (at your option) any later version.
       
     9  *
       
    10  * This program is distributed in the hope that it will be useful,
       
    11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
       
    13  * GNU General Public License for more details.
       
    14  *
       
    15  * You should have received a copy of the GNU General Public License
       
    16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
       
    17  */
       
    18 #pragma once
       
    19 
       
    20 #include <vector>
       
    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 tr {
       
    30 namespace awk {
       
    31 
       
    32 class CLIParser {
       
    33 private:
       
    34 
       
    35 	// FIXME: move common methods/classes to relpipe-lib-cli or relpipe-lib-helper
       
    36 
       
    37 	string_t readNext(std::vector<string_t> arguments, int& i) {
       
    38 		if (i < arguments.size()) return arguments[i++];
       
    39 		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);
       
    40 	}
       
    41 
       
    42 public:
       
    43 
       
    44 	static const string_t OPTION_EXPRESSION;
       
    45 
       
    46 	Configuration parse(const std::vector<string_t>& arguments) {
       
    47 		Configuration c;
       
    48 
       
    49 		for (int i = 0; i < arguments.size();) {
       
    50 			string_t option = readNext(arguments, i);
       
    51 			if (option == OPTION_EXPRESSION) c.awkExpression = readNext(arguments, i);
       
    52 			else throw relpipe::cli::RelpipeCLIException(L"Unsupported CLI option: " + option, relpipe::cli::CLI::EXIT_CODE_BAD_CLI_ARGUMENTS);
       
    53 		}
       
    54 
       
    55 		return c;
       
    56 	}
       
    57 
       
    58 	virtual ~CLIParser() {
       
    59 	}
       
    60 };
       
    61 
       
    62 const string_t CLIParser::OPTION_EXPRESSION = L"--expression";
       
    63 
       
    64 }
       
    65 }
       
    66 }