relpipe-in-cli.cpp
author František Kučera <franta-hg@frantovo.cz>
Sat, 28 Jul 2018 15:42:42 +0200
branchv_0
changeset 15 dc2121dec856
parent 14 cfed80d11caa
child 16 70af16946466
permissions -rw-r--r--
ASan: add AddressSanitizer g++ option: -fsanitize=address
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
1
7e43750deca8 shared library with pure abstract class (interface)
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
     4
#include <RelationalWriter.h>
10
77593735b057 CLI infrastructure ported from the prototype
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
     5
#include <RelpipeWriterException.h>
6
4585c212a767 move create() method to a factory
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
     6
#include <Factory.h>
5
83dd71fe5cfd use TypeId enum instead of numeric constants
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
     7
#include <TypeId.h>
0
3dd6adb7e191 empty project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     8
10
77593735b057 CLI infrastructure ported from the prototype
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
     9
#include "CLI.h"
14
cfed80d11caa introduce and use RelpipeCLIException
František Kučera <franta-hg@frantovo.cz>
parents: 13
diff changeset
    10
#include "RelpipeCLIException.h"
11
3798b6bc9aea ArgumentsCommand: generate relational data from CLI arguments
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
    11
#include "Command.h"
3798b6bc9aea ArgumentsCommand: generate relational data from CLI arguments
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
    12
#include "ArgumentsCommand.h"
12
bc6fe00dd831 move demo code to DemoCommand.h
František Kučera <franta-hg@frantovo.cz>
parents: 11
diff changeset
    13
#include "DemoCommand.h"
10
77593735b057 CLI infrastructure ported from the prototype
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    14
13
5e95f0c0a4f9 simple command router: findCommand()
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
    15
using namespace relpipe::cli;
5e95f0c0a4f9 simple command router: findCommand()
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
    16
using namespace relpipe::in::cli;
5e95f0c0a4f9 simple command router: findCommand()
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
    17
using namespace relpipe::writer;
5e95f0c0a4f9 simple command router: findCommand()
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
    18
5e95f0c0a4f9 simple command router: findCommand()
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
    19
Command* findCommand(string_t commandName) {
5e95f0c0a4f9 simple command router: findCommand()
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
    20
	if (commandName == L"demo") return new DemoCommand();
5e95f0c0a4f9 simple command router: findCommand()
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
    21
	else if (commandName == L"generate") return new ArgumentsCommand();
14
cfed80d11caa introduce and use RelpipeCLIException
František Kučera <franta-hg@frantovo.cz>
parents: 13
diff changeset
    22
	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
    23
}
5e95f0c0a4f9 simple command router: findCommand()
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
    24
0
3dd6adb7e191 empty project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    25
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
    26
	setlocale(LC_ALL, "");
77593735b057 CLI infrastructure ported from the prototype
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    27
	CLI cli(argc, argv);
77593735b057 CLI infrastructure ported from the prototype
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    28
77593735b057 CLI infrastructure ported from the prototype
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    29
	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
    30
77593735b057 CLI infrastructure ported from the prototype
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    31
	try {
77593735b057 CLI infrastructure ported from the prototype
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    32
		if (cli.arguments().size() > 0) {
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
			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
    35
			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
    36
			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
    37
				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
    38
			}
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
			std::shared_ptr<Command> command(findCommand(commandName));
5e95f0c0a4f9 simple command router: findCommand()
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
    41
			command->process(cin, cout, commandName, arguments);
5e95f0c0a4f9 simple command router: findCommand()
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
    42
			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
    43
77593735b057 CLI infrastructure ported from the prototype
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    44
		} else {
14
cfed80d11caa introduce and use RelpipeCLIException
František Kučera <franta-hg@frantovo.cz>
parents: 13
diff changeset
    45
			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
    46
		}
14
cfed80d11caa introduce and use RelpipeCLIException
František Kučera <franta-hg@frantovo.cz>
parents: 13
diff changeset
    47
	} catch (RelpipeCLIException e) {
cfed80d11caa introduce and use RelpipeCLIException
František Kučera <franta-hg@frantovo.cz>
parents: 13
diff changeset
    48
		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
    49
		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
    50
		resultCode = e.getExitCode();
10
77593735b057 CLI infrastructure ported from the prototype
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    51
	} catch (RelpipeWriterException e) {
14
cfed80d11caa introduce and use RelpipeCLIException
František Kučera <franta-hg@frantovo.cz>
parents: 13
diff changeset
    52
		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
    53
		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
    54
		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
    55
	}
77593735b057 CLI infrastructure ported from the prototype
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    56
77593735b057 CLI infrastructure ported from the prototype
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    57
	return resultCode;
77593735b057 CLI infrastructure ported from the prototype
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    58
}