Skip to content

Commit d8763b9

Browse files
fdavisdavidismAndreasBackx
authored
Help shown via no_args_is_help results in exit code 2, was 0 (#1489)
Co-authored-by: David Lord <[email protected]> Co-authored-by: Andreas Backx <[email protected]>
1 parent 4271fe2 commit d8763b9

4 files changed

Lines changed: 20 additions & 13 deletions

File tree

CHANGES.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ Unreleased
5656
:issue:`2746` :pr:`2788`
5757
- Add ``Choice.get_invalid_choice_message`` method for customizing the
5858
invalid choice message. :issue:`2621` :pr:`2622`
59+
- If help is shown because ``no_args_is_help`` is enabled (defaults to ``True``
60+
for groups, ``False`` for commands), the exit code is 2 instead of 0.
61+
:issue:`1489` :pr:`1489`
5962

6063

6164
Version 8.1.8

src/click/core.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
from .exceptions import ClickException
2525
from .exceptions import Exit
2626
from .exceptions import MissingParameter
27+
from .exceptions import NoArgsIsHelpError
2728
from .exceptions import UsageError
2829
from .formatting import HelpFormatter
2930
from .formatting import join_options
@@ -1156,8 +1157,7 @@ def make_context(
11561157

11571158
def parse_args(self, ctx: Context, args: list[str]) -> list[str]:
11581159
if not args and self.no_args_is_help and not ctx.resilient_parsing:
1159-
echo(ctx.get_help(), color=ctx.color)
1160-
ctx.exit()
1160+
raise NoArgsIsHelpError(ctx)
11611161

11621162
parser = self.make_parser(ctx)
11631163
opts, args, param_order = parser.parse_args(args=args)
@@ -1747,8 +1747,7 @@ def format_commands(self, ctx: Context, formatter: HelpFormatter) -> None:
17471747

17481748
def parse_args(self, ctx: Context, args: list[str]) -> list[str]:
17491749
if not args and self.no_args_is_help and not ctx.resilient_parsing:
1750-
echo(ctx.get_help(), color=ctx.color)
1751-
ctx.exit()
1750+
raise NoArgsIsHelpError(ctx)
17521751

17531752
rest = super().parse_args(ctx, args)
17541753

@@ -1851,7 +1850,7 @@ def resolve_command(
18511850
# place.
18521851
if cmd is None and not ctx.resilient_parsing:
18531852
if _split_opt(cmd_name)[0]:
1854-
self.parse_args(ctx, ctx.args)
1853+
self.parse_args(ctx, args)
18551854
ctx.fail(_("No such command {name!r}.").format(name=original_cmd_name))
18561855
return cmd_name if cmd else None, cmd, args[1:]
18571856

src/click/exceptions.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,15 @@ class BadArgumentUsage(UsageError):
255255
"""
256256

257257

258+
class NoArgsIsHelpError(UsageError):
259+
def __init__(self, ctx: Context) -> None:
260+
self.ctx: Context
261+
super().__init__(ctx.get_help(), ctx=ctx)
262+
263+
def show(self, file: t.IO[t.Any] | None = None) -> None:
264+
echo(self.format_message(), file=file, err=True, color=self.ctx.color)
265+
266+
258267
class FileError(ClickException):
259268
"""Raised if a file cannot be opened."""
260269

tests/test_commands.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -95,13 +95,9 @@ def long():
9595
)
9696

9797

98-
def test_no_args_is_help(runner):
99-
@click.command(no_args_is_help=True)
100-
def cli():
101-
pass
102-
103-
result = runner.invoke(cli, [])
104-
assert result.exit_code == 0
98+
def test_command_no_args_is_help(runner):
99+
result = runner.invoke(click.Command("test", no_args_is_help=True))
100+
assert result.exit_code == 2
105101
assert "Show this message and exit." in result.output
106102

107103

@@ -127,7 +123,7 @@ def foo(name):
127123
(["obj1"], 2, "Error: Missing command."),
128124
(["obj1", "--help"], 0, "Show this message and exit."),
129125
(["obj1", "move"], 0, "obj=obj1\nmove\n"),
130-
([], 0, "Show this message and exit."),
126+
([], 2, "Show this message and exit."),
131127
],
132128
)
133129
def test_group_with_args(runner, args, exit_code, expect):

0 commit comments

Comments
 (0)