src/CLIParser.h
branchv_0
changeset 26 576d4965434f
parent 25 0cfbaf5c57a6
equal deleted inserted replaced
25:0cfbaf5c57a6 26:576d4965434f
    50 		else if (value == L"attribute") return ENTITY::ATTRIBUTE;
    50 		else if (value == L"attribute") return ENTITY::ATTRIBUTE;
    51 		else if (value == L"value") return ENTITY::VALUE;
    51 		else if (value == L"value") return 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);
    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 	}
    53 	}
    54 
    54 
       
    55 	ENTITY parseModifyTarget(const relpipe::common::type::StringX& value) {
       
    56 		if (value == L"relation-name") return ENTITY::RELATION;
       
    57 		else if (value == L"attribute-name") return ENTITY::ATTRIBUTE;
       
    58 		else if (value == L"value") return ENTITY::VALUE;
       
    59 		else throw relpipe::cli::RelpipeCLIException(L"Unable to parse entity value: " + value + L" (expecting „relation-name“, „attribute-name“ or „value“)", relpipe::cli::CLI::EXIT_CODE_BAD_CLI_ARGUMENTS);
       
    60 	}
       
    61 
    55 	void addRule(RelationConfiguration& currentRelation, RewriteRule& currentRule) {
    62 	void addRule(RelationConfiguration& currentRelation, RewriteRule& currentRule) {
    56 		if (currentRule.attribute.size()) {
    63 		if (currentRule.modify != ENTITY::DEFAULT) {
       
    64 			// the --value is optional → set defaults if missing:
       
    65 			if (currentRule.modify == ENTITY::RELATION && currentRule.value.size() == 0) currentRule.value = currentRelation.relation;
       
    66 			if (currentRule.modify == ENTITY::ATTRIBUTE && currentRule.value.size() == 0) currentRule.value = currentRule.attribute;
       
    67 
    57 			currentRule.attributePattern = currentRule.caseSensitive[ENTITY::ATTRIBUTE] ? std::wregex(currentRule.attribute) : std::wregex(currentRule.attribute, std::regex_constants::icase);
    68 			currentRule.attributePattern = currentRule.caseSensitive[ENTITY::ATTRIBUTE] ? std::wregex(currentRule.attribute) : std::wregex(currentRule.attribute, std::regex_constants::icase);
    58 			currentRule.valuePattern = currentRule.caseSensitive[ENTITY::VALUE] ? std::wregex(currentRule.value) : std::wregex(currentRule.value, std::regex_constants::icase);
    69 			currentRule.valuePattern = currentRule.caseSensitive[ENTITY::VALUE] ? std::wregex(currentRule.value) : std::wregex(currentRule.value, std::regex_constants::icase);
    59 			currentRelation.rules.push_back(currentRule);
    70 			currentRelation.rules.push_back(currentRule);
    60 			currentRule = RewriteRule(currentRelation.caseSensitive, currentRelation.invertMatch);
    71 			currentRule = RewriteRule(currentRelation.caseSensitive, currentRelation.invertMatch);
    61 		}
    72 		}
    71 	}
    82 	}
    72 
    83 
    73 public:
    84 public:
    74 
    85 
    75 	static const relpipe::common::type::StringX OPTION_RELATION;
    86 	static const relpipe::common::type::StringX OPTION_RELATION;
       
    87 	static const relpipe::common::type::StringX OPTION_MODIFY;
    76 	static const relpipe::common::type::StringX OPTION_ATTRIBUTE;
    88 	static const relpipe::common::type::StringX OPTION_ATTRIBUTE;
    77 	static const relpipe::common::type::StringX OPTION_VALUE;
    89 	static const relpipe::common::type::StringX OPTION_VALUE;
    78 	static const relpipe::common::type::StringX OPTION_REPLACEMENT;
    90 	static const relpipe::common::type::StringX OPTION_REPLACEMENT;
    79 	static const relpipe::common::type::StringX OPTION_CASE_SENSITIVE;
    91 	static const relpipe::common::type::StringX OPTION_CASE_SENSITIVE;
    80 	static const relpipe::common::type::StringX OPTION_INVERT_MATCH;
    92 	static const relpipe::common::type::StringX OPTION_INVERT_MATCH;
    88 			relpipe::common::type::StringX option = readNext(arguments, i);
   100 			relpipe::common::type::StringX option = readNext(arguments, i);
    89 
   101 
    90 			if (option == OPTION_RELATION) {
   102 			if (option == OPTION_RELATION) {
    91 				addRelation(c, currentRelation, currentRule); // previous relation
   103 				addRelation(c, currentRelation, currentRule); // previous relation
    92 				currentRelation.relation = readNext(arguments, i);
   104 				currentRelation.relation = readNext(arguments, i);
       
   105 			} else if (option == OPTION_MODIFY) {
       
   106 				addRule(currentRelation, currentRule); // previous rule
       
   107 				currentRule.modify = parseModifyTarget(readNext(arguments, i));
       
   108 				if (currentRule.modify == ENTITY::VALUE) currentRule.modify = ENTITY::DEFAULT;
    93 			} else if (option == OPTION_ATTRIBUTE) {
   109 			} else if (option == OPTION_ATTRIBUTE) {
    94 				addRule(currentRelation, currentRule); // previous rule
   110 				if (currentRule.modify == ENTITY::DEFAULT) {
       
   111 					currentRule.modify = ENTITY::VALUE;
       
   112 				} else if (currentRule.modify == ENTITY::VALUE) {
       
   113 					addRule(currentRelation, currentRule); // previous rule
       
   114 					currentRule.modify = ENTITY::VALUE;
       
   115 				}
    95 				currentRule.attribute = readNext(arguments, i);
   116 				currentRule.attribute = readNext(arguments, i);
    96 			} else if (option == OPTION_VALUE) {
   117 			} else if (option == OPTION_VALUE) {
    97 				currentRule.value = readNext(arguments, i);
   118 				currentRule.value = readNext(arguments, i);
    98 			} else if (option == OPTION_REPLACEMENT) {
   119 			} else if (option == OPTION_REPLACEMENT) {
    99 				currentRule.replacement = readNext(arguments, i);
   120 				currentRule.replacement = readNext(arguments, i);
   100 			} else if (option == OPTION_CASE_SENSITIVE) {
   121 			} else if (option == OPTION_CASE_SENSITIVE) {
   101 				ENTITY entity = parseEntity(readNext(arguments, i));
   122 				ENTITY entity = parseEntity(readNext(arguments, i));
   102 				bool value = parseBoolean(readNext(arguments, i));
   123 				bool value = parseBoolean(readNext(arguments, i));
   103 				if (currentRule.attribute.size()) currentRule.caseSensitive[entity] = value;
   124 				if (currentRule.modify == ENTITY::DEFAULT) currentRelation.caseSensitive[entity] = value;
   104 				else currentRelation.caseSensitive[entity] = value;
   125 				currentRule.caseSensitive[entity] = value;
   105 			} else if (option == OPTION_INVERT_MATCH) {
   126 			} else if (option == OPTION_INVERT_MATCH) {
   106 				ENTITY entity = parseEntity(readNext(arguments, i));
   127 				ENTITY entity = parseEntity(readNext(arguments, i));
   107 				bool value = parseBoolean(readNext(arguments, i));
   128 				bool value = parseBoolean(readNext(arguments, i));
   108 				if (currentRule.attribute.size()) currentRule.invertMatch[entity] = value;
   129 				if (currentRule.modify == ENTITY::DEFAULT) currentRelation.invertMatch[entity] = value;
   109 				else currentRelation.invertMatch[entity] = value;
   130 				currentRule.invertMatch[entity] = value;
   110 			} else throw relpipe::cli::RelpipeCLIException(L"Unsupported CLI option: " + option, relpipe::cli::CLI::EXIT_CODE_BAD_CLI_ARGUMENTS);
   131 			} else throw relpipe::cli::RelpipeCLIException(L"Unsupported CLI option: " + option, relpipe::cli::CLI::EXIT_CODE_BAD_CLI_ARGUMENTS);
   111 		}
   132 		}
   112 		addRelation(c, currentRelation, currentRule); // last relation
   133 		addRelation(c, currentRelation, currentRule); // last relation
   113 
   134 
   114 		return c;
   135 		return c;
   117 	virtual ~CLIParser() {
   138 	virtual ~CLIParser() {
   118 	}
   139 	}
   119 };
   140 };
   120 
   141 
   121 const relpipe::common::type::StringX CLIParser::OPTION_RELATION = L"--relation";
   142 const relpipe::common::type::StringX CLIParser::OPTION_RELATION = L"--relation";
       
   143 const relpipe::common::type::StringX CLIParser::OPTION_MODIFY = L"--modify";
   122 const relpipe::common::type::StringX CLIParser::OPTION_ATTRIBUTE = L"--attribute";
   144 const relpipe::common::type::StringX CLIParser::OPTION_ATTRIBUTE = L"--attribute";
   123 const relpipe::common::type::StringX CLIParser::OPTION_VALUE = L"--value";
   145 const relpipe::common::type::StringX CLIParser::OPTION_VALUE = L"--value";
   124 const relpipe::common::type::StringX CLIParser::OPTION_REPLACEMENT = L"--replacement";
   146 const relpipe::common::type::StringX CLIParser::OPTION_REPLACEMENT = L"--replacement";
   125 const relpipe::common::type::StringX CLIParser::OPTION_CASE_SENSITIVE = L"--case-sensitive";
   147 const relpipe::common::type::StringX CLIParser::OPTION_CASE_SENSITIVE = L"--case-sensitive";
   126 const relpipe::common::type::StringX CLIParser::OPTION_INVERT_MATCH = L"--invert-match";
   148 const relpipe::common::type::StringX CLIParser::OPTION_INVERT_MATCH = L"--invert-match";