remove suffixes from generated command name (#2604)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by David Lord · committed by GitHub · parents b63ace2, 1496837 · Browse files · 4 files·+46 added−14 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+46 added−14 removed
- tests:+22 added−1 removed
- other:+24 added−13 removed
Provenance
- Commit
9c5e989- Tree
cbc3d87- Author
David Lord <davidism@gmail.com>- Committer
GitHub <noreply@github.com>- Parent 1
b63ace2- Parent 2
1496837
4 files changed+46 added−14 removed
modified:src/click/decorators.py+21 added-13 removed
176 unmodified lines177 r"""Creates a new :class:`Command` and uses the decorated function as178 callback. This will also automatically attach all decorated179 :func:`option`\s and :func:`argument`\s as parameters to the command.180 181- The name of the command defaults to the name of the function with182- underscores replaced by dashes. If you want to change that, you can183- pass the intended name as the first argument.181+ The name of the command defaults to the name of the function, converted to182+ lowercase, with underscores ``_`` replaced by dashes ``-``, and the suffixes183+ ``_command``, ``_cmd``, ``_group``, and ``_grp`` are removed. For example,184+ ``init_data_command`` becomes ``init-data``.185 186 All keyword arguments are forwarded to the underlying command class.187 For the ``params`` argument, any decorated params are appended to188 the end of the list.1 unmodified line190 Once decorated the function turns into a :class:`Command` instance191 that can be invoked as a command line utility or be attached to a192 command :class:`Group`.193 193- :param name: the name of the command. This defaults to the function194- name with underscores replaced by dashes.195- :param cls: the command class to instantiate. This defaults to196- :class:`Command`.194+ :param name: The name of the command. Defaults to modifying the function's195+ name as described above.196+ :param cls: The command class to create. Defaults to :class:`Command`.197 198+ .. versionchanged:: 8.2199+ The suffixes ``_command``, ``_cmd``, ``_group``, and ``_grp`` are200+ removed when generating the name.201+202 .. versionchanged:: 8.1203 This decorator can be applied without parentheses.204 205 .. versionchanged:: 8.133 unmodified lines239 if t.TYPE_CHECKING:240 assert cls is not None241 assert not callable(name)242 239- cmd = cls(240- name=name or f.__name__.lower().replace("_", "-"),241- callback=f,242- params=params,243- **attrs,244- )243+ if name is not None:244+ cmd_name = name245+ else:246+ cmd_name = f.__name__.lower().replace("_", "-")247+ cmd_left, sep, suffix = cmd_name.rpartition("-")248+249+ if sep and suffix in {"command", "cmd", "group", "grp"}:250+ cmd_name = cmd_left251+252+ cmd = cls(name=cmd_name, callback=f, params=params, **attrs)253 cmd.__doc__ = f.__doc__254 return cmd255 256 if func is not None:307 unmodified linesmodified:tests/test_command_decorators.py+21 added
1+import pytest2+3 import click4 5 6 def test_command_no_parens(runner):63 unmodified lines70 assert cli.params[0].name == "a"71 assert cli.params[1].name == "b"72 result = runner.invoke(cli, ["1", "2"])73 assert result.output == "1 2\n"74+75+76+@pytest.mark.parametrize(77+ "name",78+ [79+ "init_data",80+ "init_data_command",81+ "init_data_cmd",82+ "init_data_group",83+ "init_data_grp",84+ ],85+)86+def test_generate_name(name: str) -> None:87+ def f():88+ pass89+90+ f.__name__ = name91+ f = click.command(f)92+ assert f.name == "init-data"modified:tests/test_commands.py+1 added-1 removed
247 unmodified lines248 249 result = runner.invoke(cli, standalone_mode=False)250 # invoke should type cast default values, str becomes int, empty251 # multiple should be empty tuple instead of None252- assert result.return_value == ("other-cmd", 42, 15, ())252+ assert result.return_value == ("other", 42, 15, ())253 254 255 def test_invoked_subcommand(runner):256 @click.group(invoke_without_command=True)155 unmodified linesmodified:CHANGES.rst+3 added
26 unmodified lines27 - ``split_arg_string`` is moved from ``parser`` to ``shell_completion``.28 29 - Enable deferred evaluation of annotations with30 ``from __future__ import annotations``. :pr:`2270`31+- When generating a command's name from a decorated function's name, the32+ suffixes ``_command``, ``_cmd``, ``_group``, and ``_grp`` are removed.33+ :issue:`2322`34 35 36 Version 8.1.737 -------------1100 unmodified lines