src/CLIParser.h
branchv_0
changeset 3 610783d70ae9
parent 2 0799eaf338b9
equal deleted inserted replaced
2:0799eaf338b9 3:610783d70ae9
    17 #pragma once
    17 #pragma once
    18 
    18 
    19 #include <vector>
    19 #include <vector>
    20 #include <iostream>
    20 #include <iostream>
    21 
    21 
    22 #include <relpipe/writer/typedefs.h>
    22 #include <relpipe/common/type/typedefs.h>
    23 #include <relpipe/cli/CLI.h>
    23 #include <relpipe/cli/CLI.h>
    24 #include <relpipe/cli/RelpipeCLIException.h>
    24 #include <relpipe/cli/RelpipeCLIException.h>
    25 
    25 
    26 #include "Configuration.h"
    26 #include "Configuration.h"
    27 
    27 
    30 namespace mqtt {
    30 namespace mqtt {
    31 
    31 
    32 class CLIParser {
    32 class CLIParser {
    33 private:
    33 private:
    34 
    34 
    35 	relpipe::writer::string_t readNext(const std::vector<relpipe::writer::string_t>& arguments, int& i) {
    35 	relpipe::common::type::StringX readNext(const std::vector<relpipe::common::type::StringX>& arguments, int& i) {
    36 		if (i < arguments.size()) return arguments[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);
    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 	}
    38 	}
    39 
    39 
    40 public:
    40 public:
    41 
    41 
    42 	static const relpipe::writer::string_t OPTION_RELATION;
    42 	static const relpipe::common::type::StringX OPTION_RELATION;
    43 	static const relpipe::writer::string_t OPTION_STREAM;
    43 	static const relpipe::common::type::StringX OPTION_STREAM;
    44 	static const relpipe::writer::string_t OPTION_MESSAGE_COUNT;
    44 	static const relpipe::common::type::StringX OPTION_CONNECTION_STRING;
       
    45 	static const relpipe::common::type::StringX OPTION_CONNECTION_OPTION;
       
    46 	static const relpipe::common::type::StringX OPTION_MESSAGE_COUNT;
    45 
    47 
    46 	Configuration parse(const std::vector<relpipe::writer::string_t>& arguments) {
    48 	Configuration parse(const std::vector<relpipe::common::type::StringX>& arguments) {
    47 		Configuration c;
    49 		Configuration c;
    48 
    50 
    49 		for (int i = 0; i < arguments.size();) {
    51 		for (int i = 0; i < arguments.size();) {
    50 			relpipe::writer::string_t option = readNext(arguments, i);
    52 			relpipe::common::type::StringX option = readNext(arguments, i);
    51 
    53 
    52 			if (option == OPTION_RELATION) {
    54 			if (option == OPTION_RELATION) {
    53 				c.relation = readNext(arguments, i);
    55 				c.relation = readNext(arguments, i);
    54 			} else if (option == OPTION_STREAM) {
    56 			} else if (option == OPTION_STREAM) {
    55 				c.stream = readNext(arguments, i);
    57 				c.stream = readNext(arguments, i);
       
    58 			} else if (option == OPTION_CONNECTION_STRING) {
       
    59 				c.connectionString = readNext(arguments, i);
       
    60 			} else if (option == OPTION_CONNECTION_OPTION) {
       
    61 				auto name = readNext(arguments, i);
       
    62 				auto value = readNext(arguments, i);
       
    63 				c.connectionOptions.push_back({name, value});
    56 			} else if (option == OPTION_MESSAGE_COUNT) {
    64 			} else if (option == OPTION_MESSAGE_COUNT) {
    57 				c.messageCount = std::stoull(readNext(arguments, i));
    65 				c.messageCount = std::stoull(readNext(arguments, i));
    58 			} else throw relpipe::cli::RelpipeCLIException(L"Unsupported CLI option: " + option, relpipe::cli::CLI::EXIT_CODE_BAD_CLI_ARGUMENTS);
    66 			} else throw relpipe::cli::RelpipeCLIException(L"Unsupported CLI option: " + option, relpipe::cli::CLI::EXIT_CODE_BAD_CLI_ARGUMENTS);
    59 		}
    67 		}
    60 
    68 
    63 
    71 
    64 	virtual ~CLIParser() {
    72 	virtual ~CLIParser() {
    65 	}
    73 	}
    66 };
    74 };
    67 
    75 
    68 const relpipe::writer::string_t CLIParser::OPTION_RELATION = L"--relation";
    76 const relpipe::common::type::StringX CLIParser::OPTION_RELATION = L"--relation";
    69 const relpipe::writer::string_t CLIParser::OPTION_STREAM = L"--stream";
    77 const relpipe::common::type::StringX CLIParser::OPTION_STREAM = L"--stream";
    70 const relpipe::writer::string_t CLIParser::OPTION_MESSAGE_COUNT = L"--message-count";
    78 const relpipe::common::type::StringX CLIParser::OPTION_CONNECTION_STRING = L"--connection-string";
       
    79 const relpipe::common::type::StringX CLIParser::OPTION_CONNECTION_OPTION = L"--connection-option";
       
    80 const relpipe::common::type::StringX CLIParser::OPTION_MESSAGE_COUNT = L"--message-count";
    71 
    81 
    72 }
    82 }
    73 }
    83 }
    74 }
    84 }