Skip to content
Myr

Fix issue #926 - copy option attrs so that custom classes can be re-used.

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

authored by Dan Sully · committed by Dan Sully · parent 9c456ec · Browse files · 2 files+36 added−4 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+36 added−4 removed

  • tests:+29 added−0 removed
  • other:+7 added−4 removed

2 files+36 added−4 removed

modified:click/decorators.py+7 added-4 removed
162 unmodified lines
163 :param cls: the option class to instantiate. This defaults to164 :class:`Option`.165 """166 def decorator(f):167 # Issue 926, copy attrs, so pre-defined options can re-use the same cls=168 option_attrs = attrs.copy()169167 if 'help' in attrs:168 attrs['help'] = inspect.cleandoc(attrs['help'])169 OptionClass = attrs.pop('cls', Option)170 _param_memo(f, OptionClass(param_decls, **attrs))170 if 'help' in option_attrs:171 option_attrs['help'] = inspect.cleandoc(option_attrs['help'])172 OptionClass = option_attrs.pop('cls', Option)173 _param_memo(f, OptionClass(param_decls, **option_attrs))174 return f175 return decorator176177
134 unmodified lines
modified:tests/test_options.py+29 added
309 unmodified lines
310 assert 'I am a help text' in result.output311 assert 'you wont see me' not in result.output312313314def test_option_custom_class_reusable(runner):315 """Ensure we can reuse a custom class option. See Issue #926"""316317 class CustomOption(click.Option):318 def get_help_record(self, ctx):319 '''a dumb override of a help text for testing'''320 return ('--help', 'I am a help text')321322 # Assign to a variable to re-use the decorator.323 testoption = click.option('--testoption', cls=CustomOption, help='you wont see me')324325 @click.command()326 @testoption327 def cmd1(testoption):328 click.echo(testoption)329330 @click.command()331 @testoption332 def cmd2(testoption):333 click.echo(testoption)334335 # Both of the commands should have the --help option now.336 for cmd in (cmd1, cmd2):337338 result = runner.invoke(cmd, ['--help'])339 assert 'I am a help text' in result.output340 assert 'you wont see me' not in result.output341342343def test_aliases_for_flags(runner):344 @click.command()345 @click.option('--warnings/--no-warnings', ' /-W', default=True)346 def cli(warnings):
46 unmodified lines