Skip to content

Commit 36f9993

Browse files
committed
new NoArgsIsHelpError for displaying default help
1 parent 9f7d3db commit 36f9993

4 files changed

Lines changed: 19 additions & 13 deletions

File tree

CHANGES.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,9 @@ Unreleased
7373
- Required arguments with the ``Choice`` type show the choices in
7474
curly braces to indicate that one is required (``{a|b|c}``).
7575
:issue:`1272`
76+
- If help is shown because ``no_args_is_help`` is enabled (default on
77+
for groups, off for commands), the exit code is 1 instead of 0.
78+
:issue:`1489`
7679

7780

7881
Version 7.1.2

src/click/core.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from .exceptions import ClickException
1414
from .exceptions import Exit
1515
from .exceptions import MissingParameter
16+
from .exceptions import NoArgsIsHelpError
1617
from .exceptions import UsageError
1718
from .formatting import HelpFormatter
1819
from .formatting import join_options
@@ -1135,8 +1136,7 @@ def format_epilog(self, ctx, formatter):
11351136

11361137
def parse_args(self, ctx, args):
11371138
if not args and self.no_args_is_help and not ctx.resilient_parsing:
1138-
echo(ctx.get_help(), color=ctx.color)
1139-
ctx.exit()
1139+
raise NoArgsIsHelpError(ctx)
11401140

11411141
parser = self.make_parser(ctx)
11421142
opts, args, param_order = parser.parse_args(args=args)
@@ -1303,8 +1303,7 @@ def format_commands(self, ctx, formatter):
13031303

13041304
def parse_args(self, ctx, args):
13051305
if not args and self.no_args_is_help and not ctx.resilient_parsing:
1306-
echo(ctx.get_help(), color=ctx.color)
1307-
ctx.exit(code=1)
1306+
raise NoArgsIsHelpError(ctx)
13081307

13091308
rest = super().parse_args(ctx, args)
13101309

src/click/exceptions.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,14 @@ class BadArgumentUsage(UsageError):
199199
"""
200200

201201

202+
class NoArgsIsHelpError(UsageError):
203+
def __init__(self, ctx):
204+
super().__init__(ctx.get_help(), ctx=ctx)
205+
206+
def show(self, file=None):
207+
echo(self.format_message(), file=file, err=True, color=self.ctx.color)
208+
209+
202210
class FileError(ClickException):
203211
"""Raised if a file cannot be opened."""
204212

tests/test_commands.py

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,9 @@ def long():
7171
)
7272

7373

74-
def test_no_args_is_help(runner):
75-
@click.command(no_args_is_help=True)
76-
def cli():
77-
pass
78-
79-
result = runner.invoke(cli, [])
80-
assert result.exit_code == 0
74+
def test_command_no_args_is_help(runner):
75+
result = runner.invoke(click.Command("test", no_args_is_help=True))
76+
assert result.exit_code == 2
8177
assert "Show this message and exit." in result.output
8278

8379

@@ -107,8 +103,8 @@ def cli(obj):
107103
def move():
108104
click.echo("move")
109105

110-
result = runner.invoke(cli, [])
111-
assert result.exit_code == 0
106+
result = runner.invoke(cli)
107+
assert result.exit_code == 2
112108
assert "Show this message and exit." in result.output
113109

114110
result = runner.invoke(cli, ["obj1"])

0 commit comments

Comments
 (0)