src/relpipe-in-cli.cpp
author František Kučera <franta-hg@frantovo.cz>
Sat, 27 Nov 2021 23:15:14 +0100
branchv_0
changeset 50 8f262b05c252
parent 43 3c8ea5dcf793
permissions -rw-r--r--
exception: fix typo: getMessge() → getMessage()
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
37
27f0ffa712d9 fix license version: GNU GPLv3
František Kučera <franta-hg@frantovo.cz>
parents: 26
diff changeset
     7
 * the Free Software Foundation, version 3 of the License.
24
c31fdd965028 license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 21
diff changeset
     8
 *
c31fdd965028 license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 21
diff changeset
     9
 * 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
    10
 * 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
    11
 * 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
    12
 * GNU General Public License for more details.
c31fdd965028 license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 21
diff changeset
    13
 *
c31fdd965028 license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 21
diff changeset
    14
 * 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
    15
 * 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
    16
 */
0
3dd6adb7e191 empty project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    17
#include <cstdlib>
1
7e43750deca8 shared library with pure abstract class (interface)
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    18
#include <memory>
0
3dd6adb7e191 empty project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    19
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
    20
#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
    21
#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
    22
#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
    23
#include <relpipe/writer/TypeId.h>
0
3dd6adb7e191 empty project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    24
19
22f493401ac0 move CLI.h to a common header-only library
František Kučera <franta-hg@frantovo.cz>
parents: 18
diff changeset
    25
#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
    26
#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
    27
43
3c8ea5dcf793 change CLI interface: options: --relation --attribute --record --records --records-from-stdin
František Kučera <franta-hg@frantovo.cz>
parents: 42
diff changeset
    28
#include "CLIParser.h"
3c8ea5dcf793 change CLI interface: options: --relation --attribute --record --records --records-from-stdin
František Kučera <franta-hg@frantovo.cz>
parents: 42
diff changeset
    29
#include "Configuration.h"
3c8ea5dcf793 change CLI interface: options: --relation --attribute --record --records --records-from-stdin
František Kučera <franta-hg@frantovo.cz>
parents: 42
diff changeset
    30
3c8ea5dcf793 change CLI interface: options: --relation --attribute --record --records --records-from-stdin
František Kučera <franta-hg@frantovo.cz>
parents: 42
diff changeset
    31
#include "CLICommand.h"
10
77593735b057 CLI infrastructure ported from the prototype
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    32
13
5e95f0c0a4f9 simple command router: findCommand()
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
    33
using namespace relpipe::cli;
5e95f0c0a4f9 simple command router: findCommand()
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
    34
using namespace relpipe::in::cli;
5e95f0c0a4f9 simple command router: findCommand()
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
    35
using namespace relpipe::writer;
5e95f0c0a4f9 simple command router: findCommand()
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
    36
0
3dd6adb7e191 empty project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    37
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
    38
	setlocale(LC_ALL, "");
25
454cfb01cf95 CLI: untieStdIO() to avoid unwanted implicit flush() calls on std::cout → less write() calls
František Kučera <franta-hg@frantovo.cz>
parents: 24
diff changeset
    39
	CLI::untieStdIO();
10
77593735b057 CLI infrastructure ported from the prototype
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    40
	CLI cli(argc, argv);
77593735b057 CLI infrastructure ported from the prototype
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    41
77593735b057 CLI infrastructure ported from the prototype
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    42
	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
    43
77593735b057 CLI infrastructure ported from the prototype
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    44
	try {
43
3c8ea5dcf793 change CLI interface: options: --relation --attribute --record --records --records-from-stdin
František Kučera <franta-hg@frantovo.cz>
parents: 42
diff changeset
    45
		CLIParser cliParser;
3c8ea5dcf793 change CLI interface: options: --relation --attribute --record --records --records-from-stdin
František Kučera <franta-hg@frantovo.cz>
parents: 42
diff changeset
    46
		Configuration configuration = cliParser.parse(cli.arguments());
3c8ea5dcf793 change CLI interface: options: --relation --attribute --record --records --records-from-stdin
František Kučera <franta-hg@frantovo.cz>
parents: 42
diff changeset
    47
		CLICommand command;
3c8ea5dcf793 change CLI interface: options: --relation --attribute --record --records --records-from-stdin
František Kučera <franta-hg@frantovo.cz>
parents: 42
diff changeset
    48
		std::shared_ptr<RelationalWriter> writer(Factory::create(std::cout));
3c8ea5dcf793 change CLI interface: options: --relation --attribute --record --records --records-from-stdin
František Kučera <franta-hg@frantovo.cz>
parents: 42
diff changeset
    49
		command.process(configuration, writer);
3c8ea5dcf793 change CLI interface: options: --relation --attribute --record --records --records-from-stdin
František Kučera <franta-hg@frantovo.cz>
parents: 42
diff changeset
    50
		resultCode = CLI::EXIT_CODE_SUCCESS;
3c8ea5dcf793 change CLI interface: options: --relation --attribute --record --records --records-from-stdin
František Kučera <franta-hg@frantovo.cz>
parents: 42
diff changeset
    51
	} catch (RelpipeWriterException& e) {
50
8f262b05c252 exception: fix typo: getMessge() → getMessage()
František Kučera <franta-hg@frantovo.cz>
parents: 43
diff changeset
    52
		fwprintf(stderr, L"Caught Writer exception: %ls\n", e.getMessage().c_str());
43
3c8ea5dcf793 change CLI interface: options: --relation --attribute --record --records --records-from-stdin
František Kučera <franta-hg@frantovo.cz>
parents: 42
diff changeset
    53
		fwprintf(stderr, L"Debug: Input stream: eof=%ls, lastRead=%d\n", (cin.eof() ? L"true" : L"false"), cin.gcount());
3c8ea5dcf793 change CLI interface: options: --relation --attribute --record --records --records-from-stdin
František Kučera <franta-hg@frantovo.cz>
parents: 42
diff changeset
    54
		resultCode = CLI::EXIT_CODE_DATA_ERROR;
3c8ea5dcf793 change CLI interface: options: --relation --attribute --record --records --records-from-stdin
František Kučera <franta-hg@frantovo.cz>
parents: 42
diff changeset
    55
	} catch (RelpipeCLIException& e) {
50
8f262b05c252 exception: fix typo: getMessge() → getMessage()
František Kučera <franta-hg@frantovo.cz>
parents: 43
diff changeset
    56
		fwprintf(stderr, L"Caught CLI exception: %ls\n", e.getMessage().c_str());
14
cfed80d11caa introduce and use RelpipeCLIException
František Kučera <franta-hg@frantovo.cz>
parents: 13
diff changeset
    57
		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
    58
		resultCode = e.getExitCode();
10
77593735b057 CLI infrastructure ported from the prototype
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    59
	}
77593735b057 CLI infrastructure ported from the prototype
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    60
77593735b057 CLI infrastructure ported from the prototype
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    61
	return resultCode;
77593735b057 CLI infrastructure ported from the prototype
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    62
}