new CLI interface: --relation, --attribute, --pattern v_0
authorFrantišek Kučera <franta-hg@frantovo.cz>
Tue, 11 May 2021 18:30:50 +0200
branchv_0
changeset 24 c69670b7b4ef
parent 23 87d22d525a3e
child 25 98be80d2e65b
new CLI interface: --relation, --attribute, --pattern
bash-completion.sh
nbproject/configurations.xml
src/CLIParser.h
src/CMakeLists.txt
src/Configuration.h
src/GrepHandler.h
src/relpipe-tr-grep.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/bash-completion.sh	Tue May 11 18:30:50 2021 +0200
@@ -0,0 +1,39 @@
+# Relational pipes
+# Copyright © 2021 František Kučera (Frantovo.cz, GlobalCode.info)
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, version 3 of the License.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+_relpipe_tr_grep_completion() {
+	local w0 w1 w2 w3
+
+	COMPREPLY=()
+	w0=${COMP_WORDS[COMP_CWORD]}
+	w1=${COMP_WORDS[COMP_CWORD-1]}
+	w2=${COMP_WORDS[COMP_CWORD-2]}
+	w3=${COMP_WORDS[COMP_CWORD-3]}
+
+
+	if   [[ "$w1" == "--relation"                      && "x$w0" == "x" ]];    then COMPREPLY=("'.*'")
+	elif [[ "$w1" == "--attribute"                     && "x$w0" == "x" ]];    then COMPREPLY=("'.*'")
+	elif [[ "$w1" == "--pattern"                       && "x$w0" == "x" ]];    then COMPREPLY=("''")
+	else
+		OPTIONS=(
+			"--relation"
+			"--attribute"
+			"--pattern"
+		)
+		COMPREPLY=($(compgen -W "${OPTIONS[*]}" -- "$w0"))
+	fi
+}
+
+complete -F _relpipe_tr_grep_completion relpipe-tr-grep
--- a/nbproject/configurations.xml	Sat Oct 24 00:08:19 2020 +0200
+++ b/nbproject/configurations.xml	Tue May 11 18:30:50 2021 +0200
@@ -81,6 +81,7 @@
             <incDir>
               <pElem>../relpipe-lib-reader.cpp/include</pElem>
               <pElem>../relpipe-lib-writer.cpp/include</pElem>
+              <pElem>../relpipe-lib-common.cpp/include</pElem>
               <pElem>../relpipe-lib-cli.cpp/include</pElem>
               <pElem>build/Debug/src</pElem>
             </incDir>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/CLIParser.h	Tue May 11 18:30:50 2021 +0200
@@ -0,0 +1,84 @@
+/**
+ * Relational pipes
+ * Copyright © 2021 František Kučera (Frantovo.cz, GlobalCode.info)
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, version 3 of the License.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+#pragma once
+
+#include <vector>
+
+#include <relpipe/common/type/typedefs.h>
+#include <relpipe/cli/CLI.h>
+#include <relpipe/cli/RelpipeCLIException.h>
+
+#include "Configuration.h"
+
+namespace relpipe {
+namespace tr {
+namespace grep {
+
+class CLIParser {
+private:
+
+	relpipe::common::type::StringX readNext(std::vector<relpipe::common::type::StringX> arguments, int& i) {
+		if (i < arguments.size()) return arguments[i++];
+		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);
+	}
+
+	void addRelation(Configuration& c, RelationConfiguration& currentRelation) {
+		if (currentRelation.relation.size()) {
+			c.relationConfigurations.push_back(currentRelation);
+			currentRelation = RelationConfiguration();
+		}
+	}
+
+public:
+
+	static const relpipe::common::type::StringX OPTION_RELATION;
+	static const relpipe::common::type::StringX OPTION_ATTRIBUTE;
+	static const relpipe::common::type::StringX OPTION_PATTERN;
+
+	Configuration parse(const std::vector<relpipe::common::type::StringX>& arguments) {
+		Configuration c;
+		RelationConfiguration currentRelation;
+
+		for (int i = 0; i < arguments.size();) {
+			relpipe::common::type::StringX option = readNext(arguments, i);
+
+			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));
+			} else if (option == OPTION_PATTERN) {
+				currentRelation.valuePattern = std::wregex(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
+
+		return c;
+	}
+
+	virtual ~CLIParser() {
+	}
+};
+
+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";
+
+}
+}
+}
--- a/src/CMakeLists.txt	Sat Oct 24 00:08:19 2020 +0200
+++ b/src/CMakeLists.txt	Tue May 11 18:30:50 2021 +0200
@@ -17,7 +17,7 @@
 
 # Relpipe libraries:
 INCLUDE(FindPkgConfig)
-pkg_check_modules (RELPIPE_LIBS relpipe-lib-reader.cpp relpipe-lib-writer.cpp relpipe-lib-cli.cpp)
+pkg_check_modules (RELPIPE_LIBS relpipe-lib-reader.cpp relpipe-lib-writer.cpp relpipe-lib-common.cpp relpipe-lib-cli.cpp)
 include_directories(${RELPIPE_LIBS_INCLUDE_DIRS})
 link_directories(${RELPIPE_LIBS_LIBRARY_DIRS})
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/Configuration.h	Tue May 11 18:30:50 2021 +0200
@@ -0,0 +1,52 @@
+/**
+ * Relational pipes
+ * Copyright © 2021 František Kučera (Frantovo.cz, GlobalCode.info)
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, version 3 of the License.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+#pragma once
+
+#include <vector>
+#include <regex>
+#include <locale>
+
+#include <relpipe/common/type/typedefs.h>
+
+
+namespace relpipe {
+namespace tr {
+namespace grep {
+
+class RelationConfiguration {
+public:
+
+	virtual ~RelationConfiguration() {
+	}
+
+	relpipe::common::type::StringX relation; // TODO: remove and use just relationPattern, see CLIParser
+	std::wregex relationPattern;
+	std::wregex attributePattern;
+	std::wregex valuePattern;
+};
+
+class Configuration {
+public:
+	std::vector<RelationConfiguration> relationConfigurations;
+
+	virtual ~Configuration() {
+	}
+};
+
+}
+}
+}
\ No newline at end of file
--- a/src/GrepHandler.h	Sat Oct 24 00:08:19 2020 +0200
+++ b/src/GrepHandler.h	Tue May 11 18:30:50 2021 +0200
@@ -34,6 +34,8 @@
 
 #include <relpipe/cli/RelpipeCLIException.h>
 
+#include "Configuration.h"
+
 namespace relpipe {
 namespace tr {
 namespace grep {
@@ -46,29 +48,17 @@
 class GrepHandler : public RelationalReaderStringHandler {
 private:
 	shared_ptr<writer::RelationalWriter> relationalWriter;
-
-	wregex relationNameRegEx;
-	wregex attributeNameRegEx;
-	wregex searchRegEx;
+	Configuration configuration;
+	RelationConfiguration* currentFilter = nullptr;
 
 	vector<boolean_t> currentSearchableAttributes;
 	vector<string_t> currentRecord;
 	integer_t currentAttributeIndex = 0;
 	boolean_t includeCurrentRecord = false;
-	boolean_t filterCurrentRelation = false;
 
 public:
 
-	GrepHandler(ostream& output, const vector<string_t>& arguments) {
-		relationalWriter.reset(writer::Factory::create(output));
-
-		if (arguments.size() == 3) {
-			relationNameRegEx = wregex(arguments[0]);
-			attributeNameRegEx = wregex(arguments[1]);
-			searchRegEx = wregex(arguments[2]);
-		} else {
-			throw cli::RelpipeCLIException(L"Usage: relpipe-tr-grep <relationNameRegExp> <attributeNameRegExp> <searchRegExp>", cli::CLI::EXIT_CODE_UNKNOWN_COMMAND);
-		}
+	GrepHandler(shared_ptr<writer::RelationalWriter> relationalWriter, Configuration configuration) : relationalWriter(relationalWriter), configuration(configuration) {
 	}
 
 	void startRelation(string_t name, vector<AttributeMetadata> attributes) override {
@@ -81,10 +71,17 @@
 
 		currentRecord.resize(attributes.size());
 		currentSearchableAttributes.resize(attributes.size(), false);
-		filterCurrentRelation = regex_match(name, relationNameRegEx);
-		if (filterCurrentRelation) {
+		currentFilter = nullptr;
+		for (int i = 0; i < configuration.relationConfigurations.size(); i++) {
+			if (regex_match(name, configuration.relationConfigurations[i].relationPattern)) {
+				currentFilter = &configuration.relationConfigurations[i];
+				break;
+			}
+		}
+
+		if (currentFilter) {
 			for (int i = 0; i < currentSearchableAttributes.size(); i++) {
-				currentSearchableAttributes[i] = regex_match(attributes[i].getAttributeName(), attributeNameRegEx);
+				currentSearchableAttributes[i] = regex_match(attributes[i].getAttributeName(), currentFilter->attributePattern);
 			}
 		}
 
@@ -92,11 +89,11 @@
 	}
 
 	void attribute(const string_t& value) override {
-		if (filterCurrentRelation) {
+		if (currentFilter) {
 			currentRecord[currentAttributeIndex] = value;
 
 			if (currentSearchableAttributes[currentAttributeIndex]) {
-				includeCurrentRecord |= regex_search(value, searchRegEx);
+				includeCurrentRecord |= regex_search(value, currentFilter->valuePattern);
 			}
 
 			currentAttributeIndex++;
--- a/src/relpipe-tr-grep.cpp	Sat Oct 24 00:08:19 2020 +0200
+++ b/src/relpipe-tr-grep.cpp	Tue May 11 18:30:50 2021 +0200
@@ -31,10 +31,11 @@
 #include <relpipe/writer/Factory.h>
 #include <relpipe/writer/TypeId.h>
 
+#include "Configuration.h"
+#include "CLIParser.h"
 #include "GrepHandler.h"
 
 using namespace relpipe::cli;
-using namespace relpipe::reader;
 using namespace relpipe::tr::grep;
 
 int main(int argc, char**argv) {
@@ -42,11 +43,15 @@
 	CLI::untieStdIO();
 	CLI cli(argc, argv);
 
+	CLIParser cliParser;
+	Configuration configuration = cliParser.parse(cli.arguments());
+
 	int resultCode = CLI::EXIT_CODE_UNEXPECTED_ERROR;
 
 	try {
-		std::shared_ptr<RelationalReader> reader(Factory::create(std::cin));
-		GrepHandler handler(std::cout, 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);
 		reader->addHandler(&handler);
 		reader->process();