Skip to content
Myr

Merge pull request #2203 from pallets/command-decorator-params

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

command decorator params argument

authored by David Lord · committed by GitHub · parents f6a681a, 46b78bc · 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

3 files+29 added−3 removed

modified:src/click/decorators.py+12 added-3 removed
151 unmodified lines
152 underscores replaced by dashes. If you want to change that, you can153 pass the intended name as the first argument.154155 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.158159 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 lines
166 :class:`Command`.167168 .. versionchanged:: 8.1169 This decorator can be applied without parentheses.170171 .. 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 lines
184 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.")187188 attr_params = attrs.pop("params", None)189 params = attr_params if attr_params is not None else []190191 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))198199 if attrs.get("help") is None:200 attrs["help"] = f.__doc__201
285 unmodified lines
modified:tests/test_command_decorators.py+14 added
33 unmodified lines
3435 result = runner.invoke(grp, ["grp2", "cmd2"])36 assert result.exception is None37 assert result.output == "grp1\ngrp2\ncmd2\n"383940def test_params_argument(runner):41 opt = click.Argument(["a"])4243 @click.command(params=[opt])44 @click.argument("b")45 def cli(a, b):46 click.echo(f"{a} {b}")4748 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 lines
39- 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`.464748Version 8.0.449-------------
948 unmodified lines