src/CLIParser.h
branchv_0
changeset 0 17fc678e0a94
child 5 dbf093b8b9ac
equal deleted inserted replaced
-1:000000000000 0:17fc678e0a94
       
     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 #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 x11 {
       
    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 	/**
       
    41 	 * TODO: use a common method
       
    42 	 */
       
    43 	bool parseBoolean(const relpipe::reader::string_t& value) {
       
    44 		if (value == L"true") return true;
       
    45 		else if (value == L"false") return false;
       
    46 		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);
       
    47 	}
       
    48 
       
    49 public:
       
    50 
       
    51 	static const relpipe::reader::string_t OPTION_WRITE_HEADER;
       
    52 
       
    53 	Configuration parse(const std::vector<relpipe::reader::string_t>& arguments) {
       
    54 		Configuration c;
       
    55 
       
    56 		for (int i = 0; i < arguments.size();) {
       
    57 			relpipe::reader::string_t option = readNext(arguments, i);
       
    58 
       
    59 			if (option == OPTION_WRITE_HEADER) {
       
    60 				c.writeHeader = parseBoolean(readNext(arguments, i));
       
    61 			} else throw relpipe::cli::RelpipeCLIException(L"Unsupported CLI option: " + option, relpipe::cli::CLI::EXIT_CODE_BAD_CLI_ARGUMENTS);
       
    62 		}
       
    63 
       
    64 		return c;
       
    65 	}
       
    66 
       
    67 	virtual ~CLIParser() {
       
    68 	}
       
    69 };
       
    70 
       
    71 const relpipe::reader::string_t CLIParser::OPTION_WRITE_HEADER = L"--write-header";
       
    72 
       
    73 }
       
    74 }
       
    75 }