Skip to content

Commit

Permalink
Adding and fixing Weffc++
Browse files Browse the repository at this point in the history
  • Loading branch information
henryiii committed Nov 30, 2019
1 parent 5f69659 commit 2244ecc
Show file tree
Hide file tree
Showing 11 changed files with 101 additions and 75 deletions.
7 changes: 7 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,13 @@ if(CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME)
endif()
else()
target_compile_options(CLI11_warnings INTERFACE -Wall -Wextra -pedantic -Wshadow)
if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
if(NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS 4.9)
# GCC 4.8 has a false positive
# Other compilers ignore this flag
target_compile_options(CLI11_warnings INTERFACE -Weffc++)
endif()
endif()
if(CLI11_WARNINGS_AS_ERRORS)
target_compile_options(CLI11_warnings INTERFACE -Werror)
endif()
Expand Down
53 changes: 28 additions & 25 deletions include/CLI/App.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,10 @@ class App {
///@{

/// Subcommand name or program name (from parser if name is empty)
std::string name_;
std::string name_{};

/// Description of the current program/subcommand
std::string description_;
std::string description_{};

/// If true, allow extra arguments (ie, don't throw an error). INHERITABLE
bool allow_extras_{false};
Expand Down Expand Up @@ -96,32 +96,32 @@ class App {
bool immediate_callback_{false};

/// This is a function that runs prior to the start of parsing
std::function<void(size_t)> pre_parse_callback_;
std::function<void(size_t)> pre_parse_callback_{};

/// This is a function that runs when parsing has finished.
std::function<void()> parse_complete_callback_;
std::function<void()> parse_complete_callback_{};
/// This is a function that runs when all processing has completed
std::function<void()> final_callback_;
std::function<void()> final_callback_{};

///@}
/// @name Options
///@{

/// The default values for options, customizable and changeable INHERITABLE
OptionDefaults option_defaults_;
OptionDefaults option_defaults_{};

/// The list of options, stored locally
std::vector<Option_p> options_;
std::vector<Option_p> options_{};

///@}
/// @name Help
///@{

/// Footer to put after all options in the help output INHERITABLE
std::string footer_;
std::string footer_{};

/// This is a function that generates a footer to put after all other options in help output
std::function<std::string()> footer_callback_;
std::function<std::string()> footer_callback_{};

/// A pointer to the help flag if there is one INHERITABLE
Option *help_ptr_{nullptr};
Expand All @@ -133,7 +133,7 @@ class App {
std::shared_ptr<FormatterBase> formatter_{new Formatter()};

/// The error message printing function INHERITABLE
std::function<std::string(const App *, const Error &e)> failure_message_ = FailureMessage::simple;
std::function<std::string(const App *, const Error &e)> failure_message_{FailureMessage::simple};

///@}
/// @name Parsing
Expand All @@ -144,35 +144,35 @@ class App {
/// Pair of classifier, string for missing options. (extra detail is removed on returning from parse)
///
/// This is faster and cleaner than storing just a list of strings and reparsing. This may contain the -- separator.
missing_t missing_;
missing_t missing_{};

/// This is a list of pointers to options with the original parse order
std::vector<Option *> parse_order_;
std::vector<Option *> parse_order_{};

/// This is a list of the subcommands collected, in order
std::vector<App *> parsed_subcommands_;
std::vector<App *> parsed_subcommands_{};

/// this is a list of subcommands that are exclusionary to this one
std::set<App *> exclude_subcommands_;
std::set<App *> exclude_subcommands_{};

/// This is a list of options which are exclusionary to this App, if the options were used this subcommand should
/// not be
std::set<Option *> exclude_options_;
std::set<Option *> exclude_options_{};

/// this is a list of subcommands or option groups that are required by this one, the list is not mutual, the
/// listed subcommands do not require this one
std::set<App *> need_subcommands_;
std::set<App *> need_subcommands_{};

/// This is a list of options which are required by this app, the list is not mutual, listed options do not need the
/// subcommand not be
std::set<Option *> need_options_;
std::set<Option *> need_options_{};

///@}
/// @name Subcommands
///@{

/// Storage for subcommand list
std::vector<App_p> subcommands_;
std::vector<App_p> subcommands_{};

/// If true, the program name is not case sensitive INHERITABLE
bool ignore_case_{false};
Expand Down Expand Up @@ -204,32 +204,32 @@ class App {
App *parent_{nullptr};

/// Counts the number of times this command/subcommand was parsed
size_t parsed_ = 0;
size_t parsed_{0};

/// Minimum required subcommands (not inheritable!)
size_t require_subcommand_min_ = 0;
size_t require_subcommand_min_{0};

/// Max number of subcommands allowed (parsing stops after this number). 0 is unlimited INHERITABLE
size_t require_subcommand_max_ = 0;
size_t require_subcommand_max_{0};

/// Minimum required options (not inheritable!)
size_t require_option_min_ = 0;
size_t require_option_min_{0};

/// Max number of options allowed. 0 is unlimited (not inheritable)
size_t require_option_max_ = 0;
size_t require_option_max_{0};

/// The group membership INHERITABLE
std::string group_{"Subcommands"};

/// Alias names for the subcommand
std::vector<std::string> aliases_;
std::vector<std::string> aliases_{};

///@}
/// @name Config
///@{

/// The name of the connected config file
std::string config_name_;
std::string config_name_{};

/// True if ini is required (throws if not present), if false simply keep going.
bool config_required_{false};
Expand Down Expand Up @@ -285,6 +285,9 @@ class App {
set_help_flag("-h,--help", "Print this help message and exit");
}

App(const App &) = delete;
App &operator=(const App &) = delete;

/// virtual destructor
virtual ~App() = default;

Expand Down
8 changes: 4 additions & 4 deletions include/CLI/ConfigFwd.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,13 @@ inline std::string ini_join(std::vector<std::string> args) {
/// Holds values to load into Options
struct ConfigItem {
/// This is the list of parents
std::vector<std::string> parents;
std::vector<std::string> parents{};

/// This is the name
std::string name;
std::string name{};

/// Listing of inputs
std::vector<std::string> inputs;
std::vector<std::string> inputs{};

/// The list of parents and name joined by "."
std::string fullname() const {
Expand All @@ -61,7 +61,7 @@ struct ConfigItem {
/// This class provides a converter for configuration files.
class Config {
protected:
std::vector<ConfigItem> items;
std::vector<ConfigItem> items{};

public:
/// Convert an app into a configuration
Expand Down
2 changes: 1 addition & 1 deletion include/CLI/FormatterFwd.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class FormatterBase {

/// @brief The required help printout labels (user changeable)
/// Values are Needs, Excludes, etc.
std::map<std::string, std::string> labels_;
std::map<std::string, std::string> labels_{};

///@}
/// @name Basic
Expand Down
35 changes: 19 additions & 16 deletions include/CLI/Option.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -233,41 +233,41 @@ class Option : public OptionBase<Option> {
///@{

/// A list of the short names (`-a`) without the leading dashes
std::vector<std::string> snames_;
std::vector<std::string> snames_{};

/// A list of the long names (`--long`) without the leading dashes
std::vector<std::string> lnames_;
std::vector<std::string> lnames_{};

/// A list of the flag names with the appropriate default value, the first part of the pair should be duplicates of
/// what is in snames or lnames but will trigger a particular response on a flag
std::vector<std::pair<std::string, std::string>> default_flag_values_;
std::vector<std::pair<std::string, std::string>> default_flag_values_{};

/// a list of flag names with specified default values;
std::vector<std::string> fnames_;
std::vector<std::string> fnames_{};

/// A positional name
std::string pname_;
std::string pname_{};

/// If given, check the environment for this option
std::string envname_;
std::string envname_{};

///@}
/// @name Help
///@{

/// The description for help strings
std::string description_;
std::string description_{};

/// A human readable default value, either manually set, captured, or captured by default
std::string default_str_;
std::string default_str_{};

/// A human readable type value, set when App creates this
///
/// This is a lambda function so "types" can be dynamic, such as when a set prints its contents.
std::function<std::string()> type_name_{[]() { return std::string(); }};

/// Run this function to capture a default (ignore if empty)
std::function<std::string()> default_function_;
std::function<std::string()> default_function_{};

///@}
/// @name Configuration
Expand All @@ -285,32 +285,32 @@ class Option : public OptionBase<Option> {
int expected_max_{1};

/// A list of Validators to run on each value parsed
std::vector<Validator> validators_;
std::vector<Validator> validators_{};

/// A list of options that are required with this option
std::set<Option *> needs_;
std::set<Option *> needs_{};

/// A list of options that are excluded with this option
std::set<Option *> excludes_;
std::set<Option *> excludes_{};

///@}
/// @name Other
///@{

/// Remember the parent app
App *parent_;
App *parent_{nullptr};

/// Options store a callback to do all the work
callback_t callback_;
callback_t callback_{};

///@}
/// @name Parsing results
///@{

/// complete Results of parsing
results_t results_;
results_t results_{};
/// results after reduction
results_t proc_results_;
results_t proc_results_{};
/// enumeration for the option state machine
enum class option_state {
parsing = 0, //!< The option is currently collecting parsed results
Expand All @@ -336,6 +336,9 @@ class Option : public OptionBase<Option> {
/// @name Basic
///@{

Option(const Option &) = delete;
Option &operator=(const Option &) = delete;

/// Count the total number of times an option was passed
size_t count() const { return results_.size(); }

Expand Down
2 changes: 1 addition & 1 deletion include/CLI/Validators.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class Validator {
/// Returns a string error message if validation fails.
std::function<std::string(std::string &)> func_{[](std::string &) { return std::string{}; }};
/// The name for search purposes of the Validator
std::string name_;
std::string name_{};
/// A Validator will only apply to an indexed value (-1 is all elements)
int application_index_ = -1;
/// Enable for Validator to allow it to be disabled if need be
Expand Down
4 changes: 2 additions & 2 deletions tests/HelpTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -570,8 +570,8 @@ TEST(Exit, ExitCodes) {

struct CapturedHelp : public ::testing::Test {
CLI::App app{"My Test Program"};
std::stringstream out;
std::stringstream err;
std::stringstream out{};
std::stringstream err{};

int run(const CLI::Error &e) { return app.exit(e, out, err); }

Expand Down
13 changes: 8 additions & 5 deletions tests/NewParseTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -203,8 +203,8 @@ class spair {
public:
spair() = default;
spair(const std::string &s1, const std::string &s2) : first(s1), second(s2) {}
std::string first;
std::string second;
std::string first{};
std::string second{};
};
// an example of custom converter that can be used to add new parsing options
// On MSVC and possibly some other new compilers this can be a free standing function without the template
Expand Down Expand Up @@ -352,7 +352,7 @@ template <class X> class objWrapper {
const X &value() const { return val_; }

private:
X val_;
X val_{};
};

// I think there is a bug with the is_assignable in visual studio 2015 it is fixed in later versions
Expand Down Expand Up @@ -486,14 +486,17 @@ template <class X> class AobjWrapper {
// delete all other constructors
template <class TT> AobjWrapper(TT &&obj) = delete;
// single assignment operator
void operator=(X val) { val_ = val; }
AobjWrapper &operator=(X val) {
val_ = val;
return *this;
}
// delete all other assignment operators
template <typename TT> void operator=(TT &&obj) = delete;

const X &value() const { return val_; }

private:
X val_;
X val_{};
};

static_assert(std::is_assignable<AobjWrapper<uint16_t> &, uint16_t>::value,
Expand Down
24 changes: 14 additions & 10 deletions tests/OptionGroupTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -370,16 +370,20 @@ TEST_F(TApp, InvalidOptions) {

struct ManyGroups : public TApp {

CLI::Option_group *main;
CLI::Option_group *g1;
CLI::Option_group *g2;
CLI::Option_group *g3;
std::string name1;
std::string name2;
std::string name3;
std::string val1;
std::string val2;
std::string val3;
CLI::Option_group *main{nullptr};
CLI::Option_group *g1{nullptr};
CLI::Option_group *g2{nullptr};
CLI::Option_group *g3{nullptr};
std::string name1{};
std::string name2{};
std::string name3{};
std::string val1{};
std::string val2{};
std::string val3{};

ManyGroups(const ManyGroups &) = delete;
ManyGroups &operator=(const ManyGroups &) = delete;

ManyGroups() {
main = app.add_option_group("main", "the main outer group");
g1 = main->add_option_group("g1", "group1 description");
Expand Down
Loading

0 comments on commit 2244ecc

Please sign in to comment.