src/CLIParser.h
branchv_0
changeset 0 7e986fcf0d8f
equal deleted inserted replaced
-1:000000000000 0:7e986fcf0d8f
       
     1 /**
       
     2  * Relational pipes
       
     3  * Copyright © 2022 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 deserialize {
       
    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 public:
       
    40 
       
    41 	static const relpipe::common::type::StringX OPTION_TCP_PORT;
       
    42 
       
    43 	Configuration parse(const std::vector<relpipe::common::type::StringX>& arguments) {
       
    44 		Configuration c;
       
    45 
       
    46 		for (int i = 0; i < arguments.size();) {
       
    47 			relpipe::common::type::StringX option = readNext(arguments, i);
       
    48 
       
    49 			if (option == OPTION_TCP_PORT) {
       
    50 				c.tcpPort = stoi(readNext(arguments, i));
       
    51 			} else throw relpipe::cli::RelpipeCLIException(L"Unsupported CLI option: " + option, relpipe::cli::CLI::EXIT_CODE_BAD_CLI_ARGUMENTS);
       
    52 		}
       
    53 
       
    54 		return c;
       
    55 	}
       
    56 
       
    57 	virtual ~CLIParser() {
       
    58 	}
       
    59 };
       
    60 
       
    61 const relpipe::common::type::StringX CLIParser::OPTION_TCP_PORT = L"--tcp-port";
       
    62 
       
    63 }
       
    64 }
       
    65 }