Merge branch 'develop' into main
continuous-integration/drone/push Build is passing Details

This commit is contained in:
tastytea 2019-10-15 15:34:26 +02:00
commit 869fb59be6
Signed by: tastytea
GPG Key ID: CFC39497F1B26E07
5 changed files with 65 additions and 33 deletions

View File

@ -17,10 +17,12 @@
#ifndef REMWHAREAD_URI_HPP #ifndef REMWHAREAD_URI_HPP
#define REMWHAREAD_URI_HPP #define REMWHAREAD_URI_HPP
#include <cstdint>
#include <string> #include <string>
namespace remwharead namespace remwharead
{ {
using std::uint16_t;
using std::string; using std::string;
/*! /*!
@ -162,6 +164,13 @@ protected:
* @since 0.8.5 * @since 0.8.5
*/ */
void set_proxy(); 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 } // namespace remwharead

View File

@ -45,13 +45,9 @@ int App::main(const std::vector<std::string> &args)
{ {
std::locale::global(std::locale("")); // Set locale globally. std::locale::global(std::locale("")); // Set locale globally.
if (_version_requested) if (_exit_requested)
{ {
print_version(); return 0;
}
else if (_help_requested)
{
print_help();
} }
else else
{ {

View File

@ -20,6 +20,7 @@
#include <Poco/Util/HelpFormatter.h> #include <Poco/Util/HelpFormatter.h>
#include <Poco/Util/Option.h> #include <Poco/Util/Option.h>
#include <iostream> #include <iostream>
#include <memory>
using namespace remwharead_cli; using namespace remwharead_cli;
using std::cout; using std::cout;
@ -30,8 +31,7 @@ using Poco::Util::OptionCallback;
using Poco::Util::HelpFormatter; using Poco::Util::HelpFormatter;
App::App() App::App()
: _help_requested(false) : _exit_requested(false)
, _version_requested(false)
, _argument_error(false) , _argument_error(false)
, _format(export_format::undefined) , _format(export_format::undefined)
, _timespan({ time_point(), system_clock::now() }) , _timespan({ time_point(), system_clock::now() })
@ -43,10 +43,11 @@ void App::defineOptions(OptionSet& options)
{ {
options.addOption( options.addOption(
Option("help", "h", "Show this help message.") Option("help", "h", "Show this help message.")
.callback(OptionCallback<App>(this, &App::handle_info))); .argument("option", false)
.callback(OptionCallback<App>(this, &App::handle_options)));
options.addOption( options.addOption(
Option("version", "V", "Print version, copyright and license.") Option("version", "V", "Print version, copyright and license.")
.callback(OptionCallback<App>(this, &App::handle_info))); .callback(OptionCallback<App>(this, &App::handle_options)));
options.addOption( options.addOption(
Option("tags", "t", "Add tags to URI, delimited by commas.") Option("tags", "t", "Add tags to URI, delimited by commas.")
.argument("tags") .argument("tags")
@ -82,23 +83,21 @@ void App::defineOptions(OptionSet& options)
.callback(OptionCallback<App>(this, &App::handle_options))); .callback(OptionCallback<App>(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") if (name == "help")
{ {
_help_requested = true; _exit_requested = true;
print_help(value);
stopOptionsProcessing();
} }
else if (name == "version") else if (name == "version")
{ {
_version_requested = true; _exit_requested = true;
print_version();
stopOptionsProcessing();
} }
else if (name == "tags")
stopOptionsProcessing();
}
void App::handle_options(const std::string &name, const std::string &value)
{
if (name == "tags")
{ {
size_t pos_end = 0; size_t pos_end = 0;
size_t pos_start = 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()); std::unique_ptr<HelpFormatter> helpFormatter;
helpFormatter.setCommand(commandName()); OptionSet oneoption;
helpFormatter.setUsage("[-t tags] [-N] URI\n"
"-e format [-f file] [-T start,end] " if (option.empty())
"[[-s|-S] expression] [-r]"); {
helpFormatter.format(cout); helpFormatter = std::make_unique<HelpFormatter>(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<HelpFormatter>(oneoption);
}
helpFormatter->format(cout);
} }
void App::print_version() void App::print_version()

View File

@ -42,15 +42,13 @@ public:
protected: protected:
void defineOptions(OptionSet& options) override; void defineOptions(OptionSet& options) override;
void handle_info(const string &name, const string &);
void handle_options(const string &name, const string &value); void handle_options(const string &name, const string &value);
void print_help(); void print_help(const string &option);
void print_version(); void print_version();
int main(const std::vector<string> &args) override; int main(const std::vector<string> &args) override;
private: private:
bool _help_requested; bool _exit_requested;
bool _version_requested;
bool _argument_error; bool _argument_error;
string _uri; string _uri;
vector<string> _tags; vector<string> _tags;

View File

@ -29,6 +29,7 @@
#include <cstdint> #include <cstdint>
#include <exception> #include <exception>
#include <iostream> #include <iostream>
#include <iterator>
#include <locale> #include <locale>
#include <sstream> #include <sstream>
#include <utility> #include <utility>
@ -171,6 +172,7 @@ string URI::make_request(const string &uri, bool archive) const
} }
else else
{ {
// NOLINTNEXTLINE(cert-err60-cpp)
throw Poco::Exception("Protocol not supported."); throw Poco::Exception("Protocol not supported.");
} }
@ -214,7 +216,7 @@ string URI::make_request(const string &uri, bool archive) const
} }
default: default:
{ {
throw Poco::Exception(response.getReason()); throw Poco::Exception(response.getReason()); // NOLINT(cert-err60-cpp)
return ""; return "";
} }
} }
@ -248,7 +250,7 @@ string URI::extract_description(const string &html)
re_desc.split(html, matches); re_desc.split(html, matches);
if (matches.size() >= 2) if (matches.size() >= 2)
{ {
return remove_newlines(unescape_html(matches[1])); return remove_newlines(cut_text(unescape_html(matches[1]), 500));
} }
} }
@ -655,4 +657,20 @@ string URI::remove_newlines(string text)
return 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 } // namespace remwharead