Skip to content
Myr

ability to pass custom implementations of ``Option`` and ``Argument``

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

allow passing a different class to ``option`` and ``argument`` decorators via ``cls`` keyword argument.

authored by Sławek Ehlert · committed by Sławek Ehlert · parent 4a7fe69 · Browse files · 2 files+48 added−8 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+48 added−8 removed

  • tests:+32 added−0 removed
  • other:+16 added−8 removed

2 files+48 added−8 removed

modified:click/decorators.py+16 added-8 removed
134 unmodified lines
135136def argument(*param_decls, **attrs):137 """Attaches an argument to the command. All positional arguments are138 passed as parameter declarations to :class:`Argument`; all keyword139 arguments are forwarded unchanged. This is equivalent to creating an140 :class:`Argument` instance manually and attaching it to the141 :attr:`Command.params` list.139 arguments are forwarded unchanged (except ``cls``).140 This is equivalent to creating an :class:`Argument` instance manually141 and attaching it to the :attr:`Command.params` list.142143 :param cls: a custom :class:`Argument` subclass that can be instatiated144 instead of a default one.145 """146 def decorator(f):147 ArgumentClass = attrs.pop('cls', Argument)144 _param_memo(f, Argument(param_decls, **attrs))148 _param_memo(f, ArgumentClass(param_decls, **attrs))149 return f150 return decorator151152153def option(*param_decls, **attrs):154 """Attaches an option to the command. All positional arguments are155 passed as parameter declarations to :class:`Option`; all keyword152 arguments are forwarded unchanged. This is equivalent to creating an153 :class:`Option` instance manually and attaching it to the154 :attr:`Command.params` list.156 arguments are forwarded unchanged (except ``cls``).157 This is equivalent to creating an :class:`Option` instance manually158 and attaching it to the :attr:`Command.params` list.159160 :param cls: a custom :class:`Option` subclass that can be instatiated161 instead of a default one.162 """163 def decorator(f):164 if 'help' in attrs:165 attrs['help'] = inspect.cleandoc(attrs['help'])166 OptionClass = attrs.pop('cls', Option)159 _param_memo(f, Option(param_decls, **attrs))167 _param_memo(f, OptionClass(param_decls, **attrs))168 return f169 return decorator170171
130 unmodified lines
modified:tests/test_options.py+32 added
215 unmodified lines
216 out = result.output.splitlines()217 assert ' --foo TEXT hello' in out218 assert ' i am' in out219 assert ' multiline' in out220221222def test_argument_custom_class(runner):223 class CustomArgument(click.Argument):224 def get_default(self, ctx):225 '''a dumb ovverride of a default value for testing'''226 return 'I am a default'227228 @click.command()229 @click.argument('testarg', cls=CustomArgument, default='you wont see me')230 def cmd(testarg):231 click.echo(testarg)232233 result = runner.invoke(cmd)234 assert 'I am a default' in result.output235 assert 'you wont see me' not in result.output236237238def test_option_custom_class(runner):239 class CustomOption(click.Option):240 def get_help_record(self, ctx):241 '''a dumb ovverride of a help text for testing'''242 return ('--help', 'I am a help text')243244 @click.command()245 @click.option('--testoption', cls=CustomOption, help='you wont see me')246 def cmd(testoption):247 click.echo(testoption)248249 result = runner.invoke(cmd, ['--help'])250 assert 'I am a help text' in result.output251 assert 'you wont see me' not in result.output