relpipe-in-cli.cpp
author František Kučera <franta-hg@frantovo.cz>
Sat, 28 Jul 2018 14:16:44 +0200
branchv_0
changeset 12 bc6fe00dd831
parent 11 3798b6bc9aea
child 13 5e95f0c0a4f9
permissions -rw-r--r--
move demo code to DemoCommand.h

#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"
#include "DemoCommand.h"

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") {
				DemoCommand command;
				command.process(cin, cout, action, arguments);
				resultCode = CLI::EXIT_CODE_SUCCESS;
			} else if (action == L"generate") {
				ArgumentsCommand command;
				command.process(cin, cout, action, arguments);
				resultCode = CLI::EXIT_CODE_SUCCESS;
			} 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;
}