# HG changeset patch # User František Kučera # Date 1563976422 -7200 # Node ID 7d6ac51c0d48e2ed3d566ce49e03400820b21f20 # Parent a37196931f6345e77b3d9b6b0352da34ba0b696c configuration and CLI parser diff -r a37196931f63 -r 7d6ac51c0d48 src/CLIParser.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/CLIParser.h Wed Jul 24 15:53:42 2019 +0200 @@ -0,0 +1,101 @@ +/** + * Relational pipes + * Copyright © 2019 František Kučera (Frantovo.cz, GlobalCode.info) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#pragma once + +#include + +#include +#include +#include + +#include "Configuration.h" + +namespace relpipe { +namespace in { +namespace xmltable { + +class CLIParser { +private: + + string_t readNext(std::vector arguments, int& i) { + if (i < arguments.size()) return arguments[i++]; + else throw relpipe::cli::RelpipeCLIException(L"Missing CLI argument" + (i > 0 ? (L" after " + arguments[i - 1]) : L""), relpipe::cli::CLI::EXIT_CODE_BAD_CLI_ARGUMENTS); + } + + void addRelation(Configuration& c, RelationConfiguration& currentRelation) { + if (currentRelation.relation.size()) { + c.relationConfigurations.push_back(currentRelation); + currentRelation = RelationConfiguration(); + } + } + + relpipe::writer::TypeId parseTypeId(const string_t& value) { + using t = relpipe::writer::TypeId; + if (value == L"string") return t::STRING; + else if (value == L"integer") return t::INTEGER; + else if (value == L"boolean") return t::BOOLEAN; + else throw relpipe::cli::RelpipeCLIException(L"Unable to parse TypeId: " + value, relpipe::cli::CLI::EXIT_CODE_BAD_CLI_ARGUMENTS); + } + +public: + + static const string_t OPTION_NAMESPACE; + static const string_t OPTION_RELATION; + static const string_t OPTION_RECORDS; + static const string_t OPTION_ATTRIBUTE; + + Configuration parse(const std::vector& arguments) { + Configuration c; + RelationConfiguration currentRelation; + + for (int i = 0; i < arguments.size();) { + string_t option = readNext(arguments, i); + + if (option == OPTION_NAMESPACE) { + c.namespaceMappings.push_back(readNext(arguments, i)); + c.namespaceMappings.push_back(readNext(arguments, i)); + } else if (option == OPTION_RELATION) { + addRelation(c, currentRelation); // previous relation + currentRelation.relation = readNext(arguments, i); + } else if (option == OPTION_RECORDS) { + currentRelation.xpath = readNext(arguments, i); + } else if (option == OPTION_ATTRIBUTE) { + AttributeRecipe attribute; + attribute.name = readNext(arguments, i); + attribute.type = parseTypeId(readNext(arguments, i)); + attribute.xpath = readNext(arguments, i); + currentRelation.attributes.push_back(attribute); + } else throw relpipe::cli::RelpipeCLIException(L"Unsupported CLI option: " + option, relpipe::cli::CLI::EXIT_CODE_BAD_CLI_ARGUMENTS); + } + addRelation(c, currentRelation); // last relation + + return c; + } + + virtual ~CLIParser() { + } +}; + +const string_t CLIParser::OPTION_NAMESPACE = L"--namespace"; +const string_t CLIParser::OPTION_RELATION = L"--relation"; +const string_t CLIParser::OPTION_RECORDS = L"--records"; +const string_t CLIParser::OPTION_ATTRIBUTE = L"--attribute"; + +} +} +} diff -r a37196931f63 -r 7d6ac51c0d48 src/Configuration.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/Configuration.h Wed Jul 24 15:53:42 2019 +0200 @@ -0,0 +1,63 @@ +/** + * Relational pipes + * Copyright © 2019 František Kučera (Frantovo.cz, GlobalCode.info) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#pragma once + +#include + +#include + + +namespace relpipe { +namespace in { +namespace xmltable { + +class AttributeRecipe { +public: + + virtual ~AttributeRecipe() { + } + + relpipe::writer::string_t name; + relpipe::writer::TypeId type; + relpipe::writer::string_t xpath; +}; + +class RelationConfiguration { +public: + + virtual ~RelationConfiguration() { + } + + relpipe::writer::string_t relation; + relpipe::writer::string_t xpath; + std::vector attributes; + +}; + +class Configuration { +public: + std::vector relationConfigurations; + std::vector namespaceMappings; + + virtual ~Configuration() { + } +}; + +} +} +} \ No newline at end of file diff -r a37196931f63 -r 7d6ac51c0d48 src/relpipe-in-xmltable.cpp --- a/src/relpipe-in-xmltable.cpp Wed Jul 24 12:15:10 2019 +0200 +++ b/src/relpipe-in-xmltable.cpp Wed Jul 24 15:53:42 2019 +0200 @@ -32,6 +32,8 @@ #include #include "XMLTableCommand.h" +#include "CLIParser.h" +#include "Configuration.h" using namespace relpipe::cli; using namespace relpipe::writer; @@ -45,6 +47,8 @@ int resultCode = CLI::EXIT_CODE_UNEXPECTED_ERROR; try { + CLIParser cliParser; + Configuration configuration = cliParser.parse(cli.arguments()); XMLCommand command; command.process(cin, cout); resultCode = CLI::EXIT_CODE_SUCCESS; @@ -52,6 +56,10 @@ fwprintf(stderr, L"Caught Writer 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; + } catch (RelpipeCLIException& e) { + fwprintf(stderr, L"Caught CLI 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 = e.getExitCode(); } // TODO: catch xmlpp::exception return resultCode;