#include <cstdlib>
#include <memory>
#include <tuple>
#include <RelationalWriter.h>
#include <RelpipeWriterException.h>
#include <Factory.h>
#include <TypeId.h>
#include "CLI.h"
#include "RelpipeCLIException.h"
#include "Command.h"
#include "ArgumentsCommand.h"
#include "DemoCommand.h"
using namespace relpipe::cli;
using namespace relpipe::in::cli;
using namespace relpipe::writer;
Command* findCommand(string_t commandName) {
if (commandName == L"demo") return new DemoCommand();
else if (commandName == L"generate") return new ArgumentsCommand();
else throw RelpipeCLIException(L"Unknown command: " + commandName, CLI::EXIT_CODE_UNKNOWN_COMMAND);
}
int main(int argc, char** argv) {
setlocale(LC_ALL, "");
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;
}