Commit de215ef978104a0e9efdc7b78a9d58cd529cf17a
Committed by
GitHub
1 parent
efbfd460
Incorrect subcommand callback trigger (#733)
* fix the issue where subcommand callbacks would be triggered multiple times if specified as configurable. * style: pre-commit.ci fixes Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Showing
2 changed files
with
20 additions
and
1 deletions
include/CLI/App.hpp
| @@ -2413,7 +2413,7 @@ class App { | @@ -2413,7 +2413,7 @@ class App { | ||
| 2413 | } | 2413 | } |
| 2414 | // check for section close | 2414 | // check for section close |
| 2415 | if(item.name == "--") { | 2415 | if(item.name == "--") { |
| 2416 | - if(configurable_) { | 2416 | + if(configurable_ && parse_complete_callback_) { |
| 2417 | _process_callbacks(); | 2417 | _process_callbacks(); |
| 2418 | _process_requirements(); | 2418 | _process_requirements(); |
| 2419 | run_callback(); | 2419 | run_callback(); |
tests/ConfigFileTest.cpp
| @@ -1581,6 +1581,25 @@ TEST_CASE_METHOD(TApp, "DuplicateSubcommandCallbacks", "[config]") { | @@ -1581,6 +1581,25 @@ TEST_CASE_METHOD(TApp, "DuplicateSubcommandCallbacks", "[config]") { | ||
| 1581 | CHECK(3 == count); | 1581 | CHECK(3 == count); |
| 1582 | } | 1582 | } |
| 1583 | 1583 | ||
| 1584 | +TEST_CASE_METHOD(TApp, "SubcommandCallbackSingle", "[config]") { | ||
| 1585 | + | ||
| 1586 | + TempFile tmptoml{"Testtomlcallback.toml"}; | ||
| 1587 | + | ||
| 1588 | + app.set_config("--config", tmptoml); | ||
| 1589 | + | ||
| 1590 | + { | ||
| 1591 | + std::ofstream out{tmptoml}; | ||
| 1592 | + out << "[foo]" << std::endl; | ||
| 1593 | + } | ||
| 1594 | + int count{0}; | ||
| 1595 | + auto *foo = app.add_subcommand("foo"); | ||
| 1596 | + foo->configurable(); | ||
| 1597 | + foo->callback([&count]() { ++count; }); | ||
| 1598 | + | ||
| 1599 | + run(); | ||
| 1600 | + CHECK(1 == count); | ||
| 1601 | +} | ||
| 1602 | + | ||
| 1584 | TEST_CASE_METHOD(TApp, "IniFailure", "[config]") { | 1603 | TEST_CASE_METHOD(TApp, "IniFailure", "[config]") { |
| 1585 | 1604 | ||
| 1586 | TempFile tmpini{"TestIniTmp.ini"}; | 1605 | TempFile tmpini{"TestIniTmp.ini"}; |