simple command router: findCommand() v_0
authorFrantišek Kučera <franta-hg@frantovo.cz>
Sat, 28 Jul 2018 14:26:58 +0200
branchv_0
changeset 13 5e95f0c0a4f9
parent 12 bc6fe00dd831
child 14 cfed80d11caa
simple command router: findCommand()
relpipe-in-cli.cpp
--- a/relpipe-in-cli.cpp	Sat Jul 28 14:16:44 2018 +0200
+++ b/relpipe-in-cli.cpp	Sat Jul 28 14:26:58 2018 +0200
@@ -13,11 +13,17 @@
 #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 new RelpipeWriterException(L"Unknown command: " + commandName); // TODO: CLI expcetion
+}
+
 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);
 
@@ -26,25 +32,15 @@
 	try {
 		if (cli.arguments().size() > 0) {
 
-			const wstring action = cli.arguments()[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];
 			}
 
-			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;
-			}
-
+			std::shared_ptr<Command> command(findCommand(commandName));
+			command->process(cin, cout, commandName, arguments);
+			resultCode = CLI::EXIT_CODE_SUCCESS;
 
 		} else {
 			fwprintf(stderr, L"Missing command…\n");