relpipe-in-cli.cpp
author František Kučera <franta-hg@frantovo.cz>
Sun, 09 Sep 2018 23:20:59 +0200
branchv_0
changeset 23 9c38f6458115
parent 21 1f7b203fceab
child 24 c31fdd965028
permissions -rw-r--r--
use AttributeMetadata instead of std::pair for relation header
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
3dd6adb7e191 empty project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     1
#include <cstdlib>
1
7e43750deca8 shared library with pure abstract class (interface)
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
     2
#include <memory>
0
3dd6adb7e191 empty project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     3
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
     4
#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
     5
#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
     6
#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
     7
#include <relpipe/writer/TypeId.h>
0
3dd6adb7e191 empty project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     8
19
22f493401ac0 move CLI.h to a common header-only library
František Kučera <franta-hg@frantovo.cz>
parents: 18
diff changeset
     9
#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
    10
#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
    11
11
3798b6bc9aea ArgumentsCommand: generate relational data from CLI arguments
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
    12
#include "Command.h"
3798b6bc9aea ArgumentsCommand: generate relational data from CLI arguments
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
    13
#include "ArgumentsCommand.h"
12
bc6fe00dd831 move demo code to DemoCommand.h
František Kučera <franta-hg@frantovo.cz>
parents: 11
diff changeset
    14
#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
    15
#include "StdInCommand.h"
10
77593735b057 CLI infrastructure ported from the prototype
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    16
13
5e95f0c0a4f9 simple command router: findCommand()
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
    17
using namespace relpipe::cli;
5e95f0c0a4f9 simple command router: findCommand()
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
    18
using namespace relpipe::in::cli;
5e95f0c0a4f9 simple command router: findCommand()
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
    19
using namespace relpipe::writer;
5e95f0c0a4f9 simple command router: findCommand()
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
    20
5e95f0c0a4f9 simple command router: findCommand()
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
    21
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
    22
	// 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
    23
	// TODO: help command
13
5e95f0c0a4f9 simple command router: findCommand()
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
    24
	if (commandName == L"demo") return new DemoCommand();
5e95f0c0a4f9 simple command router: findCommand()
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
    25
	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
    26
	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
    27
	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
    28
	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
    29
}
5e95f0c0a4f9 simple command router: findCommand()
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
    30
0
3dd6adb7e191 empty project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    31
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
    32
	setlocale(LC_ALL, "");
77593735b057 CLI infrastructure ported from the prototype
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    33
	CLI cli(argc, argv);
77593735b057 CLI infrastructure ported from the prototype
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    34
77593735b057 CLI infrastructure ported from the prototype
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    35
	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
    36
77593735b057 CLI infrastructure ported from the prototype
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    37
	try {
77593735b057 CLI infrastructure ported from the prototype
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    38
		if (cli.arguments().size() > 0) {
77593735b057 CLI infrastructure ported from the prototype
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    39
13
5e95f0c0a4f9 simple command router: findCommand()
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
    40
			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
    41
			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
    42
			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
    43
				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
    44
			}
77593735b057 CLI infrastructure ported from the prototype
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    45
13
5e95f0c0a4f9 simple command router: findCommand()
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
    46
			std::shared_ptr<Command> command(findCommand(commandName));
5e95f0c0a4f9 simple command router: findCommand()
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
    47
			command->process(cin, cout, commandName, arguments);
5e95f0c0a4f9 simple command router: findCommand()
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
    48
			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
    49
77593735b057 CLI infrastructure ported from the prototype
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    50
		} else {
14
cfed80d11caa introduce and use RelpipeCLIException
František Kučera <franta-hg@frantovo.cz>
parents: 13
diff changeset
    51
			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
    52
		}
14
cfed80d11caa introduce and use RelpipeCLIException
František Kučera <franta-hg@frantovo.cz>
parents: 13
diff changeset
    53
	} catch (RelpipeCLIException e) {
cfed80d11caa introduce and use RelpipeCLIException
František Kučera <franta-hg@frantovo.cz>
parents: 13
diff changeset
    54
		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
    55
		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
    56
		resultCode = e.getExitCode();
10
77593735b057 CLI infrastructure ported from the prototype
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    57
	} catch (RelpipeWriterException e) {
14
cfed80d11caa introduce and use RelpipeCLIException
František Kučera <franta-hg@frantovo.cz>
parents: 13
diff changeset
    58
		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
    59
		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
    60
		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
    61
	}
77593735b057 CLI infrastructure ported from the prototype
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    62
77593735b057 CLI infrastructure ported from the prototype
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    63
	return resultCode;
77593735b057 CLI infrastructure ported from the prototype
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    64
}