src/CLIParser.h
branchv_0
changeset 0 28294b895e5e
equal deleted inserted replaced
-1:000000000000 0:28294b895e5e
       
     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/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 in {
       
    30 namespace asn1 {
       
    31 
       
    32 class CLIParser {
       
    33 private:
       
    34 
       
    35 	relpipe::writer::string_t readNext(const std::vector<relpipe::writer::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 	Mode parseMode(const relpipe::writer::string_t& value) {
       
    41 		if (value == L"relpipe") return Mode::Relpipe;
       
    42 		else if (value == L"freeform") return Mode::Freeform;
       
    43 		else throw relpipe::cli::RelpipeCLIException(L"Unable to parse Mode value: " + value + L" (expecting relpipe or freeform)", relpipe::cli::CLI::EXIT_CODE_BAD_CLI_ARGUMENTS);
       
    44 	}
       
    45 
       
    46 public:
       
    47 
       
    48 	static const relpipe::writer::string_t OPTION_RELATION;
       
    49 	static const relpipe::writer::string_t OPTION_PARSER_OPTION;
       
    50 	static const relpipe::writer::string_t OPTION_MODE;
       
    51 
       
    52 	Configuration parse(const std::vector<relpipe::writer::string_t>& arguments) {
       
    53 		Configuration c;
       
    54 
       
    55 		for (int i = 0; i < arguments.size();) {
       
    56 			relpipe::writer::string_t option = readNext(arguments, i);
       
    57 
       
    58 			if (option == OPTION_RELATION) c.relation = readNext(arguments, i);
       
    59 			else if (option == OPTION_PARSER_OPTION) c.parserOptions.push_back({readNext(arguments, i), readNext(arguments, i)});
       
    60 			else if (option == OPTION_MODE) c.mode = parseMode(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::writer::string_t CLIParser::OPTION_RELATION = L"--relation";
       
    72 const relpipe::writer::string_t CLIParser::OPTION_PARSER_OPTION = L"--parser-option";
       
    73 const relpipe::writer::string_t CLIParser::OPTION_MODE = L"--mode";
       
    74 
       
    75 }
       
    76 }
       
    77 }