relpipe-in-cli.cpp
author Františ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
permissions -rw-r--r--
ArgumentsCommand: generate relational data from CLI arguments

#include <cstdlib>
#include <memory>

#include <tuple>

#include <RelationalWriter.h>
#include <RelpipeWriterException.h>
#include <Factory.h>
#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, "");
	CLI cli(argc, argv);

	int resultCode = CLI::EXIT_CODE_UNEXPECTED_ERROR;

	try {
		if (cli.arguments().size() > 0) {

			const wstring action = cli.arguments()[0];
			vector<wstring> arguments(cli.arguments().size() - 1);
			for (int i = 1; i < cli.arguments().size(); i++) {
				arguments[i - 1] = cli.arguments()[i];
			}

			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;
			}


		} else {
			fwprintf(stderr, L"Missing command…\n");
			resultCode = CLI::EXIT_CODE_BAD_SYNTAX;
		}
	} catch (RelpipeWriterException e) {
		fwprintf(stderr, L"Caught exception: %ls\n", e.getMessge().c_str());
		fwprintf(stderr, L"Debug: Input stream: eof=%ls, lastRead=%d\n", (cin.eof() ? L"true" : L"false"), cin.gcount());
		resultCode = CLI::EXIT_CODE_DATA_ERROR;
	}


	return resultCode;

}

int demo() {
	using namespace relpipe::writer;
	std::shared_ptr<RelationalWriter> writer(Factory::create(std::cout));


	// Various data types passed as strings
	writer->startRelation(L"table_from_strings",{
		{L"s", TypeId::STRING},
		{L"i", TypeId::INTEGER},
		{L"b", TypeId::BOOLEAN}
	}, true);

	writer->writeAttribute(L"a");
	writer->writeAttribute(L"1");
	writer->writeAttribute(L"true");

	writer->writeAttribute(L"b");
	writer->writeAttribute(L"2");
	writer->writeAttribute(L"false");


	// Various data types passed as raw pointers + typeids
	writer->startRelation(L"from_raw_pointers",{
		{L"s", TypeId::STRING},
		{L"i", TypeId::INTEGER},
		{L"b", TypeId::BOOLEAN}
	}, true);

	string_t sValue;
	integer_t iValue;
	boolean_t bValue;

	for (int i = 0; i < 8; i++) {
		sValue.append(L"*");
		iValue = i + 1;
		bValue = iValue % 2 == 0;
		writer->writeAttribute(&sValue, typeid (sValue));
		writer->writeAttribute(&iValue, typeid (iValue));
		writer->writeAttribute(&bValue, typeid (bValue));
	}

	return 0;
}