relpipe-in-cli.cpp
author František Kučera <franta-hg@frantovo.cz>
Thu, 29 Nov 2018 22:30:11 +0100
branchv_0
changeset 24 c31fdd965028
parent 21 1f7b203fceab
child 25 454cfb01cf95
permissions -rw-r--r--
license: GNU GPLv3+
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
24
c31fdd965028 license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 21
diff changeset
     1
/**
c31fdd965028 license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 21
diff changeset
     2
 * Relational pipes
c31fdd965028 license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 21
diff changeset
     3
 * Copyright © 2018 František Kučera (Frantovo.cz, GlobalCode.info)
c31fdd965028 license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 21
diff changeset
     4
 *
c31fdd965028 license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 21
diff changeset
     5
 * This program is free software: you can redistribute it and/or modify
c31fdd965028 license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 21
diff changeset
     6
 * it under the terms of the GNU General Public License as published by
c31fdd965028 license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 21
diff changeset
     7
 * the Free Software Foundation, either version 3 of the License, or
c31fdd965028 license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 21
diff changeset
     8
 * (at your option) any later version.
c31fdd965028 license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 21
diff changeset
     9
 *
c31fdd965028 license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 21
diff changeset
    10
 * This program is distributed in the hope that it will be useful,
c31fdd965028 license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 21
diff changeset
    11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
c31fdd965028 license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 21
diff changeset
    12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
c31fdd965028 license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 21
diff changeset
    13
 * GNU General Public License for more details.
c31fdd965028 license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 21
diff changeset
    14
 *
c31fdd965028 license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 21
diff changeset
    15
 * You should have received a copy of the GNU General Public License
c31fdd965028 license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 21
diff changeset
    16
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
c31fdd965028 license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 21
diff changeset
    17
 */
0
3dd6adb7e191 empty project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    18
#include <cstdlib>
1
7e43750deca8 shared library with pure abstract class (interface)
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    19
#include <memory>
0
3dd6adb7e191 empty project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    20
20
a18b6964d300 relpipe-lib-writer: move public header files to: include/relpipe/writer/
František Kučera <franta-hg@frantovo.cz>
parents: 19
diff changeset
    21
#include <relpipe/writer/RelationalWriter.h>
a18b6964d300 relpipe-lib-writer: move public header files to: include/relpipe/writer/
František Kučera <franta-hg@frantovo.cz>
parents: 19
diff changeset
    22
#include <relpipe/writer/RelpipeWriterException.h>
a18b6964d300 relpipe-lib-writer: move public header files to: include/relpipe/writer/
František Kučera <franta-hg@frantovo.cz>
parents: 19
diff changeset
    23
#include <relpipe/writer/Factory.h>
a18b6964d300 relpipe-lib-writer: move public header files to: include/relpipe/writer/
František Kučera <franta-hg@frantovo.cz>
parents: 19
diff changeset
    24
#include <relpipe/writer/TypeId.h>
0
3dd6adb7e191 empty project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    25
19
22f493401ac0 move CLI.h to a common header-only library
František Kučera <franta-hg@frantovo.cz>
parents: 18
diff changeset
    26
#include <relpipe/cli/CLI.h>
21
1f7b203fceab move RelpipeCLIException.h from relpipe-in-cli to relpipe-lib-cli
František Kučera <franta-hg@frantovo.cz>
parents: 20
diff changeset
    27
#include <relpipe/cli/RelpipeCLIException.h>
19
22f493401ac0 move CLI.h to a common header-only library
František Kučera <franta-hg@frantovo.cz>
parents: 18
diff changeset
    28
11
3798b6bc9aea ArgumentsCommand: generate relational data from CLI arguments
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
    29
#include "Command.h"
3798b6bc9aea ArgumentsCommand: generate relational data from CLI arguments
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
    30
#include "ArgumentsCommand.h"
12
bc6fe00dd831 move demo code to DemoCommand.h
František Kučera <franta-hg@frantovo.cz>
parents: 11
diff changeset
    31
#include "DemoCommand.h"
16
70af16946466 StdInCommand: generate relational data from STDIN (and also CLI arguments)
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
    32
#include "StdInCommand.h"
10
77593735b057 CLI infrastructure ported from the prototype
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    33
13
5e95f0c0a4f9 simple command router: findCommand()
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
    34
using namespace relpipe::cli;
5e95f0c0a4f9 simple command router: findCommand()
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
    35
using namespace relpipe::in::cli;
5e95f0c0a4f9 simple command router: findCommand()
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
    36
using namespace relpipe::writer;
5e95f0c0a4f9 simple command router: findCommand()
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
    37
5e95f0c0a4f9 simple command router: findCommand()
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
    38
Command* findCommand(string_t commandName) {
16
70af16946466 StdInCommand: generate relational data from STDIN (and also CLI arguments)
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
    39
	// TODO: better command names
70af16946466 StdInCommand: generate relational data from STDIN (and also CLI arguments)
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
    40
	// TODO: help command
13
5e95f0c0a4f9 simple command router: findCommand()
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
    41
	if (commandName == L"demo") return new DemoCommand();
5e95f0c0a4f9 simple command router: findCommand()
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
    42
	else if (commandName == L"generate") return new ArgumentsCommand();
18
9e543fd0254c StdInCommand(false) should work same as ArgumentsCommand()
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
    43
	else if (commandName == L"generate-without-stdin") return new StdInCommand(false); // TODO: just for testing, StdInCommand(false) should work same as ArgumentsCommand()
9e543fd0254c StdInCommand(false) should work same as ArgumentsCommand()
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
    44
	else if (commandName == L"generate-from-stdin") return new StdInCommand(true);
14
cfed80d11caa introduce and use RelpipeCLIException
František Kučera <franta-hg@frantovo.cz>
parents: 13
diff changeset
    45
	else throw RelpipeCLIException(L"Unknown command: " + commandName, CLI::EXIT_CODE_UNKNOWN_COMMAND);
13
5e95f0c0a4f9 simple command router: findCommand()
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
    46
}
5e95f0c0a4f9 simple command router: findCommand()
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
    47
0
3dd6adb7e191 empty project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    48
int main(int argc, char** argv) {
10
77593735b057 CLI infrastructure ported from the prototype
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    49
	setlocale(LC_ALL, "");
77593735b057 CLI infrastructure ported from the prototype
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    50
	CLI cli(argc, argv);
77593735b057 CLI infrastructure ported from the prototype
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    51
77593735b057 CLI infrastructure ported from the prototype
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    52
	int resultCode = CLI::EXIT_CODE_UNEXPECTED_ERROR;
77593735b057 CLI infrastructure ported from the prototype
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    53
77593735b057 CLI infrastructure ported from the prototype
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    54
	try {
77593735b057 CLI infrastructure ported from the prototype
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    55
		if (cli.arguments().size() > 0) {
77593735b057 CLI infrastructure ported from the prototype
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    56
13
5e95f0c0a4f9 simple command router: findCommand()
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
    57
			const wstring commandName = cli.arguments()[0];
10
77593735b057 CLI infrastructure ported from the prototype
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    58
			vector<wstring> arguments(cli.arguments().size() - 1);
77593735b057 CLI infrastructure ported from the prototype
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    59
			for (int i = 1; i < cli.arguments().size(); i++) {
77593735b057 CLI infrastructure ported from the prototype
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    60
				arguments[i - 1] = cli.arguments()[i];
77593735b057 CLI infrastructure ported from the prototype
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    61
			}
77593735b057 CLI infrastructure ported from the prototype
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    62
13
5e95f0c0a4f9 simple command router: findCommand()
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
    63
			std::shared_ptr<Command> command(findCommand(commandName));
5e95f0c0a4f9 simple command router: findCommand()
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
    64
			command->process(cin, cout, commandName, arguments);
5e95f0c0a4f9 simple command router: findCommand()
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
    65
			resultCode = CLI::EXIT_CODE_SUCCESS;
10
77593735b057 CLI infrastructure ported from the prototype
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    66
77593735b057 CLI infrastructure ported from the prototype
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    67
		} else {
14
cfed80d11caa introduce and use RelpipeCLIException
František Kučera <franta-hg@frantovo.cz>
parents: 13
diff changeset
    68
			throw RelpipeCLIException(L"Missing command…", CLI::EXIT_CODE_BAD_SYNTAX);
10
77593735b057 CLI infrastructure ported from the prototype
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    69
		}
14
cfed80d11caa introduce and use RelpipeCLIException
František Kučera <franta-hg@frantovo.cz>
parents: 13
diff changeset
    70
	} catch (RelpipeCLIException e) {
cfed80d11caa introduce and use RelpipeCLIException
František Kučera <franta-hg@frantovo.cz>
parents: 13
diff changeset
    71
		fwprintf(stderr, L"Caught CLI exception: %ls\n", e.getMessge().c_str());
cfed80d11caa introduce and use RelpipeCLIException
František Kučera <franta-hg@frantovo.cz>
parents: 13
diff changeset
    72
		fwprintf(stderr, L"Debug: Input stream: eof=%ls, lastRead=%d\n", (cin.eof() ? L"true" : L"false"), cin.gcount());
cfed80d11caa introduce and use RelpipeCLIException
František Kučera <franta-hg@frantovo.cz>
parents: 13
diff changeset
    73
		resultCode = e.getExitCode();
10
77593735b057 CLI infrastructure ported from the prototype
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    74
	} catch (RelpipeWriterException e) {
14
cfed80d11caa introduce and use RelpipeCLIException
František Kučera <franta-hg@frantovo.cz>
parents: 13
diff changeset
    75
		fwprintf(stderr, L"Caught Writer exception: %ls\n", e.getMessge().c_str());
10
77593735b057 CLI infrastructure ported from the prototype
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    76
		fwprintf(stderr, L"Debug: Input stream: eof=%ls, lastRead=%d\n", (cin.eof() ? L"true" : L"false"), cin.gcount());
77593735b057 CLI infrastructure ported from the prototype
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    77
		resultCode = CLI::EXIT_CODE_DATA_ERROR;
77593735b057 CLI infrastructure ported from the prototype
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    78
	}
77593735b057 CLI infrastructure ported from the prototype
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    79
77593735b057 CLI infrastructure ported from the prototype
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    80
	return resultCode;
77593735b057 CLI infrastructure ported from the prototype
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    81
}