Skip to content

Commit

Permalink
Switching to new backend
Browse files Browse the repository at this point in the history
  • Loading branch information
henryiii committed Feb 28, 2019
1 parent 94948d6 commit 952bee9
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 53 deletions.
72 changes: 21 additions & 51 deletions include/CLI/App.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -370,16 +370,21 @@ class App {
throw OptionAlreadyAdded(myopt.get_name());
}

/// Add option for non-vectors (duplicate copy needed without defaulted to avoid `iostream << value`)
template <typename T, enable_if_t<!is_vector<T>::value, detail::enabler> = detail::dummy>
/// Add option for non-vectors with a default print
template <typename T,
enable_if_t<!is_vector<T>::value && !std::is_const<T>::value, detail::enabler> = detail::dummy>
Option *add_option(std::string option_name,
T &variable, ///< The variable to set
std::string option_description = "") {
std::string option_description = "",
bool defaulted = false) {

CLI::callback_t fun = [&variable](CLI::results_t res) { return detail::lexical_cast(res[0], variable); };

Option *opt = add_option(option_name, fun, option_description, false);
Option *opt = add_option(option_name, fun, option_description, defaulted);
opt->type_name(detail::type_name<T>());
if(defaulted) {
opt->default_str(CLI::detail::to_string(variable));
}
return opt;
}

Expand All @@ -402,6 +407,7 @@ class App {
opt->type_name(detail::type_name<T>());
return opt;
}

/// Add option with no description or variable assignment
Option *add_option(std::string option_name) {
return add_option(option_name, CLI::callback_t(), std::string{}, false);
Expand All @@ -415,55 +421,12 @@ class App {
return add_option(option_name, CLI::callback_t(), option_description, false);
}

/// Add option for non-vectors with a default print
template <typename T,
enable_if_t<!is_vector<T>::value && !std::is_const<T>::value, detail::enabler> = detail::dummy>
Option *add_option(std::string option_name,
T &variable, ///< The variable to set
std::string option_description,
bool defaulted) {

CLI::callback_t fun = [&variable](CLI::results_t res) { return detail::lexical_cast(res[0], variable); };

Option *opt = add_option(option_name, fun, option_description, defaulted);
opt->type_name(detail::type_name<T>());
if(defaulted) {
std::stringstream out;
out << variable;
opt->default_str(out.str());
}
return opt;
}

/// Add option for vectors
template <typename T>
Option *add_option(std::string option_name,
std::vector<T> &variable, ///< The variable vector to set
std::string option_description = "") {

CLI::callback_t fun = [&variable](CLI::results_t res) {
bool retval = true;
variable.clear();
variable.reserve(res.size());
for(const auto &elem : res) {

variable.emplace_back();
retval &= detail::lexical_cast(elem, variable.back());
}
return (!variable.empty()) && retval;
};

Option *opt = add_option(option_name, fun, option_description, false);
opt->type_name(detail::type_name<T>())->type_size(-1);
return opt;
}

/// Add option for vectors with defaulted argument
template <typename T>
Option *add_option(std::string option_name,
std::vector<T> &variable, ///< The variable vector to set
std::string option_description,
bool defaulted) {
std::string option_description = "",
bool defaulted = false) {

CLI::callback_t fun = [&variable](CLI::results_t res) {
bool retval = true;
Expand All @@ -480,8 +443,15 @@ class App {
Option *opt = add_option(option_name, fun, option_description, defaulted);
opt->type_name(detail::type_name<T>())->type_size(-1);

if(defaulted)
opt->default_str("[" + detail::join(variable) + "]");
if(defaulted) {
std::vector<std::string> defaults;
defaults.resize(variable.size());
std::transform(variable.begin(), variable.end(), defaults.begin(), [](T &val) {
return std::string(CLI::detail::to_string(val));
});
opt->default_str("[" + detail::join(defaults) + "]");
}

return opt;
}

Expand Down
1 change: 1 addition & 0 deletions include/CLI/Optional.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
#endif
#if CLI11_BOOST_OPTIONAL
#include <boost/optional.hpp>
#include <boost/optional/optional_io.hpp>
#endif
// [CLI11:verbatim]

Expand Down
10 changes: 8 additions & 2 deletions tests/CreationTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,12 @@ TEST_F(TApp, MakeUnstreamableOptiions) {
Unstreamable value;
app.add_option("--value", value);

// This fails to build, since it tries to stream from Unstreamable
// app.add_option("--value", value, "", false);
// This used to fail to build, since it tries to stream from Unstreamable
app.add_option("--value2", value, "", false);

std::vector<Unstreamable> values;
app.add_option("--values", values);

// This used to fail to build, since it tries to stream from Unstreamable
app.add_option("--values2", values, "", false);
}

0 comments on commit 952bee9

Please sign in to comment.