src/relpipe-in-cli.cpp
author František Kučera <franta-hg@frantovo.cz>
Tue, 22 Oct 2019 19:48:51 +0200
branchv_0
changeset 37 27f0ffa712d9
parent 26 aadef824dc93
child 42 09cd32a65709
permissions -rw-r--r--
fix license version: GNU GPLv3

/**
 * Relational pipes
 * Copyright © 2018 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/>.
 */
#include <cstdlib>
#include <memory>

#include <relpipe/writer/RelationalWriter.h>
#include <relpipe/writer/RelpipeWriterException.h>
#include <relpipe/writer/Factory.h>
#include <relpipe/writer/TypeId.h>

#include <relpipe/cli/CLI.h>
#include <relpipe/cli/RelpipeCLIException.h>

#include "Command.h"
#include "ArgumentsCommand.h"
#include "DemoCommand.h"
#include "StdInCommand.h"

using namespace relpipe::cli;
using namespace relpipe::in::cli;
using namespace relpipe::writer;

Command* findCommand(string_t commandName) {
	// TODO: better command names
	// TODO: help command
	if (commandName == L"demo") return new DemoCommand();
	else if (commandName == L"generate") return new ArgumentsCommand();
	else if (commandName == L"generate-without-stdin") return new StdInCommand(false); // TODO: just for testing, StdInCommand(false) should work same as ArgumentsCommand()
	else if (commandName == L"generate-from-stdin") return new StdInCommand(true);
	else throw RelpipeCLIException(L"Unknown command: " + commandName, CLI::EXIT_CODE_UNKNOWN_COMMAND);
}

int main(int argc, char** argv) {
	setlocale(LC_ALL, "");
	CLI::untieStdIO();
	CLI cli(argc, argv);

	int resultCode = CLI::EXIT_CODE_UNEXPECTED_ERROR;

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

			const wstring commandName = 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];
			}

			std::shared_ptr<Command> command(findCommand(commandName));
			command->process(cin, cout, commandName, arguments);
			resultCode = CLI::EXIT_CODE_SUCCESS;

		} else {
			throw RelpipeCLIException(L"Missing command…", CLI::EXIT_CODE_BAD_SYNTAX);
		}
	} catch (RelpipeCLIException e) {
		fwprintf(stderr, L"Caught CLI 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 = e.getExitCode();
	} catch (RelpipeWriterException e) {
		fwprintf(stderr, L"Caught Writer 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;
}