1
0
mirror of https://github.com/CLIUtils/CLI11.git synced 2025-04-29 12:13:52 +00:00

Support empty lambdas

This commit is contained in:
Henry Fredrick Schreiner 2018-06-27 17:55:42 +02:00 committed by Henry Schreiner
parent 9943c0300f
commit a63609102a
2 changed files with 24 additions and 6 deletions

View File

@ -226,12 +226,10 @@ class Option : public OptionBase<Option> {
///@}
/// Making an option by hand is not defined, it must be made by the App class
Option(std::string name,
std::string description = "",
std::function<bool(results_t)> callback = [](results_t) { return true; },
bool default_ = true,
App *parent = nullptr)
: description_(std::move(description)), default_(default_), parent_(parent), callback_(std::move(callback)) {
Option(
std::string name, std::string description, std::function<bool(results_t)> callback, bool default_, App *parent)
: description_(std::move(description)), default_(default_), parent_(parent),
callback_(callback ? std::move(callback) : [](results_t) { return true; }) {
std::tie(snames_, lnames_, pname_) = detail::get_names(detail::split_names(name));
}

View File

@ -1472,3 +1472,23 @@ TEST_F(TApp, RepeatingMultiArgumentOptions) {
args.pop_back();
ASSERT_THROW(run(), CLI::ArgumentMismatch);
}
// #122
TEST_F(TApp, EmptyOptionEach) {
std::string q;
app.add_option("--each", {})->each([&q](std::string s) { q = s; });
args = {"--each", "that"};
run();
EXPECT_EQ(q, "that");
}
// #122
TEST_F(TApp, EmptyOptionFail) {
std::string q;
app.add_option("--each", {});
args = {"--each", "that"};
run();
}