# HG changeset patch # User František Kučera # Date 1532758415 -7200 # Node ID 77593735b057b0d9fd179b5352f0dc74c3e2a27f # Parent 3be327f67cdd77401401dc08b10a82f89b6637ca CLI infrastructure ported from the prototype diff -r 3be327f67cdd -r 77593735b057 CLI.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/CLI.h Sat Jul 28 08:13:35 2018 +0200 @@ -0,0 +1,61 @@ +#pragma once + +#include +#include +#include +#include +#include +#include + +using namespace std; + +namespace relpipe { +namespace cli { + +class CLI { +public: + + CLI(int argc, char* argv[]) { + setlocale(LC_ALL, ""); + + this->argc = &argc; + this->argv = &argv; + + program = convertor.from_bytes(argv[0]); + + for (int i = 1; i < argc; i++) { + args.insert(args.end(), convertor.from_bytes(argv[i])); + } + + } + + CLI(const CLI& orig) { + } + + virtual ~CLI() { + } + + const wstring programName() { + return (const wstring) program; + } + + const vector arguments() { + return (const vector)args; + } + + static const int EXIT_CODE_SUCCESS = 0; + static const int EXIT_CODE_UNEXPECTED_ERROR = 1; + static const int EXIT_CODE_BAD_SYNTAX = 3; + static const int EXIT_CODE_UNKNOWN_COMMAND = 4; + static const int EXIT_CODE_DATA_ERROR = 5; + +private: + int* argc; + char*** argv; + wstring program; + vector args; + wstring_convert> convertor; // TODO: support also other encodings. +}; + +} +} diff -r 3be327f67cdd -r 77593735b057 nbproject/configurations.xml --- a/nbproject/configurations.xml Fri Jul 27 00:07:16 2018 +0200 +++ b/nbproject/configurations.xml Sat Jul 28 08:13:35 2018 +0200 @@ -4,6 +4,7 @@ + CLI.h + + @@ -79,6 +82,8 @@ + + diff -r 3be327f67cdd -r 77593735b057 nbproject/project.xml --- a/nbproject/project.xml Fri Jul 27 00:07:16 2018 +0200 +++ b/nbproject/project.xml Sat Jul 28 08:13:35 2018 +0200 @@ -6,7 +6,7 @@ relpipe-in-cli.cpp cpp - + h UTF-8 diff -r 3be327f67cdd -r 77593735b057 relpipe-in-cli.cpp --- a/relpipe-in-cli.cpp Fri Jul 27 00:07:16 2018 +0200 +++ b/relpipe-in-cli.cpp Sat Jul 28 08:13:35 2018 +0200 @@ -4,10 +4,56 @@ #include #include +#include #include #include +#include "CLI.h" + +int demo(); + int main(int argc, char** argv) { + using namespace relpipe::cli; + using namespace relpipe::writer; + + setlocale(LC_ALL, ""); + CLI cli(argc, argv); + + int resultCode = CLI::EXIT_CODE_UNEXPECTED_ERROR; + + try { + if (cli.arguments().size() > 0) { + + const wstring action = cli.arguments()[0]; + vector arguments(cli.arguments().size() - 1); + for (int i = 1; i < cli.arguments().size(); i++) { + arguments[i - 1] = cli.arguments()[i]; + } + + if (action == L"demo") { + resultCode = demo(); + } else { + fwprintf(stderr, L"Unknown command: %ls\n", action.c_str()); + resultCode = CLI::EXIT_CODE_UNKNOWN_COMMAND; + } + + + } else { + fwprintf(stderr, L"Missing command…\n"); + resultCode = CLI::EXIT_CODE_BAD_SYNTAX; + } + } catch (RelpipeWriterException e) { + fwprintf(stderr, L"Caught exception: %ls\n", e.getMessge().c_str()); + fwprintf(stderr, L"Debug: Input stream: eof=%ls, lastRead=%d\n", (cin.eof() ? L"true" : L"false"), cin.gcount()); + resultCode = CLI::EXIT_CODE_DATA_ERROR; + } + + + return resultCode; + +} + +int demo() { using namespace relpipe::writer; std::shared_ptr writer(Factory::create(std::cout));