Skip to content
Myr

Merge pull request #1005 from pallets/deprecated-flag

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

Rebase Added deprecation flag to commands

authored by LokiHokie · committed by GitHub · parents fb0a7a7, 85d5076 · Browse files · 2 files+42 added−53 removed
This commit

State

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

Diff stats

2 files+42 added−53 removed

  • tests:+16 added−50 removed
  • other:+26 added−3 removed

2 files+42 added−53 removed

modified:click/core.py+26 added-3 removed
8 unmodified lines
9from .types import convert_type, IntRange, BOOL10from .utils import make_str, make_default_short_help, echo, get_os_args11from .exceptions import ClickException, UsageError, BadParameter, Abort, \12 MissingParameter13from .termui import prompt, confirm13from .termui import prompt, confirm, style14from .formatting import HelpFormatter, join_options15from .parser import OptionParser, split_opt16from .globals import push_context, pop_context17
6 unmodified lines
2425SUBCOMMAND_METAVAR = 'COMMAND [ARGS]...'26SUBCOMMANDS_METAVAR = 'COMMAND1 [ARGS]... [COMMAND2 [ARGS]...]...'2728DEPRECATED_HELP_NOTICE = ' (DEPRECATED)'29DEPRECATED_INVOKE_NOTICE = 'DeprecationWarning: ' + \30 'The command %(name)s is deprecated.'313233def _maybe_show_deprecated_notice(cmd):34 if cmd.deprecated:35 echo(style(DEPRECATED_INVOKE_NOTICE % {'name': cmd.name}, fg='red'), err=True)363738def fast_exit(code):39 """Exit without garbage collection, this speeds up exit by about 10ms for40 things like bash completion.41 """
718 unmodified lines
760 shown on the command listing of the parent command.761 :param add_help_option: by default each command registers a ``--help``762 option. This can be disabled by this parameter.763 :param hidden: hide this command from help outputs.764765 :param deprecated: issues a message indicating that766 the command is deprecated.767 """768769 def __init__(self, name, context_settings=None, callback=None,770 params=None, help=None, epilog=None, short_help=None,771 options_metavar='[OPTIONS]', add_help_option=True,760 hidden=False):772 hidden=False, deprecated=False):773 BaseCommand.__init__(self, name, context_settings)774 #: the callback to execute when the command fires. This might be775 #: `None` in which case nothing happens.776 self.callback = callback
13 unmodified lines
790 short_help = make_default_short_help(help)791 self.short_help = short_help792 self.add_help_option = add_help_option793 self.hidden = hidden794 self.deprecated = deprecated795796 def get_usage(self, ctx):797 formatter = ctx.make_formatter()798 self.format_usage(ctx, formatter)
77 unmodified lines
876 """Writes the help text to the formatter if it exists."""877 if self.help:878 formatter.write_paragraph()879 with formatter.indentation():880 help_text = self.help881 if self.deprecated:882 help_text += DEPRECATED_HELP_NOTICE867 formatter.write_text(self.help)883 formatter.write_text(help_text)884 elif self.deprecated:885 formatter.write_paragraph()886 with formatter.indentation():887 formatter.write_text(DEPRECATED_HELP_NOTICE)888889 def format_options(self, ctx, formatter):890 """Writes all the options into the formatter if they exist."""891 opts = []
32 unmodified lines
924 def invoke(self, ctx):925 """Given a context, this invokes the attached callback (if it exists)926 in the right way.927 """928 _maybe_show_deprecated_notice(self)929 if self.callback is not None:930 return ctx.invoke(self.callback, **ctx.params)931932
107 unmodified lines
1040 if cmd.hidden:1041 continue10421043 help = cmd.short_help or ''1044 if cmd.deprecated:1045 help += DEPRECATED_HELP_NOTICE1046 rows.append((subcommand, help))10471048 if rows:1049 with formatter.section('Commands'):
778 unmodified lines
modified:tests/test_commands.py+16 added-50 removed
261 unmodified lines
262 'Args: -foo|-x|--muhaha|x|y|-x',263 ]264265266def test_subcommand_naming(runner):267 @click.group()268 def cli():266def test_deprecated_in_help_messages(runner):267 @click.command(deprecated=True)268 def cmd_with_help():269 """CLI HELP"""270 pass271271 @cli.command()272 def foo_bar():273 click.echo('foo-bar')272 result = runner.invoke(cmd_with_help, ['--help'])273 assert '(DEPRECATED)' in result.output274275 result = runner.invoke(cli, ['foo-bar'])276 assert not result.exception277 assert result.output.splitlines() == ['foo-bar']278279280def test_environment_variables(runner):281 @click.group()282 def cli():275 @click.command(deprecated=True)276 def cmd_without_help():277 pass278285 @cli.command()286 @click.option('--name', envvar='CLICK_NAME')287 def foo(name):288 click.echo(name)279 result = runner.invoke(cmd_without_help, ['--help'])280 assert '(DEPRECATED)' in result.output281290 result = runner.invoke(cli, ['foo'], env={'CLICK_NAME': 'environment'})282292 assert not result.exception293 assert result.output == 'environment\n'283def test_deprecated_in_invocation(runner):284 @click.command(deprecated=True)285 def deprecated_cmd():286 debug()287295296# Ensures the variables are read in the following order:297# 1. CLI298# 2. Environment299# 3. Defaults300variable_precedence_testdata = [301 (['foo', '--name=cli'], {'CLICK_NAME': 'environment'}, 'cli\n'),302 (['foo'], {'CLICK_NAME': 'environment'}, 'environment\n'),303 (['foo'], None, 'defaults\n'),304]305306307@pytest.mark.parametrize("command,environment,expected",308 variable_precedence_testdata)309def test_variable_precendence_00(runner, command, environment, expected):310 @click.group()311 def cli():312 pass313314 @cli.command()315 @click.option('--name', envvar='CLICK_NAME')316 def foo(name):317 click.echo(name)318319 defaults = {'foo': {'name': 'defaults'}}320 result = runner.invoke(cli, command, default_map=defaults, env=environment)321322 assert not result.exception323 assert result.output == expected288 result = runner.invoke(deprecated_cmd)289 assert 'DeprecationWarning:' in result.output