src/relpipe-in-cli.cpp
branchv_0
changeset 43 3c8ea5dcf793
parent 42 09cd32a65709
child 50 8f262b05c252
--- a/src/relpipe-in-cli.cpp	Tue Sep 22 17:42:19 2020 +0200
+++ b/src/relpipe-in-cli.cpp	Tue Sep 22 21:00:30 2020 +0200
@@ -25,23 +25,15 @@
 #include <relpipe/cli/CLI.h>
 #include <relpipe/cli/RelpipeCLIException.h>
 
-#include "Command.h"
-#include "ArgumentsCommand.h"
-#include "StdInCommand.h"
+#include "CLIParser.h"
+#include "Configuration.h"
+
+#include "CLICommand.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"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();
@@ -50,29 +42,20 @@
 	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) {
+		CLIParser cliParser;
+		Configuration configuration = cliParser.parse(cli.arguments());
+		CLICommand command;
+		std::shared_ptr<RelationalWriter> writer(Factory::create(std::cout));
+		command.process(configuration, writer);
+		resultCode = CLI::EXIT_CODE_SUCCESS;
+	} 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;
+	} 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;