Skip to content

Fix name handling in ClickAliasedGroup.add_command #26

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

Merged
merged 1 commit into from
May 21, 2025
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
4 changes: 2 additions & 2 deletions click_aliases/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ def __init__(self, *args: t.Any, **kwargs: t.Any) -> None:

def add_command(self, *args: t.Any, **kwargs: t.Any) -> None:
aliases = kwargs.pop("aliases", [])
name = kwargs.get("name")
super().add_command(*args, **kwargs)
if aliases:
cmd = args[0]
name = args[1] if len(args) > 1 else None
name = name or cmd.name
name = name or (args[1] if len(args) > 1 else None) or cmd.name
if name is None:
raise TypeError("Command has no name.")

Expand Down
24 changes: 24 additions & 0 deletions test_aliases.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/usr/bin/env python
import click
from click_aliases import ClickAliasedGroup

@click.group(cls=ClickAliasedGroup)
def cli():
pass

@click.command()
def foo():
"""Run a command."""
click.echo('foo')

# Original case that didn't work - should now show aliases
cli.add_command(foo, name='foo2', aliases=['bar', 'baz', 'qux'])

# Original working case for reference
@cli.command(aliases=['test2', 'test3'])
def test():
"""Test command."""
click.echo('test')

if __name__ == '__main__':
cli()