Added deprecation flag to commands
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by FooBarQuaxx · committed by LokiHokie · parent a00e018 · 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
Provenance
- Commit
85d5076- Tree
a726750- Author
FooBarQuaxx <mabidi6@gmail.com>- Committer
LokiHokie <wpifer3@gatech.edu>- Parent
a00e018
2 files changed+42 added−53 removed
modified:click/core.py+26 added-3 removed
8 unmodified lines9 from .types import convert_type, IntRange, BOOL10 from .utils import make_str, make_default_short_help, echo, get_os_args11 from .exceptions import ClickException, UsageError, BadParameter, Abort, \12 MissingParameter13-from .termui import prompt, confirm13+from .termui import prompt, confirm, style14 from .formatting import HelpFormatter, join_options15 from .parser import OptionParser, split_opt16 from .globals import push_context, pop_context17 6 unmodified lines24 25 SUBCOMMAND_METAVAR = 'COMMAND [ARGS]...'26 SUBCOMMANDS_METAVAR = 'COMMAND1 [ARGS]... [COMMAND2 [ARGS]...]...'27 28+DEPRECATED_HELP_NOTICE = ' (DEPRECATED)'29+DEPRECATED_INVOKE_NOTICE = 'DeprecationWarning: ' + \30+ 'The command %(name)s is deprecated.'31 32+33+def _maybe_show_deprecated_notice(cmd):34+ if cmd.deprecated:35+ echo(style(DEPRECATED_INVOKE_NOTICE % {'name': cmd.name}, fg='red'), err=True)36+37+38 def fast_exit(code):39 """Exit without garbage collection, this speeds up exit by about 10ms for40 things like bash completion.41 """718 unmodified lines760 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.764+765+ :param deprecated: issues a message indicating that766+ the command is deprecated.767 """768 769 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 = callback13 unmodified lines790 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 = deprecated795 796 def get_usage(self, ctx):797 formatter = ctx.make_formatter()798 self.format_usage(ctx, formatter)77 unmodified lines876 """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)888 889 def format_options(self, ctx, formatter):890 """Writes all the options into the formatter if they exist."""891 opts = []32 unmodified lines924 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)931 932 107 unmodified lines1040 if cmd.hidden:1041 continue1042 1043 help = cmd.short_help or ''1044+ if cmd.deprecated:1045+ help += DEPRECATED_HELP_NOTICE1046 rows.append((subcommand, help))1047 1048 if rows:1049 with formatter.section('Commands'):778 unmodified linesmodified:tests/test_commands.py+16 added-50 removed
261 unmodified lines262 'Args: -foo|-x|--muhaha|x|y|-x',263 ]264 265 266-def test_subcommand_naming(runner):267- @click.group()268- def cli():266+def test_deprecated_in_help_messages(runner):267+ @click.command(deprecated=True)268+ def cmd_with_help():269+ """CLI HELP"""270 pass271 271- @cli.command()272- def foo_bar():273- click.echo('foo-bar')272+ result = runner.invoke(cmd_with_help, ['--help'])273+ assert '(DEPRECATED)' in result.output274 275- result = runner.invoke(cli, ['foo-bar'])276- assert not result.exception277- assert result.output.splitlines() == ['foo-bar']278-279-280-def test_environment_variables(runner):281- @click.group()282- def cli():275+ @click.command(deprecated=True)276+ def cmd_without_help():277 pass278 285- @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.output281 290- result = runner.invoke(cli, ['foo'], env={'CLICK_NAME': 'environment'})282 292- assert not result.exception293- assert result.output == 'environment\n'283+def test_deprecated_in_invocation(runner):284+ @click.command(deprecated=True)285+ def deprecated_cmd():286+ debug()287 295-296-# Ensures the variables are read in the following order:297-# 1. CLI298-# 2. Environment299-# 3. Defaults300-variable_precedence_testdata = [301- (['foo', '--name=cli'], {'CLICK_NAME': 'environment'}, 'cli\n'),302- (['foo'], {'CLICK_NAME': 'environment'}, 'environment\n'),303- (['foo'], None, 'defaults\n'),304-]305-306-307-@pytest.mark.parametrize("command,environment,expected",308- variable_precedence_testdata)309-def test_variable_precendence_00(runner, command, environment, expected):310- @click.group()311- def cli():312- pass313-314- @cli.command()315- @click.option('--name', envvar='CLICK_NAME')316- def foo(name):317- click.echo(name)318-319- defaults = {'foo': {'name': 'defaults'}}320- result = runner.invoke(cli, command, default_map=defaults, env=environment)321-322- assert not result.exception323- assert result.output == expected288+ result = runner.invoke(deprecated_cmd)289+ assert 'DeprecationWarning:' in result.output