Skip to content
Myr

Help shown via no_args_is_help results in exit code 2, was 0 (#1489)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by fdavis · committed by GitHub · parent 4271fe2 · Browse files · 4 files+20 added−13 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Trailers

  • Co-authored-by: David Lord <davidism@gmail.com>
  • Co-authored-by: Andreas Backx <andreas@backx.org>

Diff stats

4 files+20 added−13 removed

  • tests:+4 added−8 removed
  • other:+16 added−5 removed

4 files+20 added−13 removed

modified:src/click/core.py+4 added-5 removed
22 unmodified lines
23from .exceptions import BadParameter24from .exceptions import ClickException25from .exceptions import Exit26from .exceptions import MissingParameter27from .exceptions import NoArgsIsHelpError28from .exceptions import UsageError29from .formatting import HelpFormatter30from .formatting import join_options31from .globals import pop_context
1124 unmodified lines
1156 return ctx11571158 def parse_args(self, ctx: Context, args: list[str]) -> list[str]:1159 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)11611162 parser = self.make_parser(ctx)1163 opts, args, param_order = parser.parse_args(args=args)1164
581 unmodified lines
1746 formatter.write_dl(rows)17471748 def parse_args(self, ctx: Context, args: list[str]) -> list[str]:1749 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)17511752 rest = super().parse_args(ctx, args)17531754 if self.chain:
94 unmodified lines
1849 # resolve things like --help which now should go to the main1850 # place.1851 if cmd is None and not ctx.resilient_parsing:1852 if _split_opt(cmd_name)[0]:1854 self.parse_args(ctx, ctx.args)1853 self.parse_args(ctx, args)1854 ctx.fail(_("No such command {name!r}.").format(name=original_cmd_name))1855 return cmd_name if cmd else None, cmd, args[1:]18561857 def shell_complete(self, ctx: Context, incomplete: str) -> list[CompletionItem]:
1181 unmodified lines
modified:src/click/exceptions.py+9 added
253 unmodified lines
254 .. versionadded:: 6.0255 """256257258class NoArgsIsHelpError(UsageError):259 def __init__(self, ctx: Context) -> None:260 self.ctx: Context261 super().__init__(ctx.get_help(), ctx=ctx)262263 def show(self, file: t.IO[t.Any] | None = None) -> None:264 echo(self.format_message(), file=file, err=True, color=self.ctx.color)265266267class FileError(ClickException):268 """Raised if a file cannot be opened."""269270 def __init__(self, filename: str, hint: str | None = None) -> None:
28 unmodified lines
modified:tests/test_commands.py+4 added-8 removed
93 unmodified lines
94 is not None95 )969798def test_no_args_is_help(runner):99 @click.command(no_args_is_help=True)100 def cli():101 pass102103 result = runner.invoke(cli, [])104 assert result.exit_code == 098def test_command_no_args_is_help(runner):99 result = runner.invoke(click.Command("test", no_args_is_help=True))100 assert result.exit_code == 2101 assert "Show this message and exit." in result.output102103104def test_default_maps(runner):
17 unmodified lines
122 [123 (["obj1"], 2, "Error: Missing command."),124 (["obj1", "--help"], 0, "Show this message and exit."),125 (["obj1", "move"], 0, "obj=obj1\nmove\n"),130 ([], 0, "Show this message and exit."),126 ([], 2, "Show this message and exit."),127 ],128)129def test_group_with_args(runner, args, exit_code, expect):130 @click.group()
277 unmodified lines
modified:CHANGES.rst+3 added
54 unmodified lines
55 will always be used instead of the value of the environment variable.56 :issue:`2746` :pr:`2788`57- Add ``Choice.get_invalid_choice_message`` method for customizing the58 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`626364Version 8.1.865-------------
1116 unmodified lines