src/CLIParser.h
branchv_0
changeset 2 0799eaf338b9
parent 0 7fdf75648c0a
child 3 610783d70ae9
equal deleted inserted replaced
1:4993a084b8ba 2:0799eaf338b9
    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::common::type::StringX& 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:
    40 public:
    50 
    41 
    51 	static const relpipe::writer::string_t OPTION_RELATION;
    42 	static const relpipe::writer::string_t OPTION_RELATION;
    52 	static const relpipe::writer::string_t OPTION_UNLINK_ON_CLOSE;
    43 	static const relpipe::writer::string_t OPTION_STREAM;
    53 	static const relpipe::writer::string_t OPTION_QUEUE;
       
    54 	static const relpipe::writer::string_t OPTION_MESSAGE_COUNT;
    44 	static const relpipe::writer::string_t OPTION_MESSAGE_COUNT;
    55 
    45 
    56 	Configuration parse(const std::vector<relpipe::writer::string_t>& arguments) {
    46 	Configuration parse(const std::vector<relpipe::writer::string_t>& arguments) {
    57 		Configuration c;
    47 		Configuration c;
    58 
    48 
    59 		for (int i = 0; i < arguments.size();) {
    49 		for (int i = 0; i < arguments.size();) {
    60 			relpipe::writer::string_t option = readNext(arguments, i);
    50 			relpipe::writer::string_t option = readNext(arguments, i);
    61 
    51 
    62 			if (option == OPTION_RELATION) {
    52 			if (option == OPTION_RELATION) {
    63 				c.relation = readNext(arguments, i);
    53 				c.relation = readNext(arguments, i);
    64 			} else if (option == OPTION_UNLINK_ON_CLOSE) {
    54 			} else if (option == OPTION_STREAM) {
    65 				c.unlinkOnClose = parseBoolean(readNext(arguments, i));
    55 				c.stream = readNext(arguments, i);
    66 			} else if (option == OPTION_QUEUE) {
       
    67 				c.queue = readNext(arguments, i);
       
    68 			} else if (option == OPTION_MESSAGE_COUNT) {
    56 			} else if (option == OPTION_MESSAGE_COUNT) {
    69 				c.messageCount = std::stoull(readNext(arguments, i));
    57 				c.messageCount = std::stoull(readNext(arguments, i));
    70 			} 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);
    71 		}
    59 		}
    72 
    60 
    76 	virtual ~CLIParser() {
    64 	virtual ~CLIParser() {
    77 	}
    65 	}
    78 };
    66 };
    79 
    67 
    80 const relpipe::writer::string_t CLIParser::OPTION_RELATION = L"--relation";
    68 const relpipe::writer::string_t CLIParser::OPTION_RELATION = L"--relation";
    81 const relpipe::writer::string_t CLIParser::OPTION_UNLINK_ON_CLOSE = L"--unlink-on-close";
    69 const relpipe::writer::string_t CLIParser::OPTION_STREAM = L"--stream";
    82 const relpipe::writer::string_t CLIParser::OPTION_QUEUE = L"--queue";
       
    83 const relpipe::writer::string_t CLIParser::OPTION_MESSAGE_COUNT = L"--message-count";
    70 const relpipe::writer::string_t CLIParser::OPTION_MESSAGE_COUNT = L"--message-count";
    84 
    71 
    85 }
    72 }
    86 }
    73 }
    87 }
    74 }