diff options
author | Rosen Penev <rosenp@gmail.com> | 2020-05-12 07:59:47 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-05-12 07:59:47 +0300 |
commit | 12bc8d78e778b65bec357760bbe26b734abdf0e7 (patch) | |
tree | 4f2fa3a880ec19b1c4194fbaa983506ad46de24b | |
parent | b0f67a06de3446aa97a4943ad0ad6086460b2b61 (diff) |
clang-tidy fixes (#231)
-rw-r--r-- | include/cxxopts.hpp | 237 |
1 files changed, 108 insertions, 129 deletions
diff --git a/include/cxxopts.hpp b/include/cxxopts.hpp index 1644692..3dd5f52 100644 --- a/include/cxxopts.hpp +++ b/include/cxxopts.hpp @@ -25,8 +25,8 @@ THE SOFTWARE. #ifndef CXXOPTS_HPP_INCLUDED #define CXXOPTS_HPP_INCLUDED -#include <cstring> #include <cctype> +#include <cstring> #include <exception> #include <iostream> #include <limits> @@ -37,6 +37,7 @@ THE SOFTWARE. #include <string> #include <unordered_map> #include <unordered_set> +#include <utility> #include <vector> #ifdef __cpp_lib_optional @@ -61,7 +62,7 @@ namespace cxxopts CXXOPTS__VERSION_MINOR, CXXOPTS__VERSION_PATCH }; -} +} // namespace cxxopts //when we ask cxxopts to use Unicode, help strings are processed using ICU, //which results in the correct lengths being computed for strings when they @@ -227,9 +228,9 @@ namespace cxxopts inline String& - stringAppend(String&s, String a) + stringAppend(String&s, const String& a) { - return s.append(std::move(a)); + return s.append(a); } inline @@ -259,7 +260,7 @@ namespace cxxopts { return s.empty(); } -} +} // namespace cxxopts //ifdef CXXOPTS_USE_UNICODE #endif @@ -275,7 +276,7 @@ namespace cxxopts const std::string LQUOTE("‘"); const std::string RQUOTE("’"); #endif - } + } // namespace class Value : public std::enable_shared_from_this<Value> { @@ -324,13 +325,13 @@ namespace cxxopts class OptionException : public std::exception { public: - OptionException(const std::string& message) - : m_message(message) + explicit OptionException(std::string message) + : m_message(std::move(message)) { } - virtual const char* - what() const noexcept + const char* + what() const noexcept override { return m_message.c_str(); } @@ -343,7 +344,7 @@ namespace cxxopts { public: - OptionSpecException(const std::string& message) + explicit OptionSpecException(const std::string& message) : OptionException(message) { } @@ -352,7 +353,7 @@ namespace cxxopts class OptionParseException : public OptionException { public: - OptionParseException(const std::string& message) + explicit OptionParseException(const std::string& message) : OptionException(message) { } @@ -361,7 +362,7 @@ namespace cxxopts class option_exists_error : public OptionSpecException { public: - option_exists_error(const std::string& option) + explicit option_exists_error(const std::string& option) : OptionSpecException("Option " + LQUOTE + option + RQUOTE + " already exists") { } @@ -370,7 +371,7 @@ namespace cxxopts class invalid_option_format_error : public OptionSpecException { public: - invalid_option_format_error(const std::string& format) + explicit invalid_option_format_error(const std::string& format) : OptionSpecException("Invalid option format " + LQUOTE + format + RQUOTE) { } @@ -378,7 +379,7 @@ namespace cxxopts class option_syntax_exception : public OptionParseException { public: - option_syntax_exception(const std::string& text) + explicit option_syntax_exception(const std::string& text) : OptionParseException("Argument " + LQUOTE + text + RQUOTE + " starts with a - but has incorrect syntax") { @@ -388,7 +389,7 @@ namespace cxxopts class option_not_exists_exception : public OptionParseException { public: - option_not_exists_exception(const std::string& option) + explicit option_not_exists_exception(const std::string& option) : OptionParseException("Option " + LQUOTE + option + RQUOTE + " does not exist") { } @@ -397,7 +398,7 @@ namespace cxxopts class missing_argument_exception : public OptionParseException { public: - missing_argument_exception(const std::string& option) + explicit missing_argument_exception(const std::string& option) : OptionParseException( "Option " + LQUOTE + option + RQUOTE + " is missing an argument" ) @@ -408,7 +409,7 @@ namespace cxxopts class option_requires_argument_exception : public OptionParseException { public: - option_requires_argument_exception(const std::string& option) + explicit option_requires_argument_exception(const std::string& option) : OptionParseException( "Option " + LQUOTE + option + RQUOTE + " requires an argument" ) @@ -436,7 +437,7 @@ namespace cxxopts class option_not_present_exception : public OptionParseException { public: - option_not_present_exception(const std::string& option) + explicit option_not_present_exception(const std::string& option) : OptionParseException("Option " + LQUOTE + option + RQUOTE + " not present") { } @@ -445,7 +446,7 @@ namespace cxxopts class argument_incorrect_type : public OptionParseException { public: - argument_incorrect_type + explicit argument_incorrect_type ( const std::string& arg ) @@ -459,7 +460,7 @@ namespace cxxopts class option_required_exception : public OptionParseException { public: - option_required_exception(const std::string& option) + explicit option_required_exception(const std::string& option) : OptionParseException( "Option " + LQUOTE + option + RQUOTE + " is required but not present" ) @@ -497,7 +498,7 @@ namespace cxxopts ("(t|T)(rue)?|1"); std::basic_regex<char> falsy_pattern ("(f|F)(alse)?|0"); - } + } // namespace namespace detail { @@ -542,7 +543,7 @@ namespace cxxopts { SignedCheck<T, std::numeric_limits<T>::is_signed>()(negative, value, text); } - } + } // namespace detail template <typename R, typename T> R @@ -745,7 +746,7 @@ namespace cxxopts { std::stringstream in(text); std::string token; - while(in.eof() == false && std::getline(in, token, CXXOPTS_VECTOR_DELIMITER)) { + while(!in.eof() && std::getline(in, token, CXXOPTS_VECTOR_DELIMITER)) { T v; parse_value(token, v); value.emplace_back(std::move(v)); @@ -798,12 +799,12 @@ namespace cxxopts { } - abstract_value(T* t) + explicit abstract_value(T* t) : m_store(t) { } - virtual ~abstract_value() = default; + ~abstract_value() override = default; abstract_value(const abstract_value& rhs) { @@ -824,37 +825,37 @@ namespace cxxopts } void - parse(const std::string& text) const + parse(const std::string& text) const override { parse_value(text, *m_store); } bool - is_container() const + is_container() const override { return type_is_container<T>::value; } void - parse() const + parse() const override { parse_value(m_default_value, *m_store); } bool - has_default() const + has_default() const override { return m_default; } bool - has_implicit() const + has_implicit() const override { return m_implicit; } std::shared_ptr<Value> - default_value(const std::string& value) + default_value(const std::string& value) override { m_default = true; m_default_value = value; @@ -862,7 +863,7 @@ namespace cxxopts } std::shared_ptr<Value> - implicit_value(const std::string& value) + implicit_value(const std::string& value) override { m_implicit = true; m_implicit_value = value; @@ -870,26 +871,26 @@ namespace cxxopts } std::shared_ptr<Value> - no_implicit_value() + no_implicit_value() override { m_implicit = false; return shared_from_this(); } std::string - get_default_value() const + get_default_value() const override { return m_default_value; } std::string - get_implicit_value() const + get_implicit_value() const override { return m_implicit_value; } bool - is_boolean() const + is_boolean() const override { return std::is_same<T, bool>::value; } @@ -901,10 +902,7 @@ namespace cxxopts { return *m_result; } - else - { - return *m_store; - } + return *m_store; } protected: @@ -935,21 +933,21 @@ namespace cxxopts class standard_value<bool> : public abstract_value<bool> { public: - ~standard_value() = default; + ~standard_value() override = default; standard_value() { set_default_and_implicit(); } - standard_value(bool* b) + explicit standard_value(bool* b) : abstract_value(b) { set_default_and_implicit(); } std::shared_ptr<Value> - clone() const + clone() const override { return std::make_shared<standard_value<bool>>(*this); } @@ -965,7 +963,7 @@ namespace cxxopts m_implicit_value = "true"; } }; - } + } // namespace values template <typename T> std::shared_ptr<Value> @@ -988,15 +986,15 @@ namespace cxxopts public: OptionDetails ( - const std::string& short_, - const std::string& long_, - const String& desc, + std::string short_, + std::string long_, + String desc, std::shared_ptr<const Value> val ) - : m_short(short_) - , m_long(long_) - , m_desc(desc) - , m_value(val) + : m_short(std::move(short_)) + , m_long(std::move(long_)) + , m_desc(std::move(desc)) + , m_value(std::move(val)) , m_count(0) { } @@ -1073,7 +1071,7 @@ namespace cxxopts void parse ( - std::shared_ptr<const OptionDetails> details, + const std::shared_ptr<const OptionDetails>& details, const std::string& text ) { @@ -1083,7 +1081,7 @@ namespace cxxopts } void - parse_default(std::shared_ptr<const OptionDetails> details) + parse_default(const std::shared_ptr<const OptionDetails>& details) { ensure_value(details); m_default = true; @@ -1120,7 +1118,7 @@ namespace cxxopts private: void - ensure_value(std::shared_ptr<const OptionDetails> details) + ensure_value(const std::shared_ptr<const OptionDetails>& details) { if (m_value == nullptr) { @@ -1175,7 +1173,7 @@ namespace cxxopts public: ParseResult( - const std::shared_ptr< + std::shared_ptr< std::unordered_map<std::string, std::shared_ptr<OptionDetails>> >, std::vector<std::string>, @@ -1226,18 +1224,18 @@ namespace cxxopts add_to_option(const std::string& option, const std::string& arg); bool - consume_positional(std::string a); + consume_positional(const std::string& a); void parse_option ( - std::shared_ptr<OptionDetails> value, + const std::shared_ptr<OptionDetails>& value, const std::string& name, const std::string& arg = "" ); void - parse_default(std::shared_ptr<OptionDetails> details); + parse_default(const std::shared_ptr<OptionDetails>& details); void checked_parse_arg @@ -1245,7 +1243,7 @@ namespace cxxopts int argc, char* argv[], int& current, - std::shared_ptr<OptionDetails> value, + const std::shared_ptr<OptionDetails>& value, const std::string& name ); @@ -1266,15 +1264,15 @@ namespace cxxopts { Option ( - const std::string& opts, - const std::string& desc, - const std::shared_ptr<const Value>& value = ::cxxopts::value<bool>(), - const std::string& arg_help = "" + std::string opts, + std::string desc, + std::shared_ptr<const Value> value = ::cxxopts::value<bool>(), + std::string arg_help = "" ) - : opts_(opts) - , desc_(desc) - , value_(value) - , arg_help_(arg_help) + : opts_(std::move(opts)) + , desc_(std::move(desc)) + , value_(std::move(value)) + , arg_help_(std::move(arg_help)) { } @@ -1286,11 +1284,10 @@ namespace cxxopts class Options { - typedef std::unordered_map<std::string, std::shared_ptr<OptionDetails>> - OptionMap; + using OptionMap = std::unordered_map<std::string, std::shared_ptr<OptionDetails>>; public: - Options(std::string program, std::string help_string = "") + explicit Options(std::string program, std::string help_string = "") : m_program(std::move(program)) , m_help_string(toLocalString(std::move(help_string))) , m_custom_help("[OPTION...]") @@ -1357,7 +1354,7 @@ namespace cxxopts const std::string& s, const std::string& l, std::string desc, - std::shared_ptr<const Value> value, + const std::shared_ptr<const Value>& value, std::string arg_help ); @@ -1380,7 +1377,7 @@ namespace cxxopts std::string help(const std::vector<std::string>& groups = {}) const; - const std::vector<std::string> + std::vector<std::string> groups() const; const HelpGroupDetails& @@ -1392,7 +1389,7 @@ namespace cxxopts add_one_option ( const std::string& option, - std::shared_ptr<OptionDetails> details + const std::shared_ptr<OptionDetails>& details ); String @@ -1438,7 +1435,7 @@ namespace cxxopts ( const std::string& opts, const std::string& desc, - std::shared_ptr<const Value> value + const std::shared_ptr<const Value>& value = ::cxxopts::value<bool>(), std::string arg_help = "" ); @@ -1465,12 +1462,12 @@ namespace cxxopts const HelpOptionDetails& o ) { - auto& s = o.s; - auto& l = o.l; + const auto& s = o.s; + const auto& l = o.l; String result = " "; - if (s.size() > 0) + if (!s.empty()) { result += "-" + toLocalString(s) + ","; } @@ -1479,12 +1476,12 @@ namespace cxxopts result += " "; } - if (l.size() > 0) + if (!l.empty()) { result += " --" + toLocalString(l); } - auto arg = o.arg_help.size() > 0 ? toLocalString(o.arg_help) : "arg"; + auto arg = !o.arg_help.empty() ? toLocalString(o.arg_help) : "arg"; if (!o.is_boolean) { @@ -1513,7 +1510,7 @@ namespace cxxopts if (o.has_default && (!o.is_boolean || o.default_value != "false")) { - if(o.default_value != "") + if(!o.default_value.empty()) { desc += toLocalString(" (default: " + o.default_value + ")"); } @@ -1576,19 +1573,19 @@ namespace cxxopts return result; } - } + } // namespace inline ParseResult::ParseResult ( - const std::shared_ptr< + std::shared_ptr< std::unordered_map<std::string, std::shared_ptr<OptionDetails>> > options, std::vector<std::string> positional, bool allow_unrecognised, int& argc, char**& argv ) -: m_options(options) +: m_options(std::move(options)) , m_positional(std::move(positional)) , m_next_positional(m_positional.begin()) , m_allow_unrecognised(allow_unrecognised) @@ -1624,7 +1621,7 @@ OptionAdder::operator() ( const std::string& opts, const std::string& desc, - std::shared_ptr<const Value> value, + const std::shared_ptr<const Value>& value, std::string arg_help ) { @@ -1657,10 +1654,7 @@ OptionAdder::operator() { return std::make_tuple(long_.str(), short_.str()); } - else - { - return std::make_tuple(short_.str(), long_.str()); - } + return std::make_tuple(short_.str(), long_.str()); }(short_match, long_match); m_options.add_option @@ -1678,7 +1672,7 @@ OptionAdder::operator() inline void -ParseResult::parse_default(std::shared_ptr<OptionDetails> details) +ParseResult::parse_default(const std::shared_ptr<OptionDetails>& details) { m_results[details].parse_default(details); } @@ -1687,7 +1681,7 @@ inline void ParseResult::parse_option ( - std::shared_ptr<OptionDetails> value, + const std::shared_ptr<OptionDetails>& value, const std::string& /*name*/, const std::string& arg ) @@ -1705,7 +1699,7 @@ ParseResult::checked_parse_arg int argc, char* argv[], int& current, - std::shared_ptr<OptionDetails> value, + const std::shared_ptr<OptionDetails>& value, const std::string& name ) { @@ -1750,7 +1744,7 @@ ParseResult::add_to_option(const std::string& option, const std::string& arg) inline bool -ParseResult::consume_positional(std::string a) +ParseResult::consume_positional(const std::string& a) { while (m_next_positional != m_positional.end()) { @@ -1766,22 +1760,13 @@ ParseResult::consume_positional(std::string a) ++m_next_positional; return true; } - else - { - ++m_next_positional; - continue; - } + ++m_next_positional; + continue; } - else - { - add_to_option(*m_next_positional, a); - return true; - } - } - else - { - throw_or_mimic<option_not_exists_exception>(*m_next_positional); + add_to_option(*m_next_positional, a); + return true; } + throw_or_mimic<option_not_exists_exception>(*m_next_positional); } return false; @@ -1808,7 +1793,7 @@ inline void Options::parse_positional(std::initializer_list<std::string> options) { - parse_positional(std::vector<std::string>(std::move(options))); + parse_positional(std::vector<std::string>(options)); } inline @@ -1882,11 +1867,8 @@ ParseResult::parse(int& argc, char**& argv) { continue; } - else - { - //error - throw_or_mimic<option_not_exists_exception>(name); - } + //error + throw_or_mimic<option_not_exists_exception>(name); } auto value = iter->second; @@ -1923,11 +1905,8 @@ ParseResult::parse(int& argc, char**& argv) ++current; continue; } - else - { - //error - throw_or_mimic<option_not_exists_exception>(name); - } + //error + throw_or_mimic<option_not_exists_exception>(name); } auto opt = iter->second; @@ -1954,7 +1933,7 @@ ParseResult::parse(int& argc, char**& argv) for (auto& opt : *m_options) { auto& detail = opt.second; - auto& value = detail->value(); + const auto& value = detail->value(); auto& store = m_results[detail]; @@ -2004,19 +1983,19 @@ Options::add_option const std::string& s, const std::string& l, std::string desc, - std::shared_ptr<const Value> value, + const std::shared_ptr<const Value>& value, std::string arg_help ) { auto stringDesc = toLocalString(std::move(desc)); auto option = std::make_shared<OptionDetails>(s, l, stringDesc, value); - if (s.size() > 0) + if (!s.empty()) { add_one_option(s, option); } - if (l.size() > 0) + if (!l.empty()) { add_one_option(l, option); } @@ -2037,7 +2016,7 @@ void Options::add_one_option ( const std::string& option, - std::shared_ptr<OptionDetails> details + const std::shared_ptr<OptionDetails>& details ) { auto in = m_options->emplace(option, details); @@ -2052,7 +2031,7 @@ inline String Options::help_one_group(const std::string& g) const { - typedef std::vector<std::pair<String, String>> OptionHelp; + using OptionHelp = std::vector<std::pair<String, String>>; auto group = m_help.find(g); if (group == m_help.end()) @@ -2151,7 +2130,7 @@ Options::generate_all_groups_help(String& result) const std::vector<std::string> all_groups; all_groups.reserve(m_help.size()); - for (auto& group : m_help) + for (const auto& group : m_help) { all_groups.push_back(group.first); } @@ -2166,13 +2145,13 @@ Options::help(const std::vector<std::string>& help_groups) const String result = m_help_string + "\nUsage:\n " + toLocalString(m_program) + " " + toLocalString(m_custom_help); - if (m_positional.size() > 0 && m_positional_help.size() > 0) { + if (!m_positional.empty() && !m_positional_help.empty()) { result += " " + toLocalString(m_positional_help); } result += "\n\n"; - if (help_groups.size() == 0) + if (help_groups.empty()) { generate_all_groups_help(result); } @@ -2185,7 +2164,7 @@ Options::help(const std::vector<std::string>& help_groups) const } inline -const std::vector<std::string> +std::vector<std::string> Options::groups() const { std::vector<std::string> g; @@ -2210,6 +2189,6 @@ Options::group_help(const std::string& group) const return m_help.at(group); } -} +} // namespace cxxopts #endif //CXXOPTS_HPP_INCLUDED |