src/CLIParser.h
branchv_0
changeset 12 7977c1bdba1f
parent 6 4062b8436838
child 13 c9fece435aa2
equal deleted inserted replaced
11:9603e64324bc 12:7977c1bdba1f
    30 namespace guile {
    30 namespace guile {
    31 
    31 
    32 class CLIParser {
    32 class CLIParser {
    33 private:
    33 private:
    34 
    34 
    35 	// TODO: move common methods/classes to relpipe-lib-cli
    35 	// FIXME: move common methods/classes to relpipe-lib-cli or relpipe-lib-helper
    36 
    36 
    37 	string_t readNext(std::vector<string_t> arguments, int& i) {
    37 	string_t readNext(std::vector<string_t> arguments, int& i) {
    38 		if (i < arguments.size()) return arguments[i++];
    38 		if (i < arguments.size()) return arguments[i++];
    39 		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 		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);
    40 	}
    40 	}
    50 		if (value == L"true") return true;
    50 		if (value == L"true") return true;
    51 		else if (value == L"false") return false;
    51 		else if (value == L"false") return false;
    52 		else throw relpipe::cli::RelpipeCLIException(L"Unable to parse boolean value of option: " + optionName + L" (expecting true or false)", relpipe::cli::CLI::EXIT_CODE_BAD_CLI_ARGUMENTS);
    52 		else throw relpipe::cli::RelpipeCLIException(L"Unable to parse boolean value of option: " + optionName + L" (expecting true or false)", relpipe::cli::CLI::EXIT_CODE_BAD_CLI_ARGUMENTS);
    53 	}
    53 	}
    54 
    54 
       
    55 	relpipe::writer::TypeId parseTypeId(const string_t& value) {
       
    56 		using t = relpipe::writer::TypeId;
       
    57 		if (value == L"string") return t::STRING;
       
    58 		else if (value == L"integer") return t::INTEGER;
       
    59 		else if (value == L"boolean") return t::BOOLEAN;
       
    60 		else throw relpipe::cli::RelpipeCLIException(L"Unable to parse TypeId: " + value, relpipe::cli::CLI::EXIT_CODE_BAD_CLI_ARGUMENTS);
       
    61 	}
       
    62 
    55 public:
    63 public:
    56 
    64 
    57 	static const string_t OPTION_RELATION;
    65 	static const string_t OPTION_RELATION;
       
    66 	static const string_t OPTION_OUTPUT_ATTRIBUTE;
    58 	static const string_t OPTION_BEFORE_RECORDS;
    67 	static const string_t OPTION_BEFORE_RECORDS;
    59 	static const string_t OPTION_AFTER_RECORDS;
    68 	static const string_t OPTION_AFTER_RECORDS;
    60 	static const string_t OPTION_FOR_EACH;
    69 	static const string_t OPTION_FOR_EACH;
    61 	static const string_t OPTION_WHERE;
    70 	static const string_t OPTION_WHERE;
    62 	static const string_t OPTION_DROP;
    71 	static const string_t OPTION_DROP;
    76 				else if (option == OPTION_WHERE) currentRelation.guileWhere = readNext(arguments, i);
    85 				else if (option == OPTION_WHERE) currentRelation.guileWhere = readNext(arguments, i);
    77 				else if (option == OPTION_DROP) currentRelation.drop = parseBoolean(readNext(arguments, i), option);
    86 				else if (option == OPTION_DROP) currentRelation.drop = parseBoolean(readNext(arguments, i), option);
    78 				else if (option == OPTION_RELATION) {
    87 				else if (option == OPTION_RELATION) {
    79 					addRelation(c, currentRelation); // previous relation
    88 					addRelation(c, currentRelation); // previous relation
    80 					currentRelation.relation = readNext(arguments, i);
    89 					currentRelation.relation = readNext(arguments, i);
       
    90 				} else if (option == OPTION_OUTPUT_ATTRIBUTE) {
       
    91 					relpipe::writer::AttributeMetadata attribute;
       
    92 					attribute.attributeName = readNext(arguments, i);
       
    93 					attribute.typeId = parseTypeId(readNext(arguments, i));
       
    94 					currentRelation.writerMetadata.push_back(attribute);
    81 				} else if (option == OPTION_DEFINE) {
    95 				} else if (option == OPTION_DEFINE) {
    82 					DefinitionRecipe definition;
    96 					DefinitionRecipe definition;
    83 					definition.name = readNext(arguments, i);
    97 					definition.name = readNext(arguments, i);
    84 					definition.type = readNext(arguments, i);
    98 					definition.type = readNext(arguments, i);
    85 					definition.value = readNext(arguments, i);
    99 					definition.value = readNext(arguments, i);
    97 	virtual ~CLIParser() {
   111 	virtual ~CLIParser() {
    98 	}
   112 	}
    99 };
   113 };
   100 
   114 
   101 const string_t CLIParser::OPTION_RELATION = L"--relation";
   115 const string_t CLIParser::OPTION_RELATION = L"--relation";
       
   116 const string_t CLIParser::OPTION_OUTPUT_ATTRIBUTE = L"--output-attribute";
   102 const string_t CLIParser::OPTION_BEFORE_RECORDS = L"--before-records";
   117 const string_t CLIParser::OPTION_BEFORE_RECORDS = L"--before-records";
   103 const string_t CLIParser::OPTION_AFTER_RECORDS = L"--after-records";
   118 const string_t CLIParser::OPTION_AFTER_RECORDS = L"--after-records";
   104 const string_t CLIParser::OPTION_FOR_EACH = L"--for-each";
   119 const string_t CLIParser::OPTION_FOR_EACH = L"--for-each";
   105 const string_t CLIParser::OPTION_WHERE = L"--where";
   120 const string_t CLIParser::OPTION_WHERE = L"--where";
   106 const string_t CLIParser::OPTION_DROP = L"--drop";
   121 const string_t CLIParser::OPTION_DROP = L"--drop";