Commit b979d3a37098da7ad6291d9bd3c4fdec3a705043
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
Showing
2 changed files
with
20 additions
and
0 deletions
include/CLI/Option.hpp
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)); | ... | ... |