# HG changeset patch # User František Kučera # Date 1532783467 -7200 # Node ID cfed80d11caae88b9d2d7381e2f997fb10f26358 # Parent 5e95f0c0a4f9e696bbaefb44545026ded45984c4 introduce and use RelpipeCLIException diff -r 5e95f0c0a4f9 -r cfed80d11caa RelpipeCLIException.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/RelpipeCLIException.h Sat Jul 28 15:11:07 2018 +0200 @@ -0,0 +1,36 @@ +#pragma once + +#include + +#include "CLI.h" + +using namespace std; + +namespace relpipe { +namespace cli { + +/** + * TODO: move to relpipe-lib-cli (a common header-only library) + */ +class RelpipeCLIException { +private: + wstring message; + int exitCode = CLI::EXIT_CODE_UNEXPECTED_ERROR; +public: + + RelpipeCLIException(wstring message, int exitCode) : + message(message), exitCode(exitCode) { + } + + wstring getMessge() { + return message; + } + + int getExitCode() { + return exitCode; + } + +}; + +} +} \ No newline at end of file diff -r 5e95f0c0a4f9 -r cfed80d11caa nbproject/configurations.xml --- a/nbproject/configurations.xml Sat Jul 28 14:26:58 2018 +0200 +++ b/nbproject/configurations.xml Sat Jul 28 15:11:07 2018 +0200 @@ -8,6 +8,7 @@ CLI.h Command.h DemoCommand.h + RelpipeCLIException.h + + @@ -99,6 +102,8 @@ + + diff -r 5e95f0c0a4f9 -r cfed80d11caa relpipe-in-cli.cpp --- a/relpipe-in-cli.cpp Sat Jul 28 14:26:58 2018 +0200 +++ b/relpipe-in-cli.cpp Sat Jul 28 15:11:07 2018 +0200 @@ -9,6 +9,7 @@ #include #include "CLI.h" +#include "RelpipeCLIException.h" #include "Command.h" #include "ArgumentsCommand.h" #include "DemoCommand.h" @@ -20,7 +21,7 @@ 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 + else throw RelpipeCLIException(L"Unknown command: " + commandName, CLI::EXIT_CODE_UNKNOWN_COMMAND); } int main(int argc, char** argv) { @@ -43,11 +44,14 @@ resultCode = CLI::EXIT_CODE_SUCCESS; } else { - fwprintf(stderr, L"Missing command…\n"); - resultCode = CLI::EXIT_CODE_BAD_SYNTAX; + 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 exception: %ls\n", e.getMessge().c_str()); + 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; }