relpipe-in-cli.cpp
branchv_0
changeset 14 cfed80d11caa
parent 13 5e95f0c0a4f9
child 15 dc2121dec856
equal deleted inserted replaced
13:5e95f0c0a4f9 14:cfed80d11caa
     7 #include <RelpipeWriterException.h>
     7 #include <RelpipeWriterException.h>
     8 #include <Factory.h>
     8 #include <Factory.h>
     9 #include <TypeId.h>
     9 #include <TypeId.h>
    10 
    10 
    11 #include "CLI.h"
    11 #include "CLI.h"
       
    12 #include "RelpipeCLIException.h"
    12 #include "Command.h"
    13 #include "Command.h"
    13 #include "ArgumentsCommand.h"
    14 #include "ArgumentsCommand.h"
    14 #include "DemoCommand.h"
    15 #include "DemoCommand.h"
    15 
    16 
    16 using namespace relpipe::cli;
    17 using namespace relpipe::cli;
    18 using namespace relpipe::writer;
    19 using namespace relpipe::writer;
    19 
    20 
    20 Command* findCommand(string_t commandName) {
    21 Command* findCommand(string_t commandName) {
    21 	if (commandName == L"demo") return new DemoCommand();
    22 	if (commandName == L"demo") return new DemoCommand();
    22 	else if (commandName == L"generate") return new ArgumentsCommand();
    23 	else if (commandName == L"generate") return new ArgumentsCommand();
    23 	else throw new RelpipeWriterException(L"Unknown command: " + commandName); // TODO: CLI expcetion
    24 	else throw RelpipeCLIException(L"Unknown command: " + commandName, CLI::EXIT_CODE_UNKNOWN_COMMAND);
    24 }
    25 }
    25 
    26 
    26 int main(int argc, char** argv) {
    27 int main(int argc, char** argv) {
    27 	setlocale(LC_ALL, "");
    28 	setlocale(LC_ALL, "");
    28 	CLI cli(argc, argv);
    29 	CLI cli(argc, argv);
    41 			std::shared_ptr<Command> command(findCommand(commandName));
    42 			std::shared_ptr<Command> command(findCommand(commandName));
    42 			command->process(cin, cout, commandName, arguments);
    43 			command->process(cin, cout, commandName, arguments);
    43 			resultCode = CLI::EXIT_CODE_SUCCESS;
    44 			resultCode = CLI::EXIT_CODE_SUCCESS;
    44 
    45 
    45 		} else {
    46 		} else {
    46 			fwprintf(stderr, L"Missing command…\n");
    47 			throw RelpipeCLIException(L"Missing command…", CLI::EXIT_CODE_BAD_SYNTAX);
    47 			resultCode = CLI::EXIT_CODE_BAD_SYNTAX;
       
    48 		}
    48 		}
       
    49 	} catch (RelpipeCLIException e) {
       
    50 		fwprintf(stderr, L"Caught CLI exception: %ls\n", e.getMessge().c_str());
       
    51 		fwprintf(stderr, L"Debug: Input stream: eof=%ls, lastRead=%d\n", (cin.eof() ? L"true" : L"false"), cin.gcount());
       
    52 		resultCode = e.getExitCode();
    49 	} catch (RelpipeWriterException e) {
    53 	} catch (RelpipeWriterException e) {
    50 		fwprintf(stderr, L"Caught exception: %ls\n", e.getMessge().c_str());
    54 		fwprintf(stderr, L"Caught Writer exception: %ls\n", e.getMessge().c_str());
    51 		fwprintf(stderr, L"Debug: Input stream: eof=%ls, lastRead=%d\n", (cin.eof() ? L"true" : L"false"), cin.gcount());
    55 		fwprintf(stderr, L"Debug: Input stream: eof=%ls, lastRead=%d\n", (cin.eof() ? L"true" : L"false"), cin.gcount());
    52 		resultCode = CLI::EXIT_CODE_DATA_ERROR;
    56 		resultCode = CLI::EXIT_CODE_DATA_ERROR;
    53 	}
    57 	}
    54 
    58 
    55 	return resultCode;
    59 	return resultCode;