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