src/CLIParser.h
branchv_0
changeset 2 1a0fbd17db13
parent 0 7ef5ce9477c8
child 3 62ced7c41c67
equal deleted inserted replaced
1:cb9577442d3b 2:1a0fbd17db13
    35 	relpipe::common::type::StringX readNext(const std::vector<relpipe::common::type::StringX>& 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 	/**
       
    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::common::type::StringX OPTION_RELATION;
    42 	static const relpipe::common::type::StringX OPTION_RELATION;
    52 	static const relpipe::common::type::StringX OPTION_UNLINK_ON_CLOSE;
    43 	static const relpipe::common::type::StringX OPTION_STREAM;
    53 	static const relpipe::common::type::StringX OPTION_QUEUE;
       
    54 
    44 
    55 	Configuration parse(const std::vector<relpipe::common::type::StringX>& arguments) {
    45 	Configuration parse(const std::vector<relpipe::common::type::StringX>& arguments) {
    56 		Configuration c;
    46 		Configuration c;
    57 
    47 
    58 		for (int i = 0; i < arguments.size();) {
    48 		for (int i = 0; i < arguments.size();) {
    59 			relpipe::common::type::StringX option = readNext(arguments, i);
    49 			relpipe::common::type::StringX option = readNext(arguments, i);
    60 
    50 
    61 			if (option == OPTION_RELATION) {
    51 			if (option == OPTION_RELATION) {
    62 				c.relation = readNext(arguments, i);
    52 				c.relation = readNext(arguments, i);
    63 			} else if (option == OPTION_UNLINK_ON_CLOSE) {
    53 			} else if (option == OPTION_STREAM) {
    64 				c.unlinkOnClose = parseBoolean(readNext(arguments, i));
    54 				c.stream = readNext(arguments, i);
    65 			} else if (option == OPTION_QUEUE) {
       
    66 				c.queue = readNext(arguments, i);
       
    67 			} else throw relpipe::cli::RelpipeCLIException(L"Unsupported CLI option: " + option, relpipe::cli::CLI::EXIT_CODE_BAD_CLI_ARGUMENTS);
    55 			} else throw relpipe::cli::RelpipeCLIException(L"Unsupported CLI option: " + option, relpipe::cli::CLI::EXIT_CODE_BAD_CLI_ARGUMENTS);
    68 		}
    56 		}
    69 
    57 
    70 		return c;
    58 		return c;
    71 	}
    59 	}
    73 	virtual ~CLIParser() {
    61 	virtual ~CLIParser() {
    74 	}
    62 	}
    75 };
    63 };
    76 
    64 
    77 const relpipe::common::type::StringX CLIParser::OPTION_RELATION = L"--relation";
    65 const relpipe::common::type::StringX CLIParser::OPTION_RELATION = L"--relation";
    78 const relpipe::common::type::StringX CLIParser::OPTION_UNLINK_ON_CLOSE = L"--unlink-on-close";
    66 const relpipe::common::type::StringX CLIParser::OPTION_STREAM = L"--stream";
    79 const relpipe::common::type::StringX CLIParser::OPTION_QUEUE = L"--queue";
       
    80 
    67 
    81 }
    68 }
    82 }
    69 }
    83 }
    70 }