Skip to content

Effective C++ #354

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 30, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 10 additions & 18 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,16 @@ on:
- master

jobs:
formatting:
pre-commit:
name: Formatting
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v1
- uses: actions/setup-python@v1
with:
python-version: '3.7'
architecture: 'x64'

- name: Install pre-commit
run: python -m pip install pre-commit

- name: Run pre-commit
run: pre-commit run --all

- name: Display format changes
run: git diff --exit-code
if: always()

- uses: actions/checkout@v1
- uses: actions/setup-python@v1
- name: set PY
run: echo "::set-env name=PY::$(python --version --version | sha256sum | cut -d' ' -f1)"
- uses: actions/cache@v1
with:
path: ~/.cache/pre-commit
key: pre-commit|${{ env.PY }}|${{ hashFiles('.pre-commit-config.yaml') }}
- uses: pre-commit/action@v1.0.0
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
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
[![Build Status Linux and macOS][travis-badge]][travis]
[![Build Status Windows][appveyor-badge]][appveyor]
[![Build Status Azure][azure-badge]][azure]
[![Actions Status][actions-badge]][actions-link]
[![Code Coverage][codecov-badge]][codecov]
[![Codacy Badge][codacy-badge]][codacy-link]
[![Join the chat at https://gitter.im/CLI11gitter/Lobby][gitter-badge]][gitter]
Expand Down Expand Up @@ -870,6 +871,8 @@ CLI11 was developed at the [University of Cincinnati][] to support of the [GooFi
[travis]: https://travis-ci.org/CLIUtils/CLI11
[appveyor-badge]: https://img.shields.io/appveyor/ci/HenrySchreiner/cli11/master.svg?label=Windows
[appveyor]: https://ci.appveyor.com/project/HenrySchreiner/cli11
[actions-badge]: https://github.com/CLIUtils/CLI11/workflows/Tests/badge.svg
[actions-link]: https://github.com/CLIUtils/CLI11/actions
[codecov-badge]: https://codecov.io/gh/CLIUtils/CLI11/branch/master/graph/badge.svg
[codecov]: https://codecov.io/gh/CLIUtils/CLI11
[gitter-badge]: https://badges.gitter.im/CLI11gitter/Lobby.svg
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
Loading