Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove suffixes from generated command name #2604

Merged
merged 1 commit into from
Sep 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ Unreleased

- Enable deferred evaluation of annotations with
``from __future__ import annotations``. :pr:`2270`
- When generating a command's name from a decorated function's name, the
suffixes ``_command``, ``_cmd``, ``_group``, and ``_grp`` are removed.
:issue:`2322`


Version 8.1.7
Expand Down
34 changes: 21 additions & 13 deletions src/click/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,9 +178,10 @@ def command(
callback. This will also automatically attach all decorated
:func:`option`\s and :func:`argument`\s as parameters to the command.

The name of the command defaults to the name of the function with
underscores replaced by dashes. If you want to change that, you can
pass the intended name as the first argument.
The name of the command defaults to the name of the function, converted to
lowercase, with underscores ``_`` replaced by dashes ``-``, and the suffixes
``_command``, ``_cmd``, ``_group``, and ``_grp`` are removed. For example,
``init_data_command`` becomes ``init-data``.

All keyword arguments are forwarded to the underlying command class.
For the ``params`` argument, any decorated params are appended to
Expand All @@ -190,10 +191,13 @@ def command(
that can be invoked as a command line utility or be attached to a
command :class:`Group`.

:param name: the name of the command. This defaults to the function
name with underscores replaced by dashes.
:param cls: the command class to instantiate. This defaults to
:class:`Command`.
:param name: The name of the command. Defaults to modifying the function's
name as described above.
:param cls: The command class to create. Defaults to :class:`Command`.

.. versionchanged:: 8.2
The suffixes ``_command``, ``_cmd``, ``_group``, and ``_grp`` are
removed when generating the name.

.. versionchanged:: 8.1
This decorator can be applied without parentheses.
Expand Down Expand Up @@ -236,12 +240,16 @@ def decorator(f: _AnyCallable) -> CmdType:
assert cls is not None
assert not callable(name)

cmd = cls(
name=name or f.__name__.lower().replace("_", "-"),
callback=f,
params=params,
**attrs,
)
if name is not None:
cmd_name = name
else:
cmd_name = f.__name__.lower().replace("_", "-")
cmd_left, sep, suffix = cmd_name.rpartition("-")

if sep and suffix in {"command", "cmd", "group", "grp"}:
cmd_name = cmd_left

cmd = cls(name=cmd_name, callback=f, params=params, **attrs)
cmd.__doc__ = f.__doc__
return cmd

Expand Down
21 changes: 21 additions & 0 deletions tests/test_command_decorators.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import pytest

import click


Expand Down Expand Up @@ -69,3 +71,22 @@ def cli(a, b):
assert cli.params[1].name == "b"
result = runner.invoke(cli, ["1", "2"])
assert result.output == "1 2\n"


@pytest.mark.parametrize(
"name",
[
"init_data",
"init_data_command",
"init_data_cmd",
"init_data_group",
"init_data_grp",
],
)
def test_generate_name(name: str) -> None:
def f():
pass

f.__name__ = name
f = click.command(f)
assert f.name == "init-data"
2 changes: 1 addition & 1 deletion tests/test_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ def other_cmd(ctx, a, b, c):
result = runner.invoke(cli, standalone_mode=False)
# invoke should type cast default values, str becomes int, empty
# multiple should be empty tuple instead of None
assert result.return_value == ("other-cmd", 42, 15, ())
assert result.return_value == ("other", 42, 15, ())


def test_invoked_subcommand(runner):
Expand Down