# HG changeset patch # User František Kučera # Date 1600874259 -7200 # Node ID a7596589a5b06263f0cbf240c82f3604f5b6aa27 # Parent e4118c36bef698f3bf3b17b94394867aa53613f8 change CLI interface: options: --write-header diff -r e4118c36bef6 -r a7596589a5b0 bash-completion.sh --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/bash-completion.sh Wed Sep 23 17:17:39 2020 +0200 @@ -0,0 +1,39 @@ +# 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, version 3 of the License. +# +# 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 . + +_relpipe_out_csv_completion() { + local w0 w1 w2 w3 + + COMPREPLY=() + w0=${COMP_WORDS[COMP_CWORD]} + w1=${COMP_WORDS[COMP_CWORD-1]} + w2=${COMP_WORDS[COMP_CWORD-2]} + w3=${COMP_WORDS[COMP_CWORD-3]} + + WRITE_HEADER=( + "true" + "false" + ) + + if [[ "$w1" == "--write-header" ]]; then COMPREPLY=($(compgen -W "${WRITE_HEADER[*]}" -- "$w0")) + else + OPTIONS=( + "--write-header" + ) + COMPREPLY=($(compgen -W "${OPTIONS[*]}" -- "$w0")) + fi +} + +complete -F _relpipe_out_csv_completion relpipe-out-csv diff -r e4118c36bef6 -r a7596589a5b0 src/CLIParser.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/CLIParser.h Wed Sep 23 17:17:39 2020 +0200 @@ -0,0 +1,75 @@ +/** + * Relational pipes + * Copyright © 2020 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, version 3 of the License. + * + * 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 + +#include "Configuration.h" + +namespace relpipe { +namespace out { +namespace csv { + +class CLIParser { +private: + + relpipe::reader::string_t readNext(const 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); + } + + /** + * TODO: use a common method + */ + bool parseBoolean(const relpipe::reader::string_t& value) { + if (value == L"true") return true; + else if (value == L"false") return false; + else throw relpipe::cli::RelpipeCLIException(L"Unable to parse boolean value: " + value + L" (expecting true or false)", relpipe::cli::CLI::EXIT_CODE_BAD_CLI_ARGUMENTS); + } + +public: + + static const relpipe::reader::string_t OPTION_WRITE_HEADER; + + Configuration parse(const std::vector& arguments) { + Configuration c; + + for (int i = 0; i < arguments.size();) { + relpipe::reader::string_t option = readNext(arguments, i); + + if (option == OPTION_WRITE_HEADER) { + c.writeHeader = parseBoolean(readNext(arguments, i)); + } else throw relpipe::cli::RelpipeCLIException(L"Unsupported CLI option: " + option, relpipe::cli::CLI::EXIT_CODE_BAD_CLI_ARGUMENTS); + } + + return c; + } + + virtual ~CLIParser() { + } +}; + +const relpipe::reader::string_t CLIParser::OPTION_WRITE_HEADER = L"--write-header"; + +} +} +} diff -r e4118c36bef6 -r a7596589a5b0 src/CSVHandler.h --- a/src/CSVHandler.h Sat Jun 06 01:50:44 2020 +0200 +++ b/src/CSVHandler.h Wed Sep 23 17:17:39 2020 +0200 @@ -29,6 +29,7 @@ #include #include +#include "Configuration.h" #include "RelpipeCSVWriterException.h" namespace relpipe { @@ -42,19 +43,20 @@ class CSVHandler : public RelationalReaderStringHandler { private: std::ostream& output; + Configuration& configuration; const char QUOTE = '"'; std::wstring_convert> convertor; // TODO: local system encoding or generate CSV always in UTF-8 like XML? std::vector firstAttributes; integer_t valueCount = 0; public: - CSVHandler(std::ostream& output) : output(output) { + CSVHandler(std::ostream& output, Configuration& configuration) : output(output), configuration(configuration) { } void startRelation(string_t name, std::vector attributes) override { if (firstAttributes.empty()) { firstAttributes = attributes; - for (auto attr : attributes) attribute(attr.getAttributeName()); + if (configuration.writeHeader) for (auto attr : attributes) attribute(attr.getAttributeName()); } else { // TODO: UNION ALL if data types and attribute count matches throw RelpipeCSVWriterException(L"Only a single relation can be converted to the CSV format."); diff -r e4118c36bef6 -r a7596589a5b0 src/Configuration.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/Configuration.h Wed Sep 23 17:17:39 2020 +0200 @@ -0,0 +1,39 @@ +/** + * Relational pipes + * Copyright © 2020 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, version 3 of the License. + * + * 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 + + +namespace relpipe { +namespace out { +namespace csv { + +class Configuration { +public: + relpipe::reader::boolean_t writeHeader = true; + + virtual ~Configuration() { + } +}; + +} +} +} \ No newline at end of file diff -r e4118c36bef6 -r a7596589a5b0 src/relpipe-out-csv.cpp --- a/src/relpipe-out-csv.cpp Sat Jun 06 01:50:44 2020 +0200 +++ b/src/relpipe-out-csv.cpp Wed Sep 23 17:17:39 2020 +0200 @@ -25,6 +25,8 @@ #include #include +#include "Configuration.h" +#include "CLIParser.h" #include "CSVHandler.h" #include "RelpipeCSVWriterException.h" @@ -39,8 +41,10 @@ int resultCode = CLI::EXIT_CODE_UNEXPECTED_ERROR; try { + CLIParser cliParser; + Configuration configuration = cliParser.parse(cli.arguments()); std::shared_ptr reader(Factory::create(std::cin)); - CSVHandler handler(std::cout); + CSVHandler handler(std::cout, configuration); reader->addHandler(&handler); reader->process();