src/CLIParser.h
branchv_0
changeset 1 291bdd97fcff
parent 0 e8205d9206fb
child 2 1eef3d465863
equal deleted inserted replaced
0:e8205d9206fb 1:291bdd97fcff
    35 	relpipe::writer::string_t readNext(const std::vector<relpipe::writer::string_t>& arguments, int& i) {
    35 	relpipe::writer::string_t readNext(const std::vector<relpipe::writer::string_t>& 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 	/**
       
    41 	 * TODO: use a common method
       
    42 	 */
       
    43 	bool parseBoolean(const relpipe::writer::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 	/**
       
    50 	 * TODO: use a common method
       
    51 	 */
       
    52 	relpipe::writer::TypeId parseTypeId(const relpipe::writer::string_t& value) {
       
    53 		using t = relpipe::writer::TypeId;
       
    54 		if (value == L"string") return t::STRING;
       
    55 		else if (value == L"integer") return t::INTEGER;
       
    56 		else if (value == L"boolean") return t::BOOLEAN;
       
    57 		else throw relpipe::cli::RelpipeCLIException(L"Unable to parse TypeId: " + value, relpipe::cli::CLI::EXIT_CODE_BAD_CLI_ARGUMENTS);
       
    58 	}
       
    59 
       
    60 	Configuration::ReadTypes parseReadTypes(const relpipe::writer::string_t& value) {
       
    61 		if (value == L"auto") return Configuration::ReadTypes::AUTO;
       
    62 		else if (value == L"true") return Configuration::ReadTypes::TRUE;
       
    63 		else if (value == L"false") return Configuration::ReadTypes::FALSE;
       
    64 		else throw relpipe::cli::RelpipeCLIException(L"Unable to parse ReadTypes: " + value, relpipe::cli::CLI::EXIT_CODE_BAD_CLI_ARGUMENTS);
       
    65 	}
       
    66 
       
    67 public:
    40 public:
    68 
    41 
    69 	static const relpipe::writer::string_t OPTION_RELATION;
    42 	static const relpipe::writer::string_t OPTION_RELATION;
    70 	static const relpipe::writer::string_t OPTION_ATTRIBUTE;
    43 	static const relpipe::writer::string_t OPTION_QUEUE;
    71 	static const relpipe::writer::string_t OPTION_READ_TYPES;
    44 	static const relpipe::writer::string_t OPTION_MESSAGE_COUNT;
    72 
    45 
    73 	Configuration parse(const std::vector<relpipe::writer::string_t>& arguments) {
    46 	Configuration parse(const std::vector<relpipe::writer::string_t>& arguments) {
    74 		Configuration c;
    47 		Configuration c;
    75 
    48 
    76 		for (int i = 0; i < arguments.size();) {
    49 		for (int i = 0; i < arguments.size();) {
    77 			relpipe::writer::string_t option = readNext(arguments, i);
    50 			relpipe::writer::string_t option = readNext(arguments, i);
    78 
    51 
    79 			if (option == OPTION_RELATION) {
    52 			if (option == OPTION_RELATION) {
    80 				c.relation = readNext(arguments, i);
    53 				c.relation = readNext(arguments, i);
    81 			} else if (option == OPTION_ATTRIBUTE) {
    54 			} else if (option == OPTION_QUEUE) {
    82 				AttributeRecipe attribute;
    55 				c.queue = readNext(arguments, i);
    83 				attribute.name = readNext(arguments, i);
    56 			} else if (option == OPTION_MESSAGE_COUNT) {
    84 				attribute.type = parseTypeId(readNext(arguments, i));
    57 				c.messageCount = std::stoull(readNext(arguments, i));
    85 				c.attributes.push_back(attribute);
       
    86 			} else if (option == OPTION_READ_TYPES) {
       
    87 				c.readTypes = parseReadTypes(readNext(arguments, i));
       
    88 			} else throw relpipe::cli::RelpipeCLIException(L"Unsupported CLI option: " + option, relpipe::cli::CLI::EXIT_CODE_BAD_CLI_ARGUMENTS);
    58 			} else throw relpipe::cli::RelpipeCLIException(L"Unsupported CLI option: " + option, relpipe::cli::CLI::EXIT_CODE_BAD_CLI_ARGUMENTS);
    89 		}
    59 		}
    90 
    60 
    91 		return c;
    61 		return c;
    92 	}
    62 	}
    94 	virtual ~CLIParser() {
    64 	virtual ~CLIParser() {
    95 	}
    65 	}
    96 };
    66 };
    97 
    67 
    98 const relpipe::writer::string_t CLIParser::OPTION_RELATION = L"--relation";
    68 const relpipe::writer::string_t CLIParser::OPTION_RELATION = L"--relation";
    99 const relpipe::writer::string_t CLIParser::OPTION_ATTRIBUTE = L"--attribute";
    69 const relpipe::writer::string_t CLIParser::OPTION_QUEUE = L"--queue";
   100 const relpipe::writer::string_t CLIParser::OPTION_READ_TYPES = L"--read-types";
    70 const relpipe::writer::string_t CLIParser::OPTION_MESSAGE_COUNT = L"--message-count";
   101 
    71 
   102 }
    72 }
   103 }
    73 }
   104 }
    74 }