# HG changeset patch # User František Kučera # Date 1532779613 -7200 # Node ID 3798b6bc9aea7816a8c78e55c367c2b30f4daca7 # Parent 77593735b057b0d9fd179b5352f0dc74c3e2a27f ArgumentsCommand: generate relational data from CLI arguments diff -r 77593735b057 -r 3798b6bc9aea ArgumentsCommand.h --- /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 +#include +#include +#include +#include + +#include + +#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& 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 writer(Factory::create(output)); + + std::vector> 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]); + } + } +}; + +} +} +} diff -r 77593735b057 -r 3798b6bc9aea CLI.h --- 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: diff -r 77593735b057 -r 3798b6bc9aea Command.h --- /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 +#include +#include +#include +#include + + +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 &arguments) = 0; + +}; + +} +} +} diff -r 77593735b057 -r 3798b6bc9aea nbproject/configurations.xml --- 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 @@ + ArgumentsCommand.h CLI.h + Command.h + + + + @@ -82,8 +88,12 @@ + + + + diff -r 77593735b057 -r 3798b6bc9aea relpipe-in-cli.cpp --- 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 #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;