Skip to content

fix(core): Ensure exit code 0 when no_args_is_help shows help #1240

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

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

def test_no_arg():
result = runner.invoke(app)
assert result.exit_code == 0
assert "[OPTIONS] COMMAND [ARGS]..." in result.output
assert "Commands" in result.output
assert "create" in result.output
Expand Down
12 changes: 12 additions & 0 deletions typer/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,19 @@ def _main(
raise click.Abort() from e
except KeyboardInterrupt as e:
raise click.exceptions.Exit(130) from e
# TODO: When deprecating Click < 8.2 uncomment the next two lines
# except click.exceptions.NoArgsIsHelpError as e:
# raise click.exceptions.Exit(0) from e
except click.ClickException as e:
# TODO: When deprecating Click < 8.2 remove this section [start]
_no_args_is_help_error = getattr(
Copy link

@diablodale diablodale Jun 9, 2025

Choose a reason for hiding this comment

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

Rather than probing attrs and instance, I suggest using the exception we want to catch.
Click's init.py doesn't pull in the NoArgsIsHelpError exception into itself. So instead, we can easily do the same at the top of this file with;

from click.exceptions import NoArgsIsHelpError

and then here simply the code to be

...
except KeyboardInterrupt as e:
    raise click.exceptions.Exit(130) from e
except NoArgsIsHelpError as e:
    raise click.exceptions.Exit(0) from e
except click.ClickException as e:
...

click.exceptions, "NoArgsIsHelpError", None
)
if _no_args_is_help_error is not None and isinstance(
e, _no_args_is_help_error
):
raise click.exceptions.Exit(0) from e
# TODO: When deprecating Click < 8.2 remove this section [end]
if not standalone_mode:
raise
# Typer override
Expand Down
Loading