# HG changeset patch # User František Kučera # Date 1532780818 -7200 # Node ID 5e95f0c0a4f9e696bbaefb44545026ded45984c4 # Parent bc6fe00dd8313e518208932219435b326adbec40 simple command router: findCommand() diff -r bc6fe00dd831 -r 5e95f0c0a4f9 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 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(findCommand(commandName)); + command->process(cin, cout, commandName, arguments); + resultCode = CLI::EXIT_CODE_SUCCESS; } else { fwprintf(stderr, L"Missing command…\n");