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
Provenance
- Commit
08583da- Tree
e115f8e- Author
David Lord <davidism@gmail.com>- Committer
GitHub <noreply@github.com>- Parent 1
bf3a945- Parent 2
0786fda
3 files changed+31 added−26 removed
modified:src/click/core.py+26 added-24 removed
38 unmodified lines39 40 SUBCOMMAND_METAVAR = "COMMAND [ARGS]..."41 SUBCOMMANDS_METAVAR = "COMMAND1 [ARGS]... [COMMAND2 [ARGS]...]..."42 43-DEPRECATED_HELP_NOTICE = " (DEPRECATED)"44-DEPRECATED_INVOKE_NOTICE = "DeprecationWarning: The command {name} is deprecated."43 46-47-def _maybe_show_deprecated_notice(cmd):48- if cmd.deprecated:49- echo(style(DEPRECATED_INVOKE_NOTICE.format(name=cmd.name), fg="red"), err=True)50-51-44 def _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 lines1184 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 ""1189 1190+ if not text and self.help:1191+ text = make_default_short_help(self.help, limit)1192+1193+ if self.deprecated:1194+ text = f"(Deprecated) {text}"1195+1196+ return text.strip()1197+1198 def format_help(self, ctx, formatter):1199 """Writes the help into the formatter if it exists.1200 1201 This is a low-level method called by :meth:`get_help`.11 unmodified lines1213 self.format_epilog(ctx, formatter)1214 1215 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 ""1218+1219+ if self.deprecated:1220+ text = f"(Deprecated) {text}"1221+1222+ if text:1223 formatter.write_paragraph()1224+1225 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)1227 1228 def format_options(self, ctx, formatter):1229 """Writes all the options into the formatter if they exist."""1230 opts = []37 unmodified lines1268 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+ )1280+1281 if self.callback is not None:1282 return ctx.invoke(self.callback, **ctx.params)1283 1284 def shell_complete(self, ctx, incomplete):1330 unmodified linesmodified:tests/test_commands.py+2 added-2 removed
295 unmodified lines296 """CLI HELP"""297 pass298 299 result = runner.invoke(cmd_with_help, ["--help"])300- assert "(DEPRECATED)" in result.output300+ assert "(Deprecated)" in result.output301 302 @click.command(deprecated=True)303 def cmd_without_help():304 pass305 306 result = runner.invoke(cmd_without_help, ["--help"])307- assert "(DEPRECATED)" in result.output307+ assert "(Deprecated)" in result.output308 309 310 def test_deprecated_in_invocation(runner):311 @click.command(deprecated=True)5 unmodified linesmodified:CHANGES.rst+3 added
187 unmodified lines188 ``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`195 196 197 Version 7.1.2198 -------------635 unmodified lines