Skip to content

strip "_command" suffix when generating command name #2322

Closed

Description

@davidism

You often want a command to have the same name as a function it is wrapping. However, this will cause a name collision, if your command is going to call init_data its function can't also be called init_data. So instead it's written as init_data_command, and then the command name has to be specified manually @command("init-data"). Click should remove a _command suffix when generating the command name from the function name.

def init_data(external=False):
    ...

@cli.command
@click.option("--external/--internal")
def init_data_command(external):
    init_data(external=external)

Currently this command would be named init-data-command. After the change, it would be init-data.

Activity

  1. bebleo commented on Sep 10, 2022

    @bebleo

    Hi @davidism, Am I right in assuming the expected behaviour would be that both _command and _cmd suffixes are dropped?

    An example using _cmd as a suffix:

    @cli.command("open")
    @click.option(
    "-i",
    "--image",
    "images",
    type=click.Path(),
    multiple=True,
    help="The image file to open.",
    )
    @generator
    def open_cmd(images):
    """Loads one or multiple images for processing. The input parameter
    can be specified multiple times to load more than one image.
    """
    for image in images:
    try:
    click.echo(f"Opening '{image}'")
    if image == "-":
    img = Image.open(click.get_binary_stdin())
    img.filename = "-"
    else:
    img = Image.open(image)
    yield img
    except Exception as e:
    click.echo(f"Could not open image '{image}': {e}", err=True)

  2. added this to the 8.2.0 milestone on Jun 29, 2023
  3. self-assigned this
    on Sep 1, 2023
  4. locked as resolved and limited conversation to collaborators on Sep 16, 2023
  5. added
    parsingParsing, parameters, commands, chaining, context
    on Aug 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

parsingParsing, parameters, commands, chaining, context

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions