diff --git a/CMakeLists.txt b/CMakeLists.txt index a3075c214..38d24c302 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -62,7 +62,7 @@ if(CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME) target_compile_options(CLI11_warnings INTERFACE "/WX") endif() else() - target_compile_options(CLI11_warnings INTERFACE -Wall -Wextra -pedantic -Wshadow) + target_compile_options(CLI11_warnings INTERFACE -Wall -Wextra -pedantic -Wshadow -Wsign-conversion) if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU") if(NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS 4.9) # GCC 4.8 has a false positive diff --git a/README.md b/README.md index 96b0a37bd..066fd1f31 100644 --- a/README.md +++ b/README.md @@ -568,7 +568,7 @@ There are several options that are supported on the main app and subcommands and - `.parse_complete_callback(void() function)`: 🚧 Set the callback that runs at the completion of parsing. for subcommands this is executed at the completion of the single subcommand and can be executed multiple times. See [Subcommand callbacks](#callbacks) for some additional details. - `.final_callback(void() function)`: 🚧 Set the callback that runs at the end of all processing. This is the last thing that is executed before returning. See [Subcommand callbacks](#callbacks) for some additional details. - `.immediate_callback()`: 🆕 Specifies whether the callback for a subcommand should be run as a `parse_complete_callback`(true) or `final_callback`(false). When used on the main app 🚧 it will execute the main app callback prior to the callbacks for a subcommand if they do not also have the `immediate_callback` flag set. 🚧 It is preferable to use the `parse_complete_callback` or `final_callback` directly instead of the `callback` and `immediate_callback` if one wishes to control the ordering and timing of callback. Though `immediate_callback` can be used to swap them if that is needed. -- `.pre_parse_callback(void(size_t) function)`: 🆕 Set a callback that executes after the first argument of an application is processed. See [Subcommand callbacks](#callbacks) for some additional details. +- `.pre_parse_callback(void(std::size_t) function)`: 🆕 Set a callback that executes after the first argument of an application is processed. See [Subcommand callbacks](#callbacks) for some additional details. - `.allow_extras()`: Do not throw an error if extra arguments are left over. - `.positionals_at_end()`: 🆕 Specify that positional arguments occur as the last arguments and throw an error if an unexpected positional is encountered. - `.prefix_command()`: Like `allow_extras`, but stop immediately on the first unrecognized item. It is ideal for allowing your app or subcommand to be a "prefix" to calling another app. diff --git a/book/chapters/flags.md b/book/chapters/flags.md index 08e5f1e7c..59c0254a9 100644 --- a/book/chapters/flags.md +++ b/book/chapters/flags.md @@ -37,7 +37,7 @@ After parsing, you can use `my_flag->count()` to count the number of times this ## Callback flags -If you want to define a callback that runs when you make a flag, you can use `add_flag_function` (C++11 or newer) or `add_flag` (C++14 or newer only) to add a callback function. The function should have the signature `void(size_t)`. This could be useful for a version printout, etc. +If you want to define a callback that runs when you make a flag, you can use `add_flag_function` (C++11 or newer) or `add_flag` (C++14 or newer only) to add a callback function. The function should have the signature `void(std::size_t)`. This could be useful for a version printout, etc. ``` auto callback = [](int count){std::cout << "This was called " << count << " times";}; diff --git a/examples/positional_arity.cpp b/examples/positional_arity.cpp index 0b390df25..a0340ec20 100644 --- a/examples/positional_arity.cpp +++ b/examples/positional_arity.cpp @@ -13,7 +13,7 @@ int main(int argc, char **argv) { files->add_option("file1", file1, "first file")->required(); files->add_option("file2", file2, "second file"); // set a pre parse callback that turns the numbers group on or off depending on the number of arguments - app.preparse_callback([numbers](size_t arity) { + app.preparse_callback([numbers](std::size_t arity) { if(arity <= 2) { numbers->disabled(); } else { diff --git a/include/CLI/App.hpp b/include/CLI/App.hpp index c8d09fbce..3871dbc59 100644 --- a/include/CLI/App.hpp +++ b/include/CLI/App.hpp @@ -96,7 +96,7 @@ class App { bool immediate_callback_{false}; /// This is a function that runs prior to the start of parsing - std::function pre_parse_callback_{}; + std::function pre_parse_callback_{}; /// This is a function that runs when parsing has finished. std::function parse_complete_callback_{}; @@ -204,19 +204,19 @@ class App { App *parent_{nullptr}; /// Counts the number of times this command/subcommand was parsed - size_t parsed_{0}; + std::size_t parsed_{0}; /// Minimum required subcommands (not inheritable!) - size_t require_subcommand_min_{0}; + std::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}; + std::size_t require_subcommand_max_{0}; /// Minimum required options (not inheritable!) - size_t require_option_min_{0}; + std::size_t require_option_min_{0}; /// Max number of options allowed. 0 is unlimited (not inheritable) - size_t require_option_max_{0}; + std::size_t require_option_max_{0}; /// The group membership INHERITABLE std::string group_{"Subcommands"}; @@ -322,7 +322,7 @@ class App { /// Set a callback to execute prior to parsing. /// - App *preparse_callback(std::function pp_callback) { + App *preparse_callback(std::function pp_callback) { pre_parse_callback_ = std::move(pp_callback); return this; } @@ -1056,12 +1056,12 @@ class App { /// No argument version of count counts the number of times this subcommand was /// passed in. The main app will return 1. Unnamed subcommands will also return 1 unless /// otherwise modified in a callback - size_t count() const { return parsed_; } + std::size_t count() const { return parsed_; } /// Get a count of all the arguments processed in options and subcommands, this excludes arguments which were /// treated as extras. - size_t count_all() const { - size_t cnt{0}; + std::size_t count_all() const { + std::size_t cnt{0}; for(auto &opt : options_) { cnt += opt->count(); } @@ -1093,17 +1093,17 @@ class App { App *require_subcommand(int value) { if(value < 0) { require_subcommand_min_ = 0; - require_subcommand_max_ = static_cast(-value); + require_subcommand_max_ = static_cast(-value); } else { - require_subcommand_min_ = static_cast(value); - require_subcommand_max_ = static_cast(value); + require_subcommand_min_ = static_cast(value); + require_subcommand_max_ = static_cast(value); } return this; } /// Explicitly control the number of subcommands required. Setting 0 /// for the max means unlimited number allowed. Max number inheritable. - App *require_subcommand(size_t min, size_t max) { + App *require_subcommand(std::size_t min, std::size_t max) { require_subcommand_min_ = min; require_subcommand_max_ = max; return this; @@ -1122,17 +1122,17 @@ class App { App *require_option(int value) { if(value < 0) { require_option_min_ = 0; - require_option_max_ = static_cast(-value); + require_option_max_ = static_cast(-value); } else { - require_option_min_ = static_cast(value); - require_option_max_ = static_cast(value); + require_option_min_ = static_cast(value); + require_option_max_ = static_cast(value); } return this; } /// Explicitly control the number of options required. Setting 0 /// for the max means unlimited number allowed. Max number inheritable. - App *require_option(size_t min, size_t max) { + App *require_option(std::size_t min, std::size_t max) { require_option_min_ = min; require_option_max_ = max; return this; @@ -1188,7 +1188,7 @@ class App { } std::vector args; - args.reserve(static_cast(argc) - 1); + args.reserve(static_cast(argc) - 1); for(int i = argc - 1; i > 0; i--) args.emplace_back(argv[i]); parse(std::move(args)); @@ -1300,7 +1300,7 @@ class App { ///@{ /// Counts the number of times the given option was passed. - size_t count(std::string option_name) const { return get_option(option_name)->count(); } + std::size_t count(std::string option_name) const { return get_option(option_name)->count(); } /// Get a subcommand pointer list to the currently selected subcommands (after parsing by default, in command /// line order; use parsed = false to get the original definition list.) @@ -1608,16 +1608,16 @@ class App { std::string get_footer() const { return (footer_callback_) ? footer_callback_() + '\n' + footer_ : footer_; } /// Get the required min subcommand value - size_t get_require_subcommand_min() const { return require_subcommand_min_; } + std::size_t get_require_subcommand_min() const { return require_subcommand_min_; } /// Get the required max subcommand value - size_t get_require_subcommand_max() const { return require_subcommand_max_; } + std::size_t get_require_subcommand_max() const { return require_subcommand_max_; } /// Get the required min option value - size_t get_require_option_min() const { return require_option_min_; } + std::size_t get_require_option_min() const { return require_option_min_; } /// Get the required max option value - size_t get_require_option_max() const { return require_option_max_; } + std::size_t get_require_option_max() const { return require_option_max_; } /// Get the prefix command status bool get_prefix_command() const { return prefix_command_; } @@ -1762,8 +1762,8 @@ class App { } /// This returns the number of remaining options, minus the -- separator - size_t remaining_size(bool recurse = false) const { - auto remaining_options = static_cast(std::count_if( + std::size_t remaining_size(bool recurse = false) const { + auto remaining_options = static_cast(std::count_if( std::begin(missing_), std::end(missing_), [](const std::pair &val) { return val.first != detail::Classifier::POSITIONAL_MARK; })); @@ -1798,7 +1798,7 @@ class App { } } - size_t nameless_subs{0}; + std::size_t nameless_subs{0}; for(const App_p &app : subcommands_) { app->_validate(); if(app->get_name().empty()) @@ -1943,7 +1943,7 @@ class App { #ifdef _MSC_VER // Windows version - size_t sz = 0; + std::size_t sz = 0; if(_dupenv_s(&buffer, &sz, opt->envname_.c_str()) == 0 && buffer != nullptr) { ename_string = std::string(buffer); free(buffer); @@ -2065,7 +2065,7 @@ class App { return; } - size_t used_options = 0; + std::size_t used_options = 0; for(const Option_p &opt : options_) { if(opt->count() != 0) { @@ -2155,7 +2155,7 @@ class App { /// Throw an error if anything is left over and should not be. void _process_extras() { if(!(allow_extras_ || prefix_command_)) { - size_t num_left_over = remaining_size(); + std::size_t num_left_over = remaining_size(); if(num_left_over > 0) { throw ExtrasError(name_, remaining(false)); } @@ -2171,7 +2171,7 @@ class App { /// Modifies the args to fill in the missing items before throwing. void _process_extras(std::vector &args) { if(!(allow_extras_ || prefix_command_)) { - size_t num_left_over = remaining_size(); + std::size_t num_left_over = remaining_size(); if(num_left_over > 0) { args = remaining(false); throw ExtrasError(name_, args); @@ -2250,7 +2250,7 @@ class App { } /// Fill in a single config option - bool _parse_single_config(const ConfigItem &item, size_t level = 0) { + bool _parse_single_config(const ConfigItem &item, std::size_t level = 0) { if(level < item.parents.size()) { try { auto subcom = get_subcommand(item.parents.at(level)); @@ -2334,13 +2334,13 @@ class App { } /// Count the required remaining positional arguments - size_t _count_remaining_positionals(bool required_only = false) const { - size_t retval = 0; + std::size_t _count_remaining_positionals(bool required_only = false) const { + std::size_t retval = 0; for(const Option_p &opt : options_) { if(opt->get_positional() && (!required_only || opt->get_required())) { if(opt->get_items_expected_min() > 0 && static_cast(opt->count()) < opt->get_items_expected_min()) { - retval += static_cast(opt->get_items_expected_min()) - opt->count(); + retval += static_cast(opt->get_items_expected_min()) - opt->count(); } } } @@ -2655,7 +2655,7 @@ class App { } /// Trigger the pre_parse callback if needed - void _trigger_pre_parse(size_t remaining_args) { + void _trigger_pre_parse(std::size_t remaining_args) { if(!pre_parse_called_) { pre_parse_called_ = true; if(pre_parse_callback_) { @@ -2831,7 +2831,7 @@ class Option_group : public App { inline void TriggerOn(App *trigger_app, App *app_to_enable) { app_to_enable->enabled_by_default(false); app_to_enable->disabled_by_default(); - trigger_app->preparse_callback([app_to_enable](size_t) { app_to_enable->disabled(false); }); + trigger_app->preparse_callback([app_to_enable](std::size_t) { app_to_enable->disabled(false); }); } /// Helper function to enable one option group/subcommand when another is used @@ -2841,7 +2841,7 @@ inline void TriggerOn(App *trigger_app, std::vector apps_to_enable) { app->disabled_by_default(); } - trigger_app->preparse_callback([apps_to_enable](size_t) { + trigger_app->preparse_callback([apps_to_enable](std::size_t) { for(auto &app : apps_to_enable) { app->disabled(false); } @@ -2852,7 +2852,7 @@ inline void TriggerOn(App *trigger_app, std::vector apps_to_enable) { inline void TriggerOff(App *trigger_app, App *app_to_enable) { app_to_enable->disabled_by_default(false); app_to_enable->enabled_by_default(); - trigger_app->preparse_callback([app_to_enable](size_t) { app_to_enable->disabled(); }); + trigger_app->preparse_callback([app_to_enable](std::size_t) { app_to_enable->disabled(); }); } /// Helper function to disable one option group/subcommand when another is used @@ -2862,7 +2862,7 @@ inline void TriggerOff(App *trigger_app, std::vector apps_to_enable) { app->enabled_by_default(); } - trigger_app->preparse_callback([apps_to_enable](size_t) { + trigger_app->preparse_callback([apps_to_enable](std::size_t) { for(auto &app : apps_to_enable) { app->disabled(); } diff --git a/include/CLI/ConfigFwd.hpp b/include/CLI/ConfigFwd.hpp index d8f041b97..98b693f89 100644 --- a/include/CLI/ConfigFwd.hpp +++ b/include/CLI/ConfigFwd.hpp @@ -20,7 +20,7 @@ namespace detail { /// Comma separated join, adds quotes if needed inline std::string ini_join(std::vector args) { std::ostringstream s; - size_t start = 0; + std::size_t start = 0; for(const auto &arg : args) { if(start++ > 0) s << " "; @@ -107,7 +107,7 @@ class ConfigINI : public Config { std::vector items_buffer; detail::trim(line); - size_t len = line.length(); + std::size_t len = line.length(); if(len > 1 && line[0] == '[' && line[len - 1] == ']') { section = line.substr(1, len - 2); } else if(len > 0 && line[0] != ';') { diff --git a/include/CLI/Error.hpp b/include/CLI/Error.hpp index f55966560..2613527b3 100644 --- a/include/CLI/Error.hpp +++ b/include/CLI/Error.hpp @@ -205,14 +205,15 @@ class ValidationError : public ParseError { class RequiredError : public ParseError { CLI11_ERROR_DEF(ParseError, RequiredError) explicit RequiredError(std::string name) : RequiredError(name + " is required", ExitCodes::RequiredError) {} - static RequiredError Subcommand(size_t min_subcom) { + static RequiredError Subcommand(std::size_t min_subcom) { if(min_subcom == 1) { return RequiredError("A subcommand"); } return RequiredError("Requires at least " + std::to_string(min_subcom) + " subcommands", ExitCodes::RequiredError); } - static RequiredError Option(size_t min_option, size_t max_option, size_t used, const std::string &option_list) { + static RequiredError + Option(std::size_t min_option, std::size_t max_option, std::size_t used, const std::string &option_list) { if((min_option == 1) && (max_option == 1) && (used == 0)) return RequiredError("Exactly 1 option from [" + option_list + "]"); if((min_option == 1) && (max_option == 1) && (used > 1)) @@ -239,18 +240,18 @@ class RequiredError : public ParseError { class ArgumentMismatch : public ParseError { CLI11_ERROR_DEF(ParseError, ArgumentMismatch) CLI11_ERROR_SIMPLE(ArgumentMismatch) - ArgumentMismatch(std::string name, int expected, size_t recieved) + ArgumentMismatch(std::string name, int expected, std::size_t recieved) : ArgumentMismatch(expected > 0 ? ("Expected exactly " + std::to_string(expected) + " arguments to " + name + ", got " + std::to_string(recieved)) : ("Expected at least " + std::to_string(-expected) + " arguments to " + name + ", got " + std::to_string(recieved)), ExitCodes::ArgumentMismatch) {} - static ArgumentMismatch AtLeast(std::string name, int num, size_t received) { + static ArgumentMismatch AtLeast(std::string name, int num, std::size_t received) { return ArgumentMismatch(name + ": At least " + std::to_string(num) + " required but received " + std::to_string(received)); } - static ArgumentMismatch AtMost(std::string name, int num, size_t received) { + static ArgumentMismatch AtMost(std::string name, int num, std::size_t received) { return ArgumentMismatch(name + ": At Most " + std::to_string(num) + " required but received " + std::to_string(received)); } diff --git a/include/CLI/FormatterFwd.hpp b/include/CLI/FormatterFwd.hpp index b3f04a451..711275cea 100644 --- a/include/CLI/FormatterFwd.hpp +++ b/include/CLI/FormatterFwd.hpp @@ -35,7 +35,7 @@ class FormatterBase { ///@{ /// The width of the first column - size_t column_width_{30}; + std::size_t column_width_{30}; /// @brief The required help printout labels (user changeable) /// Values are Needs, Excludes, etc. @@ -64,7 +64,7 @@ class FormatterBase { void label(std::string key, std::string val) { labels_[key] = val; } /// Set the column width - void column_width(size_t val) { column_width_ = val; } + void column_width(std::size_t val) { column_width_ = val; } ///@} /// @name Getters @@ -79,7 +79,7 @@ class FormatterBase { } /// Get the current column width - size_t get_column_width() const { return column_width_; } + std::size_t get_column_width() const { return column_width_; } ///@} }; diff --git a/include/CLI/Option.hpp b/include/CLI/Option.hpp index 184605904..4a6a49f69 100644 --- a/include/CLI/Option.hpp +++ b/include/CLI/Option.hpp @@ -340,7 +340,7 @@ class Option : public OptionBase