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
Provenance
- Commit
1c3dfac- Tree
e95e8b1- Author
Markus Unterwaditzer <markus@unterwaditzer.net>- Committer
Markus Unterwaditzer <markus@unterwaditzer.net>- Parent 1
3a2585c- Parent 2
48b8991
2 files changed+48 added−8 removed
modified:click/decorators.py+16 added-8 removed
134 unmodified lines135 136 def 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.142+143+ :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 decorator151 152 153 def 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.159+160+ :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 decorator170 171 130 unmodified linesmodified:tests/test_options.py+32 added
230 unmodified lines231 out = result.output.splitlines()232 assert ' --foo TEXT hello' in out233 assert ' i am' in out234 assert ' multiline' in out235+236+237+def 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'242+243+ @click.command()244+ @click.argument('testarg', cls=CustomArgument, default='you wont see me')245+ def cmd(testarg):246+ click.echo(testarg)247+248+ result = runner.invoke(cmd)249+ assert 'I am a default' in result.output250+ assert 'you wont see me' not in result.output251+252+253+def 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')258+259+ @click.command()260+ @click.option('--testoption', cls=CustomOption, help='you wont see me')261+ def cmd(testoption):262+ click.echo(testoption)263+264+ result = runner.invoke(cmd, ['--help'])265+ assert 'I am a help text' in result.output266+ assert 'you wont see me' not in result.output