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
Provenance
- Commit
d8763b9- Tree
29728e6- Author
fdavis <fdavis@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent
4271fe2
4 files changed+20 added−13 removed
modified:src/click/core.py+4 added-5 removed
22 unmodified lines23 from .exceptions import BadParameter24 from .exceptions import ClickException25 from .exceptions import Exit26 from .exceptions import MissingParameter27+from .exceptions import NoArgsIsHelpError28 from .exceptions import UsageError29 from .formatting import HelpFormatter30 from .formatting import join_options31 from .globals import pop_context1124 unmodified lines1156 return ctx1157 1158 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)1161 1162 parser = self.make_parser(ctx)1163 opts, args, param_order = parser.parse_args(args=args)1164 581 unmodified lines1746 formatter.write_dl(rows)1747 1748 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)1751 1752 rest = super().parse_args(ctx, args)1753 1754 if self.chain:94 unmodified lines1849 # 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:]1856 1857 def shell_complete(self, ctx: Context, incomplete: str) -> list[CompletionItem]:1181 unmodified linesmodified:src/click/exceptions.py+9 added
253 unmodified lines254 .. versionadded:: 6.0255 """256 257 258+class NoArgsIsHelpError(UsageError):259+ def __init__(self, ctx: Context) -> None:260+ self.ctx: Context261+ 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+267 class FileError(ClickException):268 """Raised if a file cannot be opened."""269 270 def __init__(self, filename: str, hint: str | None = None) -> None:28 unmodified linesmodified:tests/test_commands.py+4 added-8 removed
93 unmodified lines94 is not None95 )96 97 98-def test_no_args_is_help(runner):99- @click.command(no_args_is_help=True)100- def cli():101- pass102-103- result = runner.invoke(cli, [])104- assert result.exit_code == 098+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 == 2101 assert "Show this message and exit." in result.output102 103 104 def test_default_maps(runner):17 unmodified lines122 [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 )129 def test_group_with_args(runner, args, exit_code, expect):130 @click.group()277 unmodified linesmodified:CHANGES.rst+3 added
54 unmodified lines55 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`62 63 64 Version 8.1.865 -------------1116 unmodified lines