Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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