Skip to content
Myr

Merge pull request #285 from slafs/optional-param-cls

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

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

authored by Markus Unterwaditzer · committed by Markus Unterwaditzer · parents 3a2585c, 48b8991 · 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: the argument class to instantiate. This defaults to144 :class:`Argument`.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: the option class to instantiate. This defaults to161 :class:`Option`.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
230 unmodified lines
231 out = result.output.splitlines()232 assert ' --foo TEXT hello' in out233 assert ' i am' in out234 assert ' multiline' in out235236237def test_argument_custom_class(runner):238 class CustomArgument(click.Argument):239 def get_default(self, ctx):240 '''a dumb override of a default value for testing'''241 return 'I am a default'242243 @click.command()244 @click.argument('testarg', cls=CustomArgument, default='you wont see me')245 def cmd(testarg):246 click.echo(testarg)247248 result = runner.invoke(cmd)249 assert 'I am a default' in result.output250 assert 'you wont see me' not in result.output251252253def test_option_custom_class(runner):254 class CustomOption(click.Option):255 def get_help_record(self, ctx):256 '''a dumb override of a help text for testing'''257 return ('--help', 'I am a help text')258259 @click.command()260 @click.option('--testoption', cls=CustomOption, help='you wont see me')261 def cmd(testoption):262 click.echo(testoption)263264 result = runner.invoke(cmd, ['--help'])265 assert 'I am a help text' in result.output266 assert 'you wont see me' not in result.output