From 43d9e3947acc5765381d4cda293dad76edd3b0e1 Mon Sep 17 00:00:00 2001 From: tastytea Date: Tue, 8 Oct 2019 18:03:27 +0200 Subject: [PATCH 1/3] Disable clang-tidy warnings I can't do anything about. --- src/lib/uri.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/lib/uri.cpp b/src/lib/uri.cpp index 4be5adf..02db23b 100644 --- a/src/lib/uri.cpp +++ b/src/lib/uri.cpp @@ -171,6 +171,7 @@ string URI::make_request(const string &uri, bool archive) const } else { + // NOLINTNEXTLINE(cert-err60-cpp) throw Poco::Exception("Protocol not supported."); } @@ -214,7 +215,7 @@ string URI::make_request(const string &uri, bool archive) const } default: { - throw Poco::Exception(response.getReason()); + throw Poco::Exception(response.getReason()); // NOLINT(cert-err60-cpp) return ""; } } From 2b9768a5a934b83b6af0cb110cd39be4c1a2d6eb Mon Sep 17 00:00:00 2001 From: tastytea Date: Thu, 10 Oct 2019 14:36:59 +0200 Subject: [PATCH 2/3] Provide help for specific arguments (--help=option). --- src/cli/main.cpp | 8 ++---- src/cli/parse_options.cpp | 53 +++++++++++++++++++++++--------------- src/cli/remwharead_cli.hpp | 6 ++--- 3 files changed, 36 insertions(+), 31 deletions(-) diff --git a/src/cli/main.cpp b/src/cli/main.cpp index d9bac0f..e95600d 100644 --- a/src/cli/main.cpp +++ b/src/cli/main.cpp @@ -45,13 +45,9 @@ int App::main(const std::vector &args) { std::locale::global(std::locale("")); // Set locale globally. - if (_version_requested) + if (_exit_requested) { - print_version(); - } - else if (_help_requested) - { - print_help(); + return 0; } else { diff --git a/src/cli/parse_options.cpp b/src/cli/parse_options.cpp index 6c0b032..5157294 100644 --- a/src/cli/parse_options.cpp +++ b/src/cli/parse_options.cpp @@ -20,6 +20,7 @@ #include #include #include +#include using namespace remwharead_cli; using std::cout; @@ -30,8 +31,7 @@ using Poco::Util::OptionCallback; using Poco::Util::HelpFormatter; App::App() - : _help_requested(false) - , _version_requested(false) + : _exit_requested(false) , _argument_error(false) , _format(export_format::undefined) , _timespan({ time_point(), system_clock::now() }) @@ -43,10 +43,11 @@ void App::defineOptions(OptionSet& options) { options.addOption( Option("help", "h", "Show this help message.") - .callback(OptionCallback(this, &App::handle_info))); + .argument("option", false) + .callback(OptionCallback(this, &App::handle_options))); options.addOption( Option("version", "V", "Print version, copyright and license.") - .callback(OptionCallback(this, &App::handle_info))); + .callback(OptionCallback(this, &App::handle_options))); options.addOption( Option("tags", "t", "Add tags to URI, delimited by commas.") .argument("tags") @@ -82,23 +83,21 @@ void App::defineOptions(OptionSet& options) .callback(OptionCallback(this, &App::handle_options))); } -void App::handle_info(const std::string &name, const std::string &) +void App::handle_options(const std::string &name, const std::string &value) { if (name == "help") { - _help_requested = true; + _exit_requested = true; + print_help(value); + stopOptionsProcessing(); } else if (name == "version") { - _version_requested = true; + _exit_requested = true; + print_version(); + stopOptionsProcessing(); } - - stopOptionsProcessing(); -} - -void App::handle_options(const std::string &name, const std::string &value) -{ - if (name == "tags") + else if (name == "tags") { size_t pos_end = 0; size_t pos_start = 0; @@ -193,14 +192,26 @@ void App::handle_options(const std::string &name, const std::string &value) } } -void App::print_help() +void App::print_help(const string &option) { - HelpFormatter helpFormatter(options()); - helpFormatter.setCommand(commandName()); - helpFormatter.setUsage("[-t tags] [-N] URI\n" - "-e format [-f file] [-T start,end] " - "[[-s|-S] expression] [-r]"); - helpFormatter.format(cout); + std::unique_ptr helpFormatter; + OptionSet oneoption; + + if (option.empty()) + { + helpFormatter = std::make_unique(options()); + helpFormatter->setCommand(commandName()); + helpFormatter->setUsage("[-t tags] [-N] URI\n" + "-e format [-f file] [-T start,end] " + "[[-s|-S] expression] [-r]"); + } + else + { + oneoption.addOption(options().getOption(option)); + helpFormatter = std::make_unique(oneoption); + } + + helpFormatter->format(cout); } void App::print_version() diff --git a/src/cli/remwharead_cli.hpp b/src/cli/remwharead_cli.hpp index e0a014b..30acecd 100644 --- a/src/cli/remwharead_cli.hpp +++ b/src/cli/remwharead_cli.hpp @@ -42,15 +42,13 @@ public: protected: void defineOptions(OptionSet& options) override; - void handle_info(const string &name, const string &); void handle_options(const string &name, const string &value); - void print_help(); + void print_help(const string &option); void print_version(); int main(const std::vector &args) override; private: - bool _help_requested; - bool _version_requested; + bool _exit_requested; bool _argument_error; string _uri; vector _tags; From d8102b017e8913448fd31f36bce039957511abbb Mon Sep 17 00:00:00 2001 From: tastytea Date: Tue, 15 Oct 2019 15:10:39 +0200 Subject: [PATCH 3/3] Cut descriptions at 500 characters. --- include/uri.hpp | 9 +++++++++ src/lib/uri.cpp | 19 ++++++++++++++++++- 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/include/uri.hpp b/include/uri.hpp index 6864345..90cb18b 100644 --- a/include/uri.hpp +++ b/include/uri.hpp @@ -17,10 +17,12 @@ #ifndef REMWHAREAD_URI_HPP #define REMWHAREAD_URI_HPP +#include #include namespace remwharead { +using std::uint16_t; using std::string; /*! @@ -162,6 +164,13 @@ protected: * @since 0.8.5 */ void set_proxy(); + + /*! + * @brief Limits text to N characters, cuts at space. + * + * @since 0.8.5 + */ + string cut_text(const string &text, const uint16_t n_chars) const; }; } // namespace remwharead diff --git a/src/lib/uri.cpp b/src/lib/uri.cpp index 02db23b..bb2cf18 100644 --- a/src/lib/uri.cpp +++ b/src/lib/uri.cpp @@ -29,6 +29,7 @@ #include #include #include +#include #include #include #include @@ -249,7 +250,7 @@ string URI::extract_description(const string &html) re_desc.split(html, matches); if (matches.size() >= 2) { - return remove_newlines(unescape_html(matches[1])); + return remove_newlines(cut_text(unescape_html(matches[1]), 500)); } } @@ -656,4 +657,20 @@ string URI::remove_newlines(string text) return text; } + +string URI::cut_text(const string &text, const uint16_t n_chars) const +{ + if (text.size() > n_chars) + { + constexpr char suffix[] = " […]"; + constexpr auto suffix_len = std::end(suffix) - std::begin(suffix) - 1; + + const size_t pos = text.rfind(' ', n_chars - suffix_len); + + return text.substr(0, pos) + suffix; + } + + return text; +} + } // namespace remwharead