src/CLIParser.h
branchv_0
changeset 25 98be80d2e65b
parent 24 c69670b7b4ef
child 26 88e566898f8f
equal deleted inserted replaced
24:c69670b7b4ef 25:98be80d2e65b
    34 	relpipe::common::type::StringX readNext(std::vector<relpipe::common::type::StringX> arguments, int& i) {
    34 	relpipe::common::type::StringX readNext(std::vector<relpipe::common::type::StringX> arguments, int& i) {
    35 		if (i < arguments.size()) return arguments[i++];
    35 		if (i < arguments.size()) return arguments[i++];
    36 		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);
    36 		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 	}
    37 	}
    38 
    38 
       
    39 	/**
       
    40 	 * TODO: use a common method
       
    41 	 */
       
    42 	bool parseBoolean(const relpipe::common::type::StringX& value) {
       
    43 		if (value == L"true") return true;
       
    44 		else if (value == L"false") return false;
       
    45 		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);
       
    46 	}
       
    47 
       
    48 	RelationConfiguration::ENTITY parseEntity(const relpipe::common::type::StringX& value) {
       
    49 		if (value == L"relation") return RelationConfiguration::ENTITY::RELATION;
       
    50 		else if (value == L"attribute") return RelationConfiguration::ENTITY::ATTRIBUTE;
       
    51 		else if (value == L"value") return RelationConfiguration::ENTITY::VALUE;
       
    52 		else throw relpipe::cli::RelpipeCLIException(L"Unable to parse entity value: " + value + L" (expecting „relation“, „attribute“ or „value“)", relpipe::cli::CLI::EXIT_CODE_BAD_CLI_ARGUMENTS);
       
    53 	}
       
    54 
    39 	void addRelation(Configuration& c, RelationConfiguration& currentRelation) {
    55 	void addRelation(Configuration& c, RelationConfiguration& currentRelation) {
    40 		if (currentRelation.relation.size()) {
    56 		if (currentRelation.relation.size()) {
       
    57 			using E = RelationConfiguration::ENTITY;
       
    58 			currentRelation.relationPattern = currentRelation.caseSensitive[E::RELATION] ? std::wregex(currentRelation.relation) : std::wregex(currentRelation.relation, std::regex_constants::icase);
       
    59 			currentRelation.attributePattern = currentRelation.caseSensitive[E::ATTRIBUTE] ? std::wregex(currentRelation.attribute) : std::wregex(currentRelation.attribute, std::regex_constants::icase);
       
    60 			currentRelation.valuePattern = currentRelation.caseSensitive[E::VALUE] ? std::wregex(currentRelation.value) : std::wregex(currentRelation.value, std::regex_constants::icase);
    41 			c.relationConfigurations.push_back(currentRelation);
    61 			c.relationConfigurations.push_back(currentRelation);
    42 			currentRelation = RelationConfiguration();
    62 			currentRelation = RelationConfiguration();
    43 		}
    63 		}
    44 	}
    64 	}
    45 
    65 
    46 public:
    66 public:
    47 
    67 
    48 	static const relpipe::common::type::StringX OPTION_RELATION;
    68 	static const relpipe::common::type::StringX OPTION_RELATION;
    49 	static const relpipe::common::type::StringX OPTION_ATTRIBUTE;
    69 	static const relpipe::common::type::StringX OPTION_ATTRIBUTE;
    50 	static const relpipe::common::type::StringX OPTION_PATTERN;
    70 	static const relpipe::common::type::StringX OPTION_PATTERN;
       
    71 	static const relpipe::common::type::StringX OPTION_CASE_SENSITIVE;
       
    72 	static const relpipe::common::type::StringX OPTION_INVERT_MATCH;
    51 
    73 
    52 	Configuration parse(const std::vector<relpipe::common::type::StringX>& arguments) {
    74 	Configuration parse(const std::vector<relpipe::common::type::StringX>& arguments) {
    53 		Configuration c;
    75 		Configuration c;
    54 		RelationConfiguration currentRelation;
    76 		RelationConfiguration currentRelation;
    55 
    77 
    57 			relpipe::common::type::StringX option = readNext(arguments, i);
    79 			relpipe::common::type::StringX option = readNext(arguments, i);
    58 
    80 
    59 			if (option == OPTION_RELATION) {
    81 			if (option == OPTION_RELATION) {
    60 				addRelation(c, currentRelation); // previous relation
    82 				addRelation(c, currentRelation); // previous relation
    61 				currentRelation.relation = readNext(arguments, i);
    83 				currentRelation.relation = readNext(arguments, i);
    62 				currentRelation.relationPattern = std::wregex(currentRelation.relation);
       
    63 			} else if (option == OPTION_ATTRIBUTE) {
    84 			} else if (option == OPTION_ATTRIBUTE) {
    64 				currentRelation.attributePattern = std::wregex(readNext(arguments, i));
    85 				currentRelation.attribute = readNext(arguments, i);
    65 			} else if (option == OPTION_PATTERN) {
    86 			} else if (option == OPTION_PATTERN) {
    66 				currentRelation.valuePattern = std::wregex(readNext(arguments, i));
    87 				currentRelation.value = readNext(arguments, i);
       
    88 			} else if (option == OPTION_CASE_SENSITIVE) {
       
    89 				RelationConfiguration::ENTITY entity = parseEntity(readNext(arguments, i));
       
    90 				currentRelation.caseSensitive[entity] = parseBoolean(readNext(arguments, i));
       
    91 			} else if (option == OPTION_INVERT_MATCH) {
       
    92 				RelationConfiguration::ENTITY entity = parseEntity(readNext(arguments, i));
       
    93 				currentRelation.invertMatch[entity] = parseBoolean(readNext(arguments, i));
    67 			} else throw relpipe::cli::RelpipeCLIException(L"Unsupported CLI option: " + option, relpipe::cli::CLI::EXIT_CODE_BAD_CLI_ARGUMENTS);
    94 			} else throw relpipe::cli::RelpipeCLIException(L"Unsupported CLI option: " + option, relpipe::cli::CLI::EXIT_CODE_BAD_CLI_ARGUMENTS);
    68 		}
    95 		}
    69 		addRelation(c, currentRelation); // last relation
    96 		addRelation(c, currentRelation); // last relation
    70 
    97 
    71 		return c;
    98 		return c;
    76 };
   103 };
    77 
   104 
    78 const relpipe::common::type::StringX CLIParser::OPTION_RELATION = L"--relation";
   105 const relpipe::common::type::StringX CLIParser::OPTION_RELATION = L"--relation";
    79 const relpipe::common::type::StringX CLIParser::OPTION_ATTRIBUTE = L"--attribute";
   106 const relpipe::common::type::StringX CLIParser::OPTION_ATTRIBUTE = L"--attribute";
    80 const relpipe::common::type::StringX CLIParser::OPTION_PATTERN = L"--pattern";
   107 const relpipe::common::type::StringX CLIParser::OPTION_PATTERN = L"--pattern";
       
   108 const relpipe::common::type::StringX CLIParser::OPTION_CASE_SENSITIVE = L"--case-sensitive";
       
   109 const relpipe::common::type::StringX CLIParser::OPTION_INVERT_MATCH = L"--invert-match";
    81 
   110 
    82 }
   111 }
    83 }
   112 }
    84 }
   113 }