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

Bug fix for issue 369. (#370)

* Bug fix for issue 369.  The default_val call was not resetting the option state after it had executed the callback and reset the results vector, allowing the possibility of an empty results getting passed to some conversions functions.

* add the source and attribution of the new test

* update formatting
This commit is contained in:
Philip Top 2019-12-17 06:17:31 -08:00 committed by Henry Schreiner
parent df7f4f6d1c
commit b979d3a370
2 changed files with 20 additions and 0 deletions

View File

@ -1075,6 +1075,7 @@ class Option : public OptionBase<Option> {
add_result(val);
run_callback();
results_ = std::move(old_results);
current_option_state_ = option_state::parsing;
return this;
}

View File

@ -120,6 +120,25 @@ TEST_F(TApp, EnumCheckedDefualtTransform) {
EXPECT_EQ(app.get_option("--existing")->as<existing>(), existing::abort);
}
// test from https://github.com/CLIUtils/CLI11/issues/369 [Jakub Zakrzewski](https://github.com/jzakrzewski)
TEST_F(TApp, EnumCheckedDefaultTransformCallback) {
enum class existing : int16_t { abort, overwrite, remove };
auto cmd = std::make_shared<CLI::App>("deploys the repository somewhere", "deploy");
cmd->add_option("--existing", "What to do if file already exists in the destination")
->transform(
CLI::CheckedTransformer(std::unordered_map<std::string, existing>{{"abort", existing::abort},
{"overwrite", existing::overwrite},
{"delete", existing::remove},
{"remove", existing::remove}}))
->default_val("abort");
cmd->callback([cmd]() { EXPECT_EQ(cmd->get_option("--existing")->as<existing>(), existing::abort); });
app.add_subcommand(cmd);
args = {"deploy"};
run();
}
TEST_F(TApp, SimpleTransformFn) {
int value;
auto opt = app.add_option("-s", value)->transform(CLI::Transformer({{"one", "1"}}, CLI::ignore_case));