Skip to content
Myr

Merge pull request #1816 from BALaka-18/fix-deprecation-notice

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

Deprecation notice now appears at the top of the help text

authored by David Lord · committed by GitHub · parents bf3a945, 0786fda · Browse files · 3 files+31 added−26 removed
This commit

State

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

Diff stats

3 files+31 added−26 removed

  • tests:+2 added−2 removed
  • other:+29 added−24 removed

3 files+31 added−26 removed

modified:src/click/core.py+26 added-24 removed
38 unmodified lines
3940SUBCOMMAND_METAVAR = "COMMAND [ARGS]..."41SUBCOMMANDS_METAVAR = "COMMAND1 [ARGS]... [COMMAND2 [ARGS]...]..."4243DEPRECATED_HELP_NOTICE = " (DEPRECATED)"44DEPRECATED_INVOKE_NOTICE = "DeprecationWarning: The command {name} is deprecated."434647def _maybe_show_deprecated_notice(cmd):48 if cmd.deprecated:49 echo(style(DEPRECATED_INVOKE_NOTICE.format(name=cmd.name), fg="red"), err=True)505144def _fast_exit(code):45 """Low-level exit that skips Python's cleanup but speeds up exit by46 about 10ms for things like shell completion.47
1136 unmodified lines
1184 def get_short_help_str(self, limit=45):1185 """Gets short help for the command or makes it by shortening the1186 long help string.1187 """1196 return (1197 self.short_help1198 or self.help1199 and make_default_short_help(self.help, limit)1200 or ""1201 )1188 text = self.short_help or ""11891190 if not text and self.help:1191 text = make_default_short_help(self.help, limit)11921193 if self.deprecated:1194 text = f"(Deprecated) {text}"11951196 return text.strip()11971198 def format_help(self, ctx, formatter):1199 """Writes the help into the formatter if it exists.12001201 This is a low-level method called by :meth:`get_help`.
11 unmodified lines
1213 self.format_epilog(ctx, formatter)12141215 def format_help_text(self, ctx, formatter):1216 """Writes the help text to the formatter if it exists."""1222 if self.help:1217 text = self.help or ""12181219 if self.deprecated:1220 text = f"(Deprecated) {text}"12211222 if text:1223 formatter.write_paragraph()12241225 with formatter.indentation():1225 help_text = self.help1226 if self.deprecated:1227 help_text += DEPRECATED_HELP_NOTICE1228 formatter.write_text(help_text)1226 formatter.write_text(text)1229 elif self.deprecated:1230 formatter.write_paragraph()1231 with formatter.indentation():1232 formatter.write_text(DEPRECATED_HELP_NOTICE)12271228 def format_options(self, ctx, formatter):1229 """Writes all the options into the formatter if they exist."""1230 opts = []
37 unmodified lines
1268 def invoke(self, ctx):1269 """Given a context, this invokes the attached callback (if it exists)1270 in the right way.1271 """1278 _maybe_show_deprecated_notice(self)1272 if self.deprecated:1273 echo(1274 style(1275 f"DeprecationWarning: The command {self.name!r} is deprecated.",1276 fg="red",1277 ),1278 err=True,1279 )12801281 if self.callback is not None:1282 return ctx.invoke(self.callback, **ctx.params)12831284 def shell_complete(self, ctx, incomplete):
1330 unmodified lines
modified:tests/test_commands.py+2 added-2 removed
295 unmodified lines
296 """CLI HELP"""297 pass298299 result = runner.invoke(cmd_with_help, ["--help"])300 assert "(DEPRECATED)" in result.output300 assert "(Deprecated)" in result.output301302 @click.command(deprecated=True)303 def cmd_without_help():304 pass305306 result = runner.invoke(cmd_without_help, ["--help"])307 assert "(DEPRECATED)" in result.output307 assert "(Deprecated)" in result.output308309310def test_deprecated_in_invocation(runner):311 @click.command(deprecated=True)
5 unmodified lines
modified:CHANGES.rst+3 added
187 unmodified lines
188 ``Context.meta``. This is useful for extensions using ``meta`` to189 store information. :issue:`1739`190- ``Path`` ``resolve_path`` resolves symlinks on Windows Python < 3.8.191 :issue:`1813`192- Command deprecation notice appears at the start of the help text, as193 well as in the short help. The notice is not in all caps.194 :issue:`1791`195196197Version 7.1.2198-------------
635 unmodified lines