Skip to content

Commit

Permalink
Merge pull request #994 from dsully/issue-926
Browse files Browse the repository at this point in the history
Fix issue #926 - copy option attrs so that custom classes can be re-used.
  • Loading branch information
davidism authored May 14, 2018
2 parents 3cb9973 + a17b544 commit 1ea6a75
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 4 deletions.
11 changes: 7 additions & 4 deletions click/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,10 +164,13 @@ def option(*param_decls, **attrs):
:class:`Option`.
"""
def decorator(f):
if 'help' in attrs:
attrs['help'] = inspect.cleandoc(attrs['help'])
OptionClass = attrs.pop('cls', Option)
_param_memo(f, OptionClass(param_decls, **attrs))
# Issue 926, copy attrs, so pre-defined options can re-use the same cls=
option_attrs = attrs.copy()

if 'help' in option_attrs:
option_attrs['help'] = inspect.cleandoc(option_attrs['help'])
OptionClass = option_attrs.pop('cls', Option)
_param_memo(f, OptionClass(param_decls, **option_attrs))
return f
return decorator

Expand Down
29 changes: 29 additions & 0 deletions tests/test_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,35 @@ def cmd(testoption):
assert 'you wont see me' not in result.output


def test_option_custom_class_reusable(runner):
"""Ensure we can reuse a custom class option. See Issue #926"""

class CustomOption(click.Option):
def get_help_record(self, ctx):
'''a dumb override of a help text for testing'''
return ('--help', 'I am a help text')

# Assign to a variable to re-use the decorator.
testoption = click.option('--testoption', cls=CustomOption, help='you wont see me')

@click.command()
@testoption
def cmd1(testoption):
click.echo(testoption)

@click.command()
@testoption
def cmd2(testoption):
click.echo(testoption)

# Both of the commands should have the --help option now.
for cmd in (cmd1, cmd2):

result = runner.invoke(cmd, ['--help'])
assert 'I am a help text' in result.output
assert 'you wont see me' not in result.output


def test_aliases_for_flags(runner):
@click.command()
@click.option('--warnings/--no-warnings', ' /-W', default=True)
Expand Down

0 comments on commit 1ea6a75

Please sign in to comment.