new CLI options: --case-sensitive, --invert-match v_0
authorFrantišek Kučera <franta-hg@frantovo.cz>
Tue, 11 May 2021 20:42:22 +0200
branchv_0
changeset 25 98be80d2e65b
parent 24 c69670b7b4ef
child 26 88e566898f8f
new CLI options: --case-sensitive, --invert-match
bash-completion.sh
src/CLIParser.h
src/Configuration.h
src/GrepHandler.h
src/relpipe-tr-grep.cpp
--- a/bash-completion.sh	Tue May 11 18:30:50 2021 +0200
+++ b/bash-completion.sh	Tue May 11 20:42:22 2021 +0200
@@ -22,15 +22,31 @@
 	w2=${COMP_WORDS[COMP_CWORD-2]}
 	w3=${COMP_WORDS[COMP_CWORD-3]}
 
+	BOOLEAN_VALUES=(
+		"true"
+		"false"
+	)
+
+	ENTITY_VALUES=(
+		"relation"
+		"attribute"
+		"value"
+	)
 
 	if   [[ "$w1" == "--relation"                      && "x$w0" == "x" ]];    then COMPREPLY=("'.*'")
 	elif [[ "$w1" == "--attribute"                     && "x$w0" == "x" ]];    then COMPREPLY=("'.*'")
 	elif [[ "$w1" == "--pattern"                       && "x$w0" == "x" ]];    then COMPREPLY=("''")
+	elif [[ "$w1" == "--case-sensitive"                                 ]];    then COMPREPLY=($(compgen -W "${ENTITY_VALUES[*]}"  -- "$w0"))
+	elif [[ "$w2" == "--case-sensitive"                                 ]];    then COMPREPLY=($(compgen -W "${BOOLEAN_VALUES[*]}" -- "$w0"))
+	elif [[ "$w1" == "--invert-match"                                   ]];    then COMPREPLY=($(compgen -W "${ENTITY_VALUES[*]}"  -- "$w0"))
+	elif [[ "$w2" == "--invert-match"                                   ]];    then COMPREPLY=($(compgen -W "${BOOLEAN_VALUES[*]}" -- "$w0"))
 	else
 		OPTIONS=(
 			"--relation"
 			"--attribute"
 			"--pattern"
+			"--case-sensitive"
+			"--invert-match"
 		)
 		COMPREPLY=($(compgen -W "${OPTIONS[*]}" -- "$w0"))
 	fi
--- a/src/CLIParser.h	Tue May 11 18:30:50 2021 +0200
+++ b/src/CLIParser.h	Tue May 11 20:42:22 2021 +0200
@@ -36,8 +36,28 @@
 		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);
 	}
 
+	/**
+	 * TODO: use a common method
+	 */
+	bool parseBoolean(const relpipe::common::type::StringX& value) {
+		if (value == L"true") return true;
+		else if (value == L"false") return false;
+		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);
+	}
+
+	RelationConfiguration::ENTITY parseEntity(const relpipe::common::type::StringX& value) {
+		if (value == L"relation") return RelationConfiguration::ENTITY::RELATION;
+		else if (value == L"attribute") return RelationConfiguration::ENTITY::ATTRIBUTE;
+		else if (value == L"value") return RelationConfiguration::ENTITY::VALUE;
+		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);
+	}
+
 	void addRelation(Configuration& c, RelationConfiguration& currentRelation) {
 		if (currentRelation.relation.size()) {
+			using E = RelationConfiguration::ENTITY;
+			currentRelation.relationPattern = currentRelation.caseSensitive[E::RELATION] ? std::wregex(currentRelation.relation) : std::wregex(currentRelation.relation, std::regex_constants::icase);
+			currentRelation.attributePattern = currentRelation.caseSensitive[E::ATTRIBUTE] ? std::wregex(currentRelation.attribute) : std::wregex(currentRelation.attribute, std::regex_constants::icase);
+			currentRelation.valuePattern = currentRelation.caseSensitive[E::VALUE] ? std::wregex(currentRelation.value) : std::wregex(currentRelation.value, std::regex_constants::icase);
 			c.relationConfigurations.push_back(currentRelation);
 			currentRelation = RelationConfiguration();
 		}
@@ -48,6 +68,8 @@
 	static const relpipe::common::type::StringX OPTION_RELATION;
 	static const relpipe::common::type::StringX OPTION_ATTRIBUTE;
 	static const relpipe::common::type::StringX OPTION_PATTERN;
+	static const relpipe::common::type::StringX OPTION_CASE_SENSITIVE;
+	static const relpipe::common::type::StringX OPTION_INVERT_MATCH;
 
 	Configuration parse(const std::vector<relpipe::common::type::StringX>& arguments) {
 		Configuration c;
@@ -59,11 +81,16 @@
 			if (option == OPTION_RELATION) {
 				addRelation(c, currentRelation); // previous relation
 				currentRelation.relation = readNext(arguments, i);
-				currentRelation.relationPattern = std::wregex(currentRelation.relation);
 			} else if (option == OPTION_ATTRIBUTE) {
-				currentRelation.attributePattern = std::wregex(readNext(arguments, i));
+				currentRelation.attribute = readNext(arguments, i);
 			} else if (option == OPTION_PATTERN) {
-				currentRelation.valuePattern = std::wregex(readNext(arguments, i));
+				currentRelation.value = readNext(arguments, i);
+			} else if (option == OPTION_CASE_SENSITIVE) {
+				RelationConfiguration::ENTITY entity = parseEntity(readNext(arguments, i));
+				currentRelation.caseSensitive[entity] = parseBoolean(readNext(arguments, i));
+			} else if (option == OPTION_INVERT_MATCH) {
+				RelationConfiguration::ENTITY entity = parseEntity(readNext(arguments, i));
+				currentRelation.invertMatch[entity] = parseBoolean(readNext(arguments, i));
 			} else throw relpipe::cli::RelpipeCLIException(L"Unsupported CLI option: " + option, relpipe::cli::CLI::EXIT_CODE_BAD_CLI_ARGUMENTS);
 		}
 		addRelation(c, currentRelation); // last relation
@@ -78,6 +105,8 @@
 const relpipe::common::type::StringX CLIParser::OPTION_RELATION = L"--relation";
 const relpipe::common::type::StringX CLIParser::OPTION_ATTRIBUTE = L"--attribute";
 const relpipe::common::type::StringX CLIParser::OPTION_PATTERN = L"--pattern";
+const relpipe::common::type::StringX CLIParser::OPTION_CASE_SENSITIVE = L"--case-sensitive";
+const relpipe::common::type::StringX CLIParser::OPTION_INVERT_MATCH = L"--invert-match";
 
 }
 }
--- a/src/Configuration.h	Tue May 11 18:30:50 2021 +0200
+++ b/src/Configuration.h	Tue May 11 20:42:22 2021 +0200
@@ -16,7 +16,7 @@
  */
 #pragma once
 
-#include <vector>
+#include <map>
 #include <regex>
 #include <locale>
 
@@ -30,13 +30,32 @@
 class RelationConfiguration {
 public:
 
+	enum class ENTITY {
+		RELATION,
+		ATTRIBUTE,
+		VALUE
+	};
+
+	RelationConfiguration() {
+		caseSensitive[ENTITY::RELATION] = true;
+		caseSensitive[ENTITY::ATTRIBUTE] = true;
+		caseSensitive[ENTITY::VALUE] = true;
+		invertMatch[ENTITY::RELATION] = false;
+		invertMatch[ENTITY::ATTRIBUTE] = false;
+		invertMatch[ENTITY::VALUE] = false;
+	}
+
 	virtual ~RelationConfiguration() {
 	}
 
-	relpipe::common::type::StringX relation; // TODO: remove and use just relationPattern, see CLIParser
+	relpipe::common::type::StringX relation;
+	relpipe::common::type::StringX attribute;
+	relpipe::common::type::StringX value;
 	std::wregex relationPattern;
 	std::wregex attributePattern;
 	std::wregex valuePattern;
+	std::map<ENTITY, relpipe::common::type::Boolean> caseSensitive;
+	std::map<ENTITY, relpipe::common::type::Boolean> invertMatch;
 };
 
 class Configuration {
--- a/src/GrepHandler.h	Tue May 11 18:30:50 2021 +0200
+++ b/src/GrepHandler.h	Tue May 11 20:42:22 2021 +0200
@@ -73,7 +73,7 @@
 		currentSearchableAttributes.resize(attributes.size(), false);
 		currentFilter = nullptr;
 		for (int i = 0; i < configuration.relationConfigurations.size(); i++) {
-			if (regex_match(name, configuration.relationConfigurations[i].relationPattern)) {
+			if (regex_match(name, configuration.relationConfigurations[i].relationPattern) ^ configuration.relationConfigurations[i].invertMatch[RelationConfiguration::ENTITY::RELATION]) {
 				currentFilter = &configuration.relationConfigurations[i];
 				break;
 			}
@@ -81,7 +81,7 @@
 
 		if (currentFilter) {
 			for (int i = 0; i < currentSearchableAttributes.size(); i++) {
-				currentSearchableAttributes[i] = regex_match(attributes[i].getAttributeName(), currentFilter->attributePattern);
+				currentSearchableAttributes[i] = regex_match(attributes[i].getAttributeName(), currentFilter->attributePattern) ^ currentFilter->invertMatch[RelationConfiguration::ENTITY::ATTRIBUTE];
 			}
 		}
 
@@ -94,11 +94,13 @@
 
 			if (currentSearchableAttributes[currentAttributeIndex]) {
 				includeCurrentRecord |= regex_search(value, currentFilter->valuePattern);
+				// includeCurrentRecord ^= currentFilter->invertMatch[RelationConfiguration::ENTITY::VALUE]; // might be inverted here, but it would be probably less useful
 			}
 
 			currentAttributeIndex++;
 
 			if (currentAttributeIndex > 0 && currentAttributeIndex % currentSearchableAttributes.size() == 0) {
+				includeCurrentRecord ^= currentFilter->invertMatch[RelationConfiguration::ENTITY::VALUE];
 				if (includeCurrentRecord) for (string_t v : currentRecord) relationalWriter->writeAttribute(v);
 				includeCurrentRecord = false;
 			}
--- a/src/relpipe-tr-grep.cpp	Tue May 11 18:30:50 2021 +0200
+++ b/src/relpipe-tr-grep.cpp	Tue May 11 20:42:22 2021 +0200
@@ -43,12 +43,12 @@
 	CLI::untieStdIO();
 	CLI cli(argc, argv);
 
-	CLIParser cliParser;
-	Configuration configuration = cliParser.parse(cli.arguments());
-
 	int resultCode = CLI::EXIT_CODE_UNEXPECTED_ERROR;
 
 	try {
+		CLIParser cliParser;
+		Configuration configuration = cliParser.parse(cli.arguments());
+		
 		std::shared_ptr<writer::RelationalWriter> writer(relpipe::writer::Factory::create(std::cout));
 		std::shared_ptr<RelationalReader> reader(relpipe::reader::Factory::create(std::cin));
 		GrepHandler handler(writer, configuration);