src/relpipe-in-cli.cpp
branchv_0
changeset 43 3c8ea5dcf793
parent 42 09cd32a65709
child 50 8f262b05c252
equal deleted inserted replaced
42:09cd32a65709 43:3c8ea5dcf793
    23 #include <relpipe/writer/TypeId.h>
    23 #include <relpipe/writer/TypeId.h>
    24 
    24 
    25 #include <relpipe/cli/CLI.h>
    25 #include <relpipe/cli/CLI.h>
    26 #include <relpipe/cli/RelpipeCLIException.h>
    26 #include <relpipe/cli/RelpipeCLIException.h>
    27 
    27 
    28 #include "Command.h"
    28 #include "CLIParser.h"
    29 #include "ArgumentsCommand.h"
    29 #include "Configuration.h"
    30 #include "StdInCommand.h"
    30 
       
    31 #include "CLICommand.h"
    31 
    32 
    32 using namespace relpipe::cli;
    33 using namespace relpipe::cli;
    33 using namespace relpipe::in::cli;
    34 using namespace relpipe::in::cli;
    34 using namespace relpipe::writer;
    35 using namespace relpipe::writer;
    35 
       
    36 Command* findCommand(string_t commandName) {
       
    37 	// TODO: better command names
       
    38 	// TODO: help command
       
    39 	if (commandName == L"generate") return new ArgumentsCommand();
       
    40 	else if (commandName == L"generate-without-stdin") return new StdInCommand(false); // TODO: just for testing, StdInCommand(false) should work same as ArgumentsCommand()
       
    41 	else if (commandName == L"generate-from-stdin") return new StdInCommand(true);
       
    42 	else throw RelpipeCLIException(L"Unknown command: " + commandName, CLI::EXIT_CODE_UNKNOWN_COMMAND);
       
    43 }
       
    44 
    36 
    45 int main(int argc, char** argv) {
    37 int main(int argc, char** argv) {
    46 	setlocale(LC_ALL, "");
    38 	setlocale(LC_ALL, "");
    47 	CLI::untieStdIO();
    39 	CLI::untieStdIO();
    48 	CLI cli(argc, argv);
    40 	CLI cli(argc, argv);
    49 
    41 
    50 	int resultCode = CLI::EXIT_CODE_UNEXPECTED_ERROR;
    42 	int resultCode = CLI::EXIT_CODE_UNEXPECTED_ERROR;
    51 
    43 
    52 	try {
    44 	try {
    53 		if (cli.arguments().size() > 0) {
    45 		CLIParser cliParser;
    54 
    46 		Configuration configuration = cliParser.parse(cli.arguments());
    55 			const wstring commandName = cli.arguments()[0];
    47 		CLICommand command;
    56 			vector<wstring> arguments(cli.arguments().size() - 1);
    48 		std::shared_ptr<RelationalWriter> writer(Factory::create(std::cout));
    57 			for (int i = 1; i < cli.arguments().size(); i++) {
    49 		command.process(configuration, writer);
    58 				arguments[i - 1] = cli.arguments()[i];
    50 		resultCode = CLI::EXIT_CODE_SUCCESS;
    59 			}
    51 	} catch (RelpipeWriterException& e) {
    60 
    52 		fwprintf(stderr, L"Caught Writer exception: %ls\n", e.getMessge().c_str());
    61 			std::shared_ptr<Command> command(findCommand(commandName));
    53 		fwprintf(stderr, L"Debug: Input stream: eof=%ls, lastRead=%d\n", (cin.eof() ? L"true" : L"false"), cin.gcount());
    62 			command->process(cin, cout, commandName, arguments);
    54 		resultCode = CLI::EXIT_CODE_DATA_ERROR;
    63 			resultCode = CLI::EXIT_CODE_SUCCESS;
    55 	} catch (RelpipeCLIException& e) {
    64 
       
    65 		} else {
       
    66 			throw RelpipeCLIException(L"Missing command…", CLI::EXIT_CODE_BAD_SYNTAX);
       
    67 		}
       
    68 	} catch (RelpipeCLIException e) {
       
    69 		fwprintf(stderr, L"Caught CLI exception: %ls\n", e.getMessge().c_str());
    56 		fwprintf(stderr, L"Caught CLI exception: %ls\n", e.getMessge().c_str());
    70 		fwprintf(stderr, L"Debug: Input stream: eof=%ls, lastRead=%d\n", (cin.eof() ? L"true" : L"false"), cin.gcount());
    57 		fwprintf(stderr, L"Debug: Input stream: eof=%ls, lastRead=%d\n", (cin.eof() ? L"true" : L"false"), cin.gcount());
    71 		resultCode = e.getExitCode();
    58 		resultCode = e.getExitCode();
    72 	} catch (RelpipeWriterException e) {
       
    73 		fwprintf(stderr, L"Caught Writer exception: %ls\n", e.getMessge().c_str());
       
    74 		fwprintf(stderr, L"Debug: Input stream: eof=%ls, lastRead=%d\n", (cin.eof() ? L"true" : L"false"), cin.gcount());
       
    75 		resultCode = CLI::EXIT_CODE_DATA_ERROR;
       
    76 	}
    59 	}
    77 
    60 
    78 	return resultCode;
    61 	return resultCode;
    79 }
    62 }