1
0
mirror of https://github.com/CLIUtils/CLI11.git synced 2025-05-01 13:13:53 +00:00

clear up some additional Wshadow warnings on older compilers (#232)

* clear up some additional Wshadow warnings on older compilers

* clear up a few more Wshadow warnings in option

* add extra test for delimiter in add_option_function call.

* clear up a warning from the test
This commit is contained in:
Philip Top 2019-02-20 09:52:07 -08:00 committed by Henry Schreiner
parent 17d05b000c
commit 546d5ec237
4 changed files with 112 additions and 86 deletions

View File

@ -36,7 +36,7 @@ if(CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME)
if(MSVC) if(MSVC)
add_definitions("/W4") add_definitions("/W4")
else() else()
add_definitions(-Wall -Wextra -pedantic) add_definitions(-Wall -Wextra -pedantic -Wshadow)
endif() endif()
if(CMAKE_VERSION VERSION_GREATER 3.6) if(CMAKE_VERSION VERSION_GREATER 3.6)

View File

@ -193,8 +193,8 @@ class App {
///@} ///@}
/// Special private constructor for subcommand /// Special private constructor for subcommand
App(std::string description, std::string app_name, App *parent) App(std::string app_description, std::string app_name, App *parent)
: name_(std::move(app_name)), description_(std::move(description)), parent_(parent) { : name_(std::move(app_name)), description_(std::move(app_description)), parent_(parent) {
// Inherit if not from a nullptr // Inherit if not from a nullptr
if(parent_ != nullptr) { if(parent_ != nullptr) {
if(parent_->help_ptr_ != nullptr) if(parent_->help_ptr_ != nullptr)
@ -228,7 +228,8 @@ class App {
///@{ ///@{
/// Create a new program. Pass in the same arguments as main(), along with a help string. /// Create a new program. Pass in the same arguments as main(), along with a help string.
explicit App(std::string description = "", std::string app_name = "") : App(description, app_name, nullptr) { explicit App(std::string app_description = "", std::string app_name = "")
: App(app_description, app_name, nullptr) {
set_help_flag("-h,--help", "Print this help message and exit"); set_help_flag("-h,--help", "Print this help message and exit");
} }
@ -353,16 +354,16 @@ class App {
/// ///
Option *add_option(std::string option_name, Option *add_option(std::string option_name,
callback_t option_callback, callback_t option_callback,
std::string description = "", std::string option_description = "",
bool defaulted = false) { bool defaulted = false) {
Option myopt{option_name, description, option_callback, defaulted, this}; Option myopt{option_name, option_description, option_callback, defaulted, this};
if(std::find_if(std::begin(options_), std::end(options_), [&myopt](const Option_p &v) { if(std::find_if(std::begin(options_), std::end(options_), [&myopt](const Option_p &v) {
return *v == myopt; return *v == myopt;
}) == std::end(options_)) { }) == std::end(options_)) {
options_.emplace_back(); options_.emplace_back();
Option_p &option = options_.back(); Option_p &option = options_.back();
option.reset(new Option(option_name, description, option_callback, defaulted, this)); option.reset(new Option(option_name, option_description, option_callback, defaulted, this));
option_defaults_.copy_to(option.get()); option_defaults_.copy_to(option.get());
return option.get(); return option.get();
} else } else
@ -373,11 +374,11 @@ class App {
template <typename T, enable_if_t<!is_vector<T>::value, detail::enabler> = detail::dummy> template <typename T, enable_if_t<!is_vector<T>::value, detail::enabler> = detail::dummy>
Option *add_option(std::string option_name, Option *add_option(std::string option_name,
T &variable, ///< The variable to set T &variable, ///< The variable to set
std::string description = "") { std::string option_description = "") {
CLI::callback_t fun = [&variable](CLI::results_t res) { return detail::lexical_cast(res[0], variable); }; CLI::callback_t fun = [&variable](CLI::results_t res) { return detail::lexical_cast(res[0], variable); };
Option *opt = add_option(option_name, fun, description, false); Option *opt = add_option(option_name, fun, option_description, false);
opt->type_name(detail::type_name<T>()); opt->type_name(detail::type_name<T>());
return opt; return opt;
} }
@ -386,7 +387,7 @@ class App {
template <typename T, enable_if_t<!is_vector<T>::value, detail::enabler> = detail::dummy> template <typename T, enable_if_t<!is_vector<T>::value, detail::enabler> = detail::dummy>
Option *add_option_function(std::string option_name, Option *add_option_function(std::string option_name,
const std::function<bool(const T &)> &func, ///< the callback to execute const std::function<bool(const T &)> &func, ///< the callback to execute
std::string description = "") { std::string option_description = "") {
CLI::callback_t fun = [func](CLI::results_t res) { CLI::callback_t fun = [func](CLI::results_t res) {
T variable; T variable;
@ -397,7 +398,7 @@ class App {
return result; return result;
}; };
Option *opt = add_option(option_name, std::move(fun), description, false); Option *opt = add_option(option_name, std::move(fun), option_description, false);
opt->type_name(detail::type_name<T>()); opt->type_name(detail::type_name<T>());
return opt; return opt;
} }
@ -406,12 +407,12 @@ class App {
template <typename T, enable_if_t<!is_vector<T>::value, detail::enabler> = detail::dummy> template <typename T, enable_if_t<!is_vector<T>::value, detail::enabler> = detail::dummy>
Option *add_option(std::string option_name, Option *add_option(std::string option_name,
T &variable, ///< The variable to set T &variable, ///< The variable to set
std::string description, std::string option_description,
bool defaulted) { bool defaulted) {
CLI::callback_t fun = [&variable](CLI::results_t res) { return detail::lexical_cast(res[0], variable); }; CLI::callback_t fun = [&variable](CLI::results_t res) { return detail::lexical_cast(res[0], variable); };
Option *opt = add_option(option_name, fun, description, defaulted); Option *opt = add_option(option_name, fun, option_description, defaulted);
opt->type_name(detail::type_name<T>()); opt->type_name(detail::type_name<T>());
if(defaulted) { if(defaulted) {
std::stringstream out; std::stringstream out;
@ -425,14 +426,14 @@ class App {
template <typename T> template <typename T>
Option *add_option(std::string option_name, Option *add_option(std::string option_name,
std::vector<T> &variable, ///< The variable vector to set std::vector<T> &variable, ///< The variable vector to set
std::string description = "", std::string option_description = "",
char delimiter = '\0') { char delimiter = '\0') {
CLI::callback_t fun = [&variable, delimiter](CLI::results_t res) { CLI::callback_t fun = [&variable, delimiter](CLI::results_t res) {
bool retval = true; bool retval = true;
variable.clear(); variable.clear();
for(const auto &elem : res) { for(const auto &elem : res) {
if(delimiter != '\0') { if((delimiter != '\0') && (elem.find_first_of(delimiter) != std::string::npos)) {
for(const auto &var : CLI::detail::split(elem, delimiter)) { for(const auto &var : CLI::detail::split(elem, delimiter)) {
if(!var.empty()) { if(!var.empty()) {
variable.emplace_back(); variable.emplace_back();
@ -447,7 +448,7 @@ class App {
return (!variable.empty()) && retval; return (!variable.empty()) && retval;
}; };
Option *opt = add_option(option_name, fun, description, false); Option *opt = add_option(option_name, fun, option_description, false);
opt->type_name(detail::type_name<T>())->type_size(-1); opt->type_name(detail::type_name<T>())->type_size(-1);
return opt; return opt;
} }
@ -456,7 +457,7 @@ class App {
template <typename T> template <typename T>
Option *add_option(std::string option_name, Option *add_option(std::string option_name,
std::vector<T> &variable, ///< The variable vector to set std::vector<T> &variable, ///< The variable vector to set
std::string description, std::string option_description,
bool defaulted, bool defaulted,
char delimiter = '\0') { char delimiter = '\0') {
@ -464,7 +465,7 @@ class App {
bool retval = true; bool retval = true;
variable.clear(); variable.clear();
for(const auto &elem : res) { for(const auto &elem : res) {
if(delimiter != '\0') { if((delimiter != '\0') && (elem.find_first_of(delimiter) != std::string::npos)) {
for(const auto &var : CLI::detail::split(elem, delimiter)) { for(const auto &var : CLI::detail::split(elem, delimiter)) {
if(!var.empty()) { if(!var.empty()) {
variable.emplace_back(); variable.emplace_back();
@ -479,7 +480,7 @@ class App {
return (!variable.empty()) && retval; return (!variable.empty()) && retval;
}; };
Option *opt = add_option(option_name, fun, description, defaulted); Option *opt = add_option(option_name, fun, option_description, defaulted);
opt->type_name(detail::type_name<T>())->type_size(-1); opt->type_name(detail::type_name<T>())->type_size(-1);
if(defaulted) if(defaulted)
opt->default_str("[" + detail::join(variable) + "]"); opt->default_str("[" + detail::join(variable) + "]");
@ -490,15 +491,25 @@ class App {
template <typename T, enable_if_t<is_vector<T>::value, detail::enabler> = detail::dummy> template <typename T, enable_if_t<is_vector<T>::value, detail::enabler> = detail::dummy>
Option *add_option_function(std::string option_name, Option *add_option_function(std::string option_name,
const std::function<bool(const T &)> &func, ///< the callback to execute const std::function<bool(const T &)> &func, ///< the callback to execute
std::string description = "") { std::string option_description = "",
char delimiter = '\0') {
CLI::callback_t fun = [func](CLI::results_t res) { CLI::callback_t fun = [func, delimiter](CLI::results_t res) {
T values; T values;
bool retval = true; bool retval = true;
values.reserve(res.size()); values.reserve(res.size());
for(const auto &a : res) { for(const auto &elem : res) {
if((delimiter != '\0') && (elem.find_first_of(delimiter) != std::string::npos)) {
for(const auto &var : CLI::detail::split(elem, delimiter)) {
if(!var.empty()) {
values.emplace_back(); values.emplace_back();
retval &= detail::lexical_cast(a, values.back()); retval &= detail::lexical_cast(var, values.back());
}
}
} else {
values.emplace_back();
retval &= detail::lexical_cast(elem, values.back());
}
} }
if(retval) { if(retval) {
return func(values); return func(values);
@ -506,13 +517,13 @@ class App {
return retval; return retval;
}; };
Option *opt = add_option(option_name, std::move(fun), description, false); Option *opt = add_option(option_name, std::move(fun), std::move(option_description), false);
opt->type_name(detail::type_name<T>())->type_size(-1); opt->type_name(detail::type_name<T>())->type_size(-1);
return opt; return opt;
} }
/// Set a help flag, replace the existing one if present /// Set a help flag, replace the existing one if present
Option *set_help_flag(std::string flag_name = "", std::string description = "") { Option *set_help_flag(std::string flag_name = "", std::string help_description = "") {
if(help_ptr_ != nullptr) { if(help_ptr_ != nullptr) {
remove_option(help_ptr_); remove_option(help_ptr_);
help_ptr_ = nullptr; help_ptr_ = nullptr;
@ -520,7 +531,7 @@ class App {
// Empty name will simply remove the help flag // Empty name will simply remove the help flag
if(!flag_name.empty()) { if(!flag_name.empty()) {
help_ptr_ = add_flag(flag_name, description); help_ptr_ = add_flag(flag_name, std::move(help_description));
help_ptr_->configurable(false); help_ptr_->configurable(false);
} }
@ -528,7 +539,7 @@ class App {
} }
/// Set a help all flag, replaced the existing one if present /// Set a help all flag, replaced the existing one if present
Option *set_help_all_flag(std::string help_name = "", std::string description = "") { Option *set_help_all_flag(std::string help_name = "", std::string help_description = "") {
if(help_all_ptr_ != nullptr) { if(help_all_ptr_ != nullptr) {
remove_option(help_all_ptr_); remove_option(help_all_ptr_);
help_all_ptr_ = nullptr; help_all_ptr_ = nullptr;
@ -536,7 +547,7 @@ class App {
// Empty name will simply remove the help all flag // Empty name will simply remove the help all flag
if(!help_name.empty()) { if(!help_name.empty()) {
help_all_ptr_ = add_flag(help_name, description); help_all_ptr_ = add_flag(help_name, std::move(help_description));
help_all_ptr_->configurable(false); help_all_ptr_->configurable(false);
} }
@ -544,9 +555,9 @@ class App {
} }
/// Add option for flag /// Add option for flag
Option *add_flag(std::string flag_name, std::string description = "") { Option *add_flag(std::string flag_name, std::string flag_description = "") {
CLI::callback_t fun = [](CLI::results_t) { return true; }; CLI::callback_t fun = [](CLI::results_t) { return true; };
Option *opt = add_option(flag_name, fun, description, false); Option *opt = add_option(flag_name, fun, flag_description, false);
if(opt->get_positional()) if(opt->get_positional())
throw IncorrectConstruction::PositionalFlag(flag_name); throw IncorrectConstruction::PositionalFlag(flag_name);
opt->type_size(0); opt->type_size(0);
@ -558,7 +569,7 @@ class App {
enable_if_t<std::is_integral<T>::value && !is_bool<T>::value, detail::enabler> = detail::dummy> enable_if_t<std::is_integral<T>::value && !is_bool<T>::value, detail::enabler> = detail::dummy>
Option *add_flag(std::string flag_name, Option *add_flag(std::string flag_name,
T &flag_count, ///< A variable holding the count T &flag_count, ///< A variable holding the count
std::string description = "") { std::string flag_description = "") {
flag_count = 0; flag_count = 0;
Option *opt; Option *opt;
CLI::callback_t fun = [&flag_count](CLI::results_t res) { CLI::callback_t fun = [&flag_count](CLI::results_t res) {
@ -568,10 +579,10 @@ class App {
if(detail::has_false_flags(flag_name)) { if(detail::has_false_flags(flag_name)) {
std::vector<std::string> neg = detail::get_false_flags(flag_name); std::vector<std::string> neg = detail::get_false_flags(flag_name);
detail::remove_false_flag_notation(flag_name); detail::remove_false_flag_notation(flag_name);
opt = add_option(flag_name, fun, description, false); opt = add_option(flag_name, fun, flag_description, false);
opt->fnames_ = std::move(neg); opt->fnames_ = std::move(neg);
} else { } else {
opt = add_option(flag_name, fun, description, false); opt = add_option(flag_name, fun, flag_description, false);
} }
if(opt->get_positional()) if(opt->get_positional())
@ -585,7 +596,7 @@ class App {
template <typename T, enable_if_t<is_bool<T>::value, detail::enabler> = detail::dummy> template <typename T, enable_if_t<is_bool<T>::value, detail::enabler> = detail::dummy>
Option *add_flag(std::string flag_name, Option *add_flag(std::string flag_name,
T &flag_result, ///< A variable holding true if passed T &flag_result, ///< A variable holding true if passed
std::string description = "") { std::string flag_description = "") {
flag_result = false; flag_result = false;
Option *opt; Option *opt;
CLI::callback_t fun = [&flag_result](CLI::results_t res) { CLI::callback_t fun = [&flag_result](CLI::results_t res) {
@ -595,10 +606,10 @@ class App {
if(detail::has_false_flags(flag_name)) { if(detail::has_false_flags(flag_name)) {
std::vector<std::string> neg = detail::get_false_flags(flag_name); std::vector<std::string> neg = detail::get_false_flags(flag_name);
detail::remove_false_flag_notation(flag_name); detail::remove_false_flag_notation(flag_name);
opt = add_option(flag_name, fun, std::move(description), false); opt = add_option(flag_name, fun, std::move(flag_description), false);
opt->fnames_ = std::move(neg); opt->fnames_ = std::move(neg);
} else { } else {
opt = add_option(flag_name, fun, std::move(description), false); opt = add_option(flag_name, fun, std::move(flag_description), false);
} }
if(opt->get_positional()) if(opt->get_positional())
@ -611,7 +622,7 @@ class App {
/// Add option for callback /// Add option for callback
Option *add_flag_function(std::string flag_name, Option *add_flag_function(std::string flag_name,
std::function<void(int)> function, ///< A function to call, void(size_t) std::function<void(int)> function, ///< A function to call, void(size_t)
std::string description = "") { std::string flag_description = "") {
CLI::callback_t fun = [function](CLI::results_t res) { CLI::callback_t fun = [function](CLI::results_t res) {
int flag_count = 0; int flag_count = 0;
@ -623,10 +634,10 @@ class App {
if(detail::has_false_flags(flag_name)) { if(detail::has_false_flags(flag_name)) {
std::vector<std::string> neg = detail::get_false_flags(flag_name); std::vector<std::string> neg = detail::get_false_flags(flag_name);
detail::remove_false_flag_notation(flag_name); detail::remove_false_flag_notation(flag_name);
opt = add_option(flag_name, fun, std::move(description), false); opt = add_option(flag_name, fun, std::move(flag_description), false);
opt->fnames_ = std::move(neg); opt->fnames_ = std::move(neg);
} else { } else {
opt = add_option(flag_name, fun, std::move(description), false); opt = add_option(flag_name, fun, std::move(flag_description), false);
} }
if(opt->get_positional()) if(opt->get_positional())
@ -639,8 +650,8 @@ class App {
/// Add option for callback (C++14 or better only) /// Add option for callback (C++14 or better only)
Option *add_flag(std::string flag_name, Option *add_flag(std::string flag_name,
std::function<void(int)> function, ///< A function to call, void(int) std::function<void(int)> function, ///< A function to call, void(int)
std::string description = "") { std::string flag_description = "") {
return add_flag_function(std::move(flag_name), std::move(function), std::move(description)); return add_flag_function(std::move(flag_name), std::move(function), std::move(flag_description));
} }
#endif #endif
@ -649,21 +660,21 @@ class App {
Option *add_set(std::string option_name, Option *add_set(std::string option_name,
T &member, ///< The selected member of the set T &member, ///< The selected member of the set
std::set<T> options, ///< The set of possibilities std::set<T> options, ///< The set of possibilities
std::string description = "") { std::string option_description = "") {
Option *opt = add_option(option_name, member, std::move(description)); Option *opt = add_option(option_name, member, std::move(option_description));
opt->check(IsMember{options}); opt->check(IsMember{options});
return opt; return opt;
} }
/// Add set of options (No default, set can be changed afterwords - do not destroy the set) DEPRECATED /// Add set of options (No default, set can be changed afterwards - do not destroy the set) DEPRECATED
template <typename T> template <typename T>
Option *add_mutable_set(std::string option_name, Option *add_mutable_set(std::string option_name,
T &member, ///< The selected member of the set T &member, ///< The selected member of the set
const std::set<T> &options, ///< The set of possibilities const std::set<T> &options, ///< The set of possibilities
std::string description = "") { std::string option_description = "") {
Option *opt = add_option(option_name, member, std::move(description)); Option *opt = add_option(option_name, member, std::move(option_description));
opt->check(IsMember{&options}); opt->check(IsMember{&options});
return opt; return opt;
} }
@ -673,10 +684,10 @@ class App {
Option *add_set(std::string option_name, Option *add_set(std::string option_name,
T &member, ///< The selected member of the set T &member, ///< The selected member of the set
std::set<T> options, ///< The set of possibilities std::set<T> options, ///< The set of possibilities
std::string description, std::string option_description,
bool defaulted) { bool defaulted) {
Option *opt = add_option(option_name, member, std::move(description), defaulted); Option *opt = add_option(option_name, member, std::move(option_description), defaulted);
opt->check(IsMember{options}); opt->check(IsMember{options});
return opt; return opt;
} }
@ -686,10 +697,10 @@ class App {
Option *add_mutable_set(std::string option_name, Option *add_mutable_set(std::string option_name,
T &member, ///< The selected member of the set T &member, ///< The selected member of the set
const std::set<T> &options, ///< The set of possibilities const std::set<T> &options, ///< The set of possibilities
std::string description, std::string option_description,
bool defaulted) { bool defaulted) {
Option *opt = add_option(option_name, member, std::move(description), defaulted); Option *opt = add_option(option_name, member, std::move(option_description), defaulted);
opt->check(IsMember{&options}); opt->check(IsMember{&options});
return opt; return opt;
} }
@ -699,9 +710,9 @@ class App {
Option *add_set_ignore_case(std::string option_name, Option *add_set_ignore_case(std::string option_name,
std::string &member, ///< The selected member of the set std::string &member, ///< The selected member of the set
std::set<std::string> options, ///< The set of possibilities std::set<std::string> options, ///< The set of possibilities
std::string description = "") { std::string option_description = "") {
Option *opt = add_option(option_name, member, std::move(description)); Option *opt = add_option(option_name, member, std::move(option_description));
opt->transform(IsMember{options, CLI::ignore_case}); opt->transform(IsMember{options, CLI::ignore_case});
return opt; return opt;
} }
@ -712,9 +723,9 @@ class App {
Option *add_mutable_set_ignore_case(std::string option_name, Option *add_mutable_set_ignore_case(std::string option_name,
std::string &member, ///< The selected member of the set std::string &member, ///< The selected member of the set
const std::set<std::string> &options, ///< The set of possibilities const std::set<std::string> &options, ///< The set of possibilities
std::string description = "") { std::string option_description = "") {
Option *opt = add_option(option_name, member, std::move(description)); Option *opt = add_option(option_name, member, std::move(option_description));
opt->transform(IsMember{&options, CLI::ignore_case}); opt->transform(IsMember{&options, CLI::ignore_case});
return opt; return opt;
} }
@ -724,10 +735,10 @@ class App {
Option *add_set_ignore_case(std::string option_name, Option *add_set_ignore_case(std::string option_name,
std::string &member, ///< The selected member of the set std::string &member, ///< The selected member of the set
std::set<std::string> options, ///< The set of possibilities std::set<std::string> options, ///< The set of possibilities
std::string description, std::string option_description,
bool defaulted) { bool defaulted) {
Option *opt = add_option(option_name, member, std::move(description), defaulted); Option *opt = add_option(option_name, member, std::move(option_description), defaulted);
opt->transform(IsMember{options, CLI::ignore_case}); opt->transform(IsMember{options, CLI::ignore_case});
return opt; return opt;
} }
@ -738,10 +749,10 @@ class App {
Option *add_mutable_set_ignore_case(std::string option_name, Option *add_mutable_set_ignore_case(std::string option_name,
std::string &member, ///< The selected member of the set std::string &member, ///< The selected member of the set
const std::set<std::string> &options, ///< The set of possibilities const std::set<std::string> &options, ///< The set of possibilities
std::string description, std::string option_description,
bool defaulted) { bool defaulted) {
Option *opt = add_option(option_name, member, std::move(description), defaulted); Option *opt = add_option(option_name, member, std::move(option_description), defaulted);
opt->transform(IsMember{&options, CLI::ignore_case}); opt->transform(IsMember{&options, CLI::ignore_case});
return opt; return opt;
} }
@ -751,9 +762,9 @@ class App {
Option *add_set_ignore_underscore(std::string option_name, Option *add_set_ignore_underscore(std::string option_name,
std::string &member, ///< The selected member of the set std::string &member, ///< The selected member of the set
std::set<std::string> options, ///< The set of possibilities std::set<std::string> options, ///< The set of possibilities
std::string description = "") { std::string option_description = "") {
Option *opt = add_option(option_name, member, std::move(description)); Option *opt = add_option(option_name, member, std::move(option_description));
opt->transform(IsMember{options, CLI::ignore_underscore}); opt->transform(IsMember{options, CLI::ignore_underscore});
return opt; return opt;
} }
@ -764,9 +775,9 @@ class App {
Option *add_mutable_set_ignore_underscore(std::string option_name, Option *add_mutable_set_ignore_underscore(std::string option_name,
std::string &member, ///< The selected member of the set std::string &member, ///< The selected member of the set
const std::set<std::string> &options, ///< The set of possibilities const std::set<std::string> &options, ///< The set of possibilities
std::string description = "") { std::string option_description = "") {
Option *opt = add_option(option_name, member, std::move(description)); Option *opt = add_option(option_name, member, std::move(option_description));
opt->transform(IsMember{options, CLI::ignore_underscore}); opt->transform(IsMember{options, CLI::ignore_underscore});
return opt; return opt;
} }
@ -776,10 +787,10 @@ class App {
Option *add_set_ignore_underscore(std::string option_name, Option *add_set_ignore_underscore(std::string option_name,
std::string &member, ///< The selected member of the set std::string &member, ///< The selected member of the set
std::set<std::string> options, ///< The set of possibilities std::set<std::string> options, ///< The set of possibilities
std::string description, std::string option_description,
bool defaulted) { bool defaulted) {
Option *opt = add_option(option_name, member, std::move(description), defaulted); Option *opt = add_option(option_name, member, std::move(option_description), defaulted);
opt->transform(IsMember{options, CLI::ignore_underscore}); opt->transform(IsMember{options, CLI::ignore_underscore});
return opt; return opt;
} }
@ -790,10 +801,10 @@ class App {
Option *add_mutable_set_ignore_underscore(std::string option_name, Option *add_mutable_set_ignore_underscore(std::string option_name,
std::string &member, ///< The selected member of the set std::string &member, ///< The selected member of the set
const std::set<std::string> &options, ///< The set of possibilities const std::set<std::string> &options, ///< The set of possibilities
std::string description, std::string option_description,
bool defaulted) { bool defaulted) {
Option *opt = add_option(option_name, member, std::move(description), defaulted); Option *opt = add_option(option_name, member, std::move(option_description), defaulted);
opt->transform(IsMember{&options, CLI::ignore_underscore}); opt->transform(IsMember{&options, CLI::ignore_underscore});
return opt; return opt;
} }
@ -803,9 +814,9 @@ class App {
Option *add_set_ignore_case_underscore(std::string option_name, Option *add_set_ignore_case_underscore(std::string option_name,
std::string &member, ///< The selected member of the set std::string &member, ///< The selected member of the set
std::set<std::string> options, ///< The set of possibilities std::set<std::string> options, ///< The set of possibilities
std::string description = "") { std::string option_description = "") {
Option *opt = add_option(option_name, member, std::move(description)); Option *opt = add_option(option_name, member, std::move(option_description));
opt->transform(IsMember{options, CLI::ignore_underscore, CLI::ignore_case}); opt->transform(IsMember{options, CLI::ignore_underscore, CLI::ignore_case});
return opt; return opt;
} }
@ -817,9 +828,9 @@ class App {
Option *add_mutable_set_ignore_case_underscore(std::string option_name, Option *add_mutable_set_ignore_case_underscore(std::string option_name,
std::string &member, ///< The selected member of the set std::string &member, ///< The selected member of the set
const std::set<std::string> &options, ///< The set of possibilities const std::set<std::string> &options, ///< The set of possibilities
std::string description = "") { std::string option_description = "") {
Option *opt = add_option(option_name, member, std::move(description)); Option *opt = add_option(option_name, member, std::move(option_description));
opt->transform(IsMember{&options, CLI::ignore_underscore, CLI::ignore_case}); opt->transform(IsMember{&options, CLI::ignore_underscore, CLI::ignore_case});
return opt; return opt;
} }
@ -829,10 +840,10 @@ class App {
Option *add_set_ignore_case_underscore(std::string option_name, Option *add_set_ignore_case_underscore(std::string option_name,
std::string &member, ///< The selected member of the set std::string &member, ///< The selected member of the set
std::set<std::string> options, ///< The set of possibilities std::set<std::string> options, ///< The set of possibilities
std::string description, std::string option_description,
bool defaulted) { bool defaulted) {
Option *opt = add_option(option_name, member, std::move(description), defaulted); Option *opt = add_option(option_name, member, std::move(option_description), defaulted);
opt->transform(IsMember{options, CLI::ignore_underscore, CLI::ignore_case}); opt->transform(IsMember{options, CLI::ignore_underscore, CLI::ignore_case});
return opt; return opt;
} }
@ -844,10 +855,10 @@ class App {
Option *add_mutable_set_ignore_case_underscore(std::string option_name, Option *add_mutable_set_ignore_case_underscore(std::string option_name,
std::string &member, ///< The selected member of the set std::string &member, ///< The selected member of the set
const std::set<std::string> &options, ///< The set of possibilities const std::set<std::string> &options, ///< The set of possibilities
std::string description, std::string option_description,
bool defaulted) { bool defaulted) {
Option *opt = add_option(option_name, member, std::move(description), defaulted); Option *opt = add_option(option_name, member, std::move(option_description), defaulted);
opt->transform(IsMember{&options, CLI::ignore_underscore, CLI::ignore_case}); opt->transform(IsMember{&options, CLI::ignore_underscore, CLI::ignore_case});
return opt; return opt;
} }
@ -856,7 +867,7 @@ class App {
template <typename T> template <typename T>
Option *add_complex(std::string option_name, Option *add_complex(std::string option_name,
T &variable, T &variable,
std::string description = "", std::string option_description = "",
bool defaulted = false, bool defaulted = false,
std::string label = "COMPLEX") { std::string label = "COMPLEX") {
@ -871,7 +882,7 @@ class App {
return worked; return worked;
}; };
CLI::Option *opt = add_option(option_name, std::move(fun), std::move(description), defaulted); CLI::Option *opt = add_option(option_name, std::move(fun), std::move(option_description), defaulted);
opt->type_name(label)->type_size(2); opt->type_name(label)->type_size(2);
if(defaulted) { if(defaulted) {
std::stringstream out; std::stringstream out;
@ -929,8 +940,8 @@ class App {
///@{ ///@{
/// Add a subcommand. Inherits INHERITABLE and OptionDefaults, and help flag /// Add a subcommand. Inherits INHERITABLE and OptionDefaults, and help flag
App *add_subcommand(std::string subcommand_name = "", std::string description = "") { App *add_subcommand(std::string subcommand_name = "", std::string subcommand_description = "") {
CLI::App_p subcom = std::shared_ptr<App>(new App(std::move(description), subcommand_name, this)); CLI::App_p subcom = std::shared_ptr<App>(new App(std::move(subcommand_description), subcommand_name, this));
return add_subcommand(std::move(subcom)); return add_subcommand(std::move(subcom));
} }
@ -1278,9 +1289,9 @@ class App {
/// Get the app or subcommand description /// Get the app or subcommand description
std::string get_description() const { return description_; } std::string get_description() const { return description_; }
/// Set the description /// Set the description of the app
App *description(const std::string &description) { App *description(std::string app_description) {
description_ = description; description_ = std::move(app_description);
return this; return this;
} }

View File

@ -246,11 +246,11 @@ class Option : public OptionBase<Option> {
/// Making an option by hand is not defined, it must be made by the App class /// Making an option by hand is not defined, it must be made by the App class
Option(std::string option_name, Option(std::string option_name,
std::string description, std::string option_description,
std::function<bool(results_t)> callback, std::function<bool(results_t)> callback,
bool defaulted, bool defaulted,
App *parent) App *parent)
: description_(std::move(description)), default_(defaulted), parent_(parent), : description_(std::move(option_description)), default_(defaulted), parent_(parent),
callback_(callback ? std::move(callback) : [](results_t) { return true; }) { callback_(callback ? std::move(callback) : [](results_t) { return true; }) {
std::tie(snames_, lnames_, pname_) = detail::get_names(detail::split_names(option_name)); std::tie(snames_, lnames_, pname_) = detail::get_names(detail::split_names(option_name));
} }
@ -542,8 +542,8 @@ class Option : public OptionBase<Option> {
const std::string &get_description() const { return description_; } const std::string &get_description() const { return description_; }
/// Set the description /// Set the description
Option *description(const std::string &description) { Option *description(std::string option_description) {
description_ = description; description_ = std::move(option_description);
return this; return this;
} }

View File

@ -1847,6 +1847,21 @@ TEST_F(TApp, CustomUserSepParse2) {
EXPECT_EQ(vals, std::vector<int>({1, 2})); EXPECT_EQ(vals, std::vector<int>({1, 2}));
} }
TEST_F(TApp, CustomUserSepParseFunction) {
std::vector<int> vals = {1, 2, 3};
args = {"--idx", "1,2,3"};
app.add_option_function<std::vector<int>>("--idx",
[&vals](std::vector<int> v) {
vals = std::move(v);
return true;
},
"",
',');
run();
EXPECT_EQ(vals, std::vector<int>({1, 2, 3}));
}
// #209 // #209
TEST_F(TApp, CustomUserSepParse3) { TEST_F(TApp, CustomUserSepParse3) {