command decorator params argument
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by David Lord · committed by David Lord · parent f6a681a · Browse files · 3 files·+29 added−3 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+29 added−3 removed
- tests:+14 added−0 removed
- other:+15 added−3 removed
Provenance
- Commit
46b78bc- Tree
f50dc47- Author
David Lord <davidism@gmail.com>- Committer
David Lord <davidism@gmail.com>- Parent
f6a681a
3 files changed+29 added−3 removed
modified:src/click/decorators.py+12 added-3 removed
151 unmodified lines152 underscores replaced by dashes. If you want to change that, you can153 pass the intended name as the first argument.154 155 All keyword arguments are forwarded to the underlying command class.156+ For the ``params`` argument, any decorated params are appended to157+ the end of the list.158 159 Once decorated the function turns into a :class:`Command` instance160 that can be invoked as a command line utility or be attached to a161 command :class:`Group`.4 unmodified lines166 :class:`Command`.167 168 .. versionchanged:: 8.1169 This decorator can be applied without parentheses.170+171+ .. versionchanged:: 8.1172+ The ``params`` argument can be used. Decorated params are173+ appended to the end of the list.174 """175 if cls is None:176 cls = Command177 6 unmodified lines184 def decorator(f: t.Callable[..., t.Any]) -> Command:185 if isinstance(f, Command):186 raise TypeError("Attempted to convert a callback into a command twice.")187 188+ attr_params = attrs.pop("params", None)189+ params = attr_params if attr_params is not None else []190+191 try:183- params = f.__click_params__ # type: ignore192+ decorator_params = f.__click_params__ # type: ignore193 except AttributeError:185- params = []194+ pass195 else:187- params.reverse()196 del f.__click_params__ # type: ignore197+ params.extend(reversed(decorator_params))198 199 if attrs.get("help") is None:200 attrs["help"] = f.__doc__201 285 unmodified linesmodified:tests/test_command_decorators.py+14 added
33 unmodified lines34 35 result = runner.invoke(grp, ["grp2", "cmd2"])36 assert result.exception is None37 assert result.output == "grp1\ngrp2\ncmd2\n"38+39+40+def test_params_argument(runner):41+ opt = click.Argument(["a"])42+43+ @click.command(params=[opt])44+ @click.argument("b")45+ def cli(a, b):46+ click.echo(f"{a} {b}")47+48+ assert cli.params[0].name == "a"49+ assert cli.params[1].name == "b"50+ result = runner.invoke(cli, ["1", "2"])51+ assert result.output == "1 2\n"modified:CHANGES.rst+3 added
38 unmodified lines39 - Allow empty str input for ``prompt()`` when40 ``confirmation_prompt=True`` and ``default=""``. :issue:`2157`41 - Windows glob pattern expansion doesn't fail if a value is an invalid42 pattern. :issue:`2195`43+- It's possible to pass a list of ``params`` to ``@command``. Any44+ params defined with decorators are appended to the passed params.45+ :issue:`2131`.46 47 48 Version 8.0.449 -------------948 unmodified lines