Commit b979d3a37098da7ad6291d9bd3c4fdec3a705043

Authored by Philip Top
Committed by Henry Schreiner
1 parent df7f4f6d

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
include/CLI/Option.hpp
... ... @@ -1075,6 +1075,7 @@ class Option : public OptionBase<Option> {
1075 1075 add_result(val);
1076 1076 run_callback();
1077 1077 results_ = std::move(old_results);
  1078 + current_option_state_ = option_state::parsing;
1078 1079 return this;
1079 1080 }
1080 1081  
... ...
tests/TransformTest.cpp
... ... @@ -120,6 +120,25 @@ TEST_F(TApp, EnumCheckedDefualtTransform) {
120 120 EXPECT_EQ(app.get_option("--existing")->as<existing>(), existing::abort);
121 121 }
122 122  
  123 +// test from https://github.com/CLIUtils/CLI11/issues/369 [Jakub Zakrzewski](https://github.com/jzakrzewski)
  124 +TEST_F(TApp, EnumCheckedDefaultTransformCallback) {
  125 + enum class existing : int16_t { abort, overwrite, remove };
  126 + auto cmd = std::make_shared<CLI::App>("deploys the repository somewhere", "deploy");
  127 + cmd->add_option("--existing", "What to do if file already exists in the destination")
  128 + ->transform(
  129 + CLI::CheckedTransformer(std::unordered_map<std::string, existing>{{"abort", existing::abort},
  130 + {"overwrite", existing::overwrite},
  131 + {"delete", existing::remove},
  132 + {"remove", existing::remove}}))
  133 + ->default_val("abort");
  134 +
  135 + cmd->callback([cmd]() { EXPECT_EQ(cmd->get_option("--existing")->as<existing>(), existing::abort); });
  136 + app.add_subcommand(cmd);
  137 +
  138 + args = {"deploy"};
  139 + run();
  140 +}
  141 +
123 142 TEST_F(TApp, SimpleTransformFn) {
124 143 int value;
125 144 auto opt = app.add_option("-s", value)->transform(CLI::Transformer({{"one", "1"}}, CLI::ignore_case));
... ...