ArgumentsCommand: generate relational data from CLI arguments v_0
authorFrantišek Kučera <franta-hg@frantovo.cz>
Sat, 28 Jul 2018 14:06:53 +0200
branchv_0
changeset 11 3798b6bc9aea
parent 10 77593735b057
child 12 bc6fe00dd831
ArgumentsCommand: generate relational data from CLI arguments
ArgumentsCommand.h
CLI.h
Command.h
nbproject/configurations.xml
relpipe-in-cli.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ArgumentsCommand.h	Sat Jul 28 14:06:53 2018 +0200
@@ -0,0 +1,50 @@
+#pragma once
+
+#include <cstdlib>
+#include <iostream>
+#include <string>
+#include <vector>
+#include <algorithm>
+
+#include <typedefs.h>
+
+#include "Command.h"
+
+namespace relpipe {
+namespace in {
+namespace cli {
+
+class ArgumentsCommand : public Command {
+public:
+
+	void process(std::istream& input, std::ostream& output, const relpipe::writer::string_t& command, const std::vector<relpipe::writer::string_t>& arguments) override {
+		using namespace relpipe::writer;
+
+		size_t i = 0;
+		string_t relationName = arguments[i++];
+		integer_t attributeCount = std::stoul(arguments[i++]); // TODO: use integer data type's method?
+		boolean_t writeHeader = true; // TODO: add option for header omitting
+
+		// TODO: check argument count
+
+		std::shared_ptr<RelationalWriter> writer(Factory::create(output));
+
+		std::vector<std::pair<string_t, TypeId >> attributes(attributeCount);
+
+		for (size_t j = 0; j < attributeCount; j++) {
+			string_t attributeName = arguments[i++];
+			TypeId attributeType = writer->toTypeId(arguments[i++]);
+			attributes[j] = {attributeName, attributeType};
+		}
+
+		writer->startRelation(relationName, attributes, writeHeader);
+
+		for (; i < arguments.size(); i++) {
+			writer->writeAttribute(arguments[i]);
+		}
+	}
+};
+
+}
+}
+}
--- a/CLI.h	Sat Jul 28 08:13:35 2018 +0200
+++ b/CLI.h	Sat Jul 28 14:06:53 2018 +0200
@@ -12,6 +12,9 @@
 namespace relpipe {
 namespace cli {
 
+/**
+ * TODO: move to relpipe-lib-cli (a common header-only library)
+ */
 class CLI {
 public:
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Command.h	Sat Jul 28 14:06:53 2018 +0200
@@ -0,0 +1,32 @@
+#pragma once
+
+#include <cstdlib>
+#include <iostream>
+#include <string>
+#include <vector>
+#include <algorithm>
+
+
+namespace relpipe {
+namespace in {
+namespace cli {
+
+class Command {
+public:
+	virtual ~Command() = default;
+
+	/**
+	 * Processes the inputs (stream + arguments) and generates output in the relational pipes format.
+	 * 
+	 * @param input usually STDIN, may not be used if all data are passed as CLI arguments
+	 * @param output usually STDOUT, relational data is passed here
+	 * @param command command name, usualy not needed, may be significant if the same Command instance is used for several commands
+	 * @param arguments CLI arguments containing data or parameters (format is specific to each command)
+	 */
+	virtual void process(std::istream& input, std::ostream& output, const relpipe::writer::string_t& command, const std::vector<relpipe::writer::string_t> &arguments) = 0;
+
+};
+
+}
+}
+}
--- a/nbproject/configurations.xml	Sat Jul 28 08:13:35 2018 +0200
+++ b/nbproject/configurations.xml	Sat Jul 28 14:06:53 2018 +0200
@@ -4,7 +4,9 @@
     <logicalFolder name="HeaderFiles"
                    displayName="Header Files"
                    projectFiles="true">
+      <itemPath>ArgumentsCommand.h</itemPath>
       <itemPath>CLI.h</itemPath>
+      <itemPath>Command.h</itemPath>
     </logicalFolder>
     <logicalFolder name="ResourceFiles"
                    displayName="Resource Files"
@@ -51,8 +53,12 @@
           </linkerLibItems>
         </linkerTool>
       </compileType>
+      <item path="ArgumentsCommand.h" ex="false" tool="3" flavor2="0">
+      </item>
       <item path="CLI.h" ex="false" tool="3" flavor2="0">
       </item>
+      <item path="Command.h" ex="false" tool="3" flavor2="0">
+      </item>
       <item path="relpipe-in-cli.cpp" ex="false" tool="1" flavor2="0">
       </item>
     </conf>
@@ -82,8 +88,12 @@
           </linkerLibItems>
         </linkerTool>
       </compileType>
+      <item path="ArgumentsCommand.h" ex="false" tool="3" flavor2="0">
+      </item>
       <item path="CLI.h" ex="false" tool="3" flavor2="0">
       </item>
+      <item path="Command.h" ex="false" tool="3" flavor2="0">
+      </item>
       <item path="relpipe-in-cli.cpp" ex="false" tool="1" flavor2="0">
       </item>
     </conf>
--- a/relpipe-in-cli.cpp	Sat Jul 28 08:13:35 2018 +0200
+++ b/relpipe-in-cli.cpp	Sat Jul 28 14:06:53 2018 +0200
@@ -9,11 +9,14 @@
 #include <TypeId.h>
 
 #include "CLI.h"
+#include "Command.h"
+#include "ArgumentsCommand.h"
 
 int demo();
 
 int main(int argc, char** argv) {
 	using namespace relpipe::cli;
+	using namespace relpipe::in::cli;
 	using namespace relpipe::writer;
 
 	setlocale(LC_ALL, "");
@@ -32,6 +35,9 @@
 
 			if (action == L"demo") {
 				resultCode = demo();
+			} else if (action == L"generate") {
+				ArgumentsCommand command;
+				command.process(cin, cout, action, arguments);
 			} else {
 				fwprintf(stderr, L"Unknown command: %ls\n", action.c_str());
 				resultCode = CLI::EXIT_CODE_UNKNOWN_COMMAND;