Skip to content
Myr

remove suffixes from generated command name

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 b63ace2 · 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

4 files+46 added−14 removed

modified:src/click/decorators.py+21 added-13 removed
176 unmodified lines
177 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.180181 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``.185186 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 line
190 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`.193193 :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`.197198 .. versionchanged:: 8.2199 The suffixes ``_command``, ``_cmd``, ``_group``, and ``_grp`` are200 removed when generating the name.201202 .. versionchanged:: 8.1203 This decorator can be applied without parentheses.204205 .. versionchanged:: 8.1
33 unmodified lines
239 if t.TYPE_CHECKING:240 assert cls is not None241 assert not callable(name)242239 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("-")248249 if sep and suffix in {"command", "cmd", "group", "grp"}:250 cmd_name = cmd_left251252 cmd = cls(name=cmd_name, callback=f, params=params, **attrs)253 cmd.__doc__ = f.__doc__254 return cmd255256 if func is not None:
307 unmodified lines
modified:tests/test_command_decorators.py+21 added
1import pytest23import click456def test_command_no_parens(runner):
63 unmodified lines
70 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"747576@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)86def test_generate_name(name: str) -> None:87 def f():88 pass8990 f.__name__ = name91 f = click.command(f)92 assert f.name == "init-data"
modified:tests/test_commands.py+1 added-1 removed
247 unmodified lines
248249 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, ())253254255def test_invoked_subcommand(runner):256 @click.group(invoke_without_command=True)
155 unmodified lines
modified:CHANGES.rst+3 added
26 unmodified lines
27 - ``split_arg_string`` is moved from ``parser`` to ``shell_completion``.2829- 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`343536Version 8.1.737-------------
1100 unmodified lines