src/relpipe-in-cli.cpp
branchv_0
changeset 26 aadef824dc93
parent 25 454cfb01cf95
child 37 27f0ffa712d9
equal deleted inserted replaced
25:454cfb01cf95 26:aadef824dc93
       
     1 /**
       
     2  * Relational pipes
       
     3  * Copyright © 2018 František Kučera (Frantovo.cz, GlobalCode.info)
       
     4  *
       
     5  * This program is free software: you can redistribute it and/or modify
       
     6  * it under the terms of the GNU General Public License as published by
       
     7  * the Free Software Foundation, either version 3 of the License, or
       
     8  * (at your option) any later version.
       
     9  *
       
    10  * This program is distributed in the hope that it will be useful,
       
    11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
       
    13  * GNU General Public License for more details.
       
    14  *
       
    15  * You should have received a copy of the GNU General Public License
       
    16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
       
    17  */
       
    18 #include <cstdlib>
       
    19 #include <memory>
       
    20 
       
    21 #include <relpipe/writer/RelationalWriter.h>
       
    22 #include <relpipe/writer/RelpipeWriterException.h>
       
    23 #include <relpipe/writer/Factory.h>
       
    24 #include <relpipe/writer/TypeId.h>
       
    25 
       
    26 #include <relpipe/cli/CLI.h>
       
    27 #include <relpipe/cli/RelpipeCLIException.h>
       
    28 
       
    29 #include "Command.h"
       
    30 #include "ArgumentsCommand.h"
       
    31 #include "DemoCommand.h"
       
    32 #include "StdInCommand.h"
       
    33 
       
    34 using namespace relpipe::cli;
       
    35 using namespace relpipe::in::cli;
       
    36 using namespace relpipe::writer;
       
    37 
       
    38 Command* findCommand(string_t commandName) {
       
    39 	// TODO: better command names
       
    40 	// TODO: help command
       
    41 	if (commandName == L"demo") return new DemoCommand();
       
    42 	else if (commandName == L"generate") return new ArgumentsCommand();
       
    43 	else if (commandName == L"generate-without-stdin") return new StdInCommand(false); // TODO: just for testing, StdInCommand(false) should work same as ArgumentsCommand()
       
    44 	else if (commandName == L"generate-from-stdin") return new StdInCommand(true);
       
    45 	else throw RelpipeCLIException(L"Unknown command: " + commandName, CLI::EXIT_CODE_UNKNOWN_COMMAND);
       
    46 }
       
    47 
       
    48 int main(int argc, char** argv) {
       
    49 	setlocale(LC_ALL, "");
       
    50 	CLI::untieStdIO();
       
    51 	CLI cli(argc, argv);
       
    52 
       
    53 	int resultCode = CLI::EXIT_CODE_UNEXPECTED_ERROR;
       
    54 
       
    55 	try {
       
    56 		if (cli.arguments().size() > 0) {
       
    57 
       
    58 			const wstring commandName = cli.arguments()[0];
       
    59 			vector<wstring> arguments(cli.arguments().size() - 1);
       
    60 			for (int i = 1; i < cli.arguments().size(); i++) {
       
    61 				arguments[i - 1] = cli.arguments()[i];
       
    62 			}
       
    63 
       
    64 			std::shared_ptr<Command> command(findCommand(commandName));
       
    65 			command->process(cin, cout, commandName, arguments);
       
    66 			resultCode = CLI::EXIT_CODE_SUCCESS;
       
    67 
       
    68 		} else {
       
    69 			throw RelpipeCLIException(L"Missing command…", CLI::EXIT_CODE_BAD_SYNTAX);
       
    70 		}
       
    71 	} catch (RelpipeCLIException e) {
       
    72 		fwprintf(stderr, L"Caught CLI exception: %ls\n", e.getMessge().c_str());
       
    73 		fwprintf(stderr, L"Debug: Input stream: eof=%ls, lastRead=%d\n", (cin.eof() ? L"true" : L"false"), cin.gcount());
       
    74 		resultCode = e.getExitCode();
       
    75 	} catch (RelpipeWriterException e) {
       
    76 		fwprintf(stderr, L"Caught Writer exception: %ls\n", e.getMessge().c_str());
       
    77 		fwprintf(stderr, L"Debug: Input stream: eof=%ls, lastRead=%d\n", (cin.eof() ? L"true" : L"false"), cin.gcount());
       
    78 		resultCode = CLI::EXIT_CODE_DATA_ERROR;
       
    79 	}
       
    80 
       
    81 	return resultCode;
       
    82 }