fix(core): Ensure exit code 0 when no_args_is_help shows help #1246
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
When
no_args_is_help
is True and no arguments are provided to a Typer application, the help message is displayed. However, an additional empty error panel also currently appears, and the application exits with status code 2. This issue affects applications using Click version 8.2.0 or later.The root cause is a change in Click (pallets/click@d8763b93) where
Command.parse_args
now raisesclick.exceptions.NoArgsIsHelpError
whenno_args_is_help
is true and no arguments are given. Previously, Click would print help and callctx.exit()
directly withinparse_args
.Typer's generic
except click.ClickException as e:
block incore.py._main
catches thisNoArgsIsHelpError
. The subsequent call torich_utils.rich_format_error(e)
(ore.show()
if Rich is not used) results in the help message being displayed. This is becauseNoArgsIsHelpError
is a subclass ofclick.UsageError
, andUsageError.show()
printsctx.get_help()
. However, this process also leads to an attempt to format a non-existent specific error message (asNoArgsIsHelpError.format_message()
is empty), causing the blank error panel. Finally,sys.exit(e.exit_code)
uses theUsageError
's exit code of 2.This commit addresses the issue by checking if the
click.ClickException
is of instanceNoArgsIsHelpError
and then raisingclick.exceptions.Exit(0)
This ensures that after the help message is displayed, the application exits cleanly with status code 0 and without displaying the erroneous empty error panel, aligning with the intended behavior of
no_args_is_help
.Based on discussion from [[https://github.com//pull/1240]]