src/CLIParser.h
branchv_0
changeset 54 bc6e11cccdf4
parent 47 428c278af4be
child 59 a1775ba6d056
equal deleted inserted replaced
53:cc6ffeba0fe5 54:bc6e11cccdf4
    34 	// FIXME: move common methods/classes to relpipe-lib-cli or relpipe-lib-helper
    34 	// FIXME: move common methods/classes to relpipe-lib-cli or relpipe-lib-helper
    35 
    35 
    36 	string_t readNext(std::vector<string_t> arguments, int& i) {
    36 	string_t readNext(std::vector<string_t> arguments, int& i) {
    37 		if (i < arguments.size()) return arguments[i++];
    37 		if (i < arguments.size()) return arguments[i++];
    38 		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 		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);
       
    39 	}
       
    40 
       
    41 	/**
       
    42 	 * TODO: use a common method
       
    43 	 */
       
    44 	bool parseBoolean(const string_t& value) {
       
    45 		if (value == L"true") return true;
       
    46 		else if (value == L"false") return false;
       
    47 		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);
    39 	}
    48 	}
    40 
    49 
    41 	void addQuery(Configuration& c, Statement& currentQuery) {
    50 	void addQuery(Configuration& c, Statement& currentQuery) {
    42 		if (currentQuery.sql.size()) {
    51 		if (currentQuery.sql.size()) {
    43 			c.statements.push_back(currentQuery);
    52 			c.statements.push_back(currentQuery);
    83 			} else if (option == OPTION_DATA_SOURCE_NAME) {
    92 			} else if (option == OPTION_DATA_SOURCE_NAME) {
    84 				c.dataSourceName = readNext(arguments, i);
    93 				c.dataSourceName = readNext(arguments, i);
    85 			} else if (option == OPTION_DATA_SOURCE_STRING) {
    94 			} else if (option == OPTION_DATA_SOURCE_STRING) {
    86 				c.dataSourceString = readNext(arguments, i);
    95 				c.dataSourceString = readNext(arguments, i);
    87 			} else if (option == OPTION_LIST_DATA_SOURCES) {
    96 			} else if (option == OPTION_LIST_DATA_SOURCES) {
    88 				c.listDataSources = true;
    97 				c.listDataSources = parseBoolean(readNext(arguments, i));
    89 			} else throw relpipe::cli::RelpipeCLIException(L"Unsupported CLI option: " + option, relpipe::cli::CLI::EXIT_CODE_BAD_CLI_ARGUMENTS);
    98 			} else throw relpipe::cli::RelpipeCLIException(L"Unsupported CLI option: " + option, relpipe::cli::CLI::EXIT_CODE_BAD_CLI_ARGUMENTS);
    90 		}
    99 		}
    91 		addQuery(c, currentQuery); // last relation
   100 		addQuery(c, currentQuery); // last relation
    92 
   101 
    93 		if (c.dataSourceName.size() && c.dataSourceString.size()) throw relpipe::cli::RelpipeCLIException(L"Specify data source name or data source string, not both.", relpipe::cli::CLI::EXIT_CODE_BAD_CLI_ARGUMENTS);
   102 		if (c.dataSourceName.size() && c.dataSourceString.size()) throw relpipe::cli::RelpipeCLIException(L"Specify data source name or data source string, not both.", relpipe::cli::CLI::EXIT_CODE_BAD_CLI_ARGUMENTS);