Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: remove duplicated call to subcommand's final_callback #584

Merged
merged 2 commits into from
Jun 24, 2021

Conversation

dryleev
Copy link
Contributor

@dryleev dryleev commented May 6, 2021

Fixes #572.

When parse_complete_callback_ is set there is an extra call to
run_callback() inside the App::_parse(std::vector<std::string>&) method.
This extra call also excessively calls a final_callback_ (when it is
also set) for the command and (since it is recursive) for it's
subcommands.

This commit adds extra boolean parameter for the run_callback() method
allowing to explicitly suppress calling to final_callback_. The value of
this parameter is also propagated to recursive calls to run_callback().

Fixes CLIUtils#572
@codecov
Copy link

codecov bot commented May 6, 2021

Codecov Report

Merging #584 (14a0b29) into master (4af78be) will not change coverage.
The diff coverage is 100.00%.

❗ Current head 14a0b29 differs from pull request most recent head 53124d9. Consider uploading reports for the commit 53124d9 to get more accurate results
Impacted file tree graph

@@            Coverage Diff            @@
##            master      #584   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           12        12           
  Lines         3764      3764           
=========================================
  Hits          3764      3764           
Impacted Files Coverage Δ
include/CLI/App.hpp 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 4af78be...53124d9. Read the comment docs.

@henryiii
Copy link
Collaborator

Nice, thanks! Could you add a little test for this? (I can add it later if not, though it might be able to be merged faster if you add it).

@henryiii henryiii changed the title Fix excessive call to subcommand's final_callback fix: remove excessive call to subcommand's final_callback Jun 15, 2021
@henryiii henryiii changed the title fix: remove excessive call to subcommand's final_callback fix: remove duplicated call to subcommand's final_callback Jun 15, 2021
@henryiii
Copy link
Collaborator

Added tests, and fixed another bug, I think - if this is the main app, the final_callback should always run. Currently it was only running if at least one more subcommand was passed. @phlptp could you verify that this looks correct?

if(final_callback_ && (parsed_ > 0)) {
if(!name_.empty() || count_all() > 0) {
if(final_callback_ && (parsed_ > 0) && (!suppress_final_callback)) {
if(!name_.empty() || count_all() > 0 || parent_ == nullptr) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here, specifically, final_callback wasn't always running on the main app.

run();

CHECK(app_compl == 1);
CHECK(app_final == 1);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without the above fix, this is 0, not 1. The rest of them were correct.

@henryiii henryiii merged commit d0a2aa7 into CLIUtils:master Jun 24, 2021
@github-actions github-actions bot added needs changelog Hasn't been added to the changelog yet needs README Needs to be mentioned in the README labels Jun 24, 2021
@henryiii henryiii added this to the v2.0 milestone Jun 24, 2021
@henryiii henryiii removed needs README Needs to be mentioned in the README needs changelog Hasn't been added to the changelog yet labels Jul 1, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

final_callback of a nested subcommand will be called twice if parse_complete_callback exist
2 participants