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

Bugfix/shellcompletion/commandnames #28

Merged
merged 2 commits into from
Aug 19, 2020
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
22 changes: 11 additions & 11 deletions src/click/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,11 +129,11 @@ def sort_key(item):


def _get_visible_commands(ctx, incomplete):
for c in ctx.command.list_commands(ctx):
if c.startswith(incomplete):
command = ctx.command.get_command(ctx, c)
for name in ctx.command.list_commands(ctx):
if name.startswith(incomplete):
command = ctx.command.get_command(ctx, name)
if not command.hidden:
yield command
yield name, command


class ParameterSource:
Expand Down Expand Up @@ -765,14 +765,14 @@ def shell_complete(self, ctx, all_args, incomplete):
ctx = ctx.parent
if isinstance(ctx.command, MultiCommand) and ctx.command.chain:
remaining_commands = [
c
for c in _get_visible_commands(ctx, incomplete)
if c.name not in ctx.protected_args
(name, command)
for name, command in _get_visible_commands(ctx, incomplete)
if name not in ctx.protected_args
]
results.extend(
[
("plain", c.name, c.get_short_help_str())
for c in remaining_commands
("plain", name, command.get_short_help_str())
for name, command in remaining_commands
]
)
return results
Expand Down Expand Up @@ -1420,8 +1420,8 @@ def shell_complete(self, ctx, all_args, incomplete):
results = []
results.extend(
[
("plain", c.name, c.get_short_help_str())
for c in _get_visible_commands(ctx, incomplete)
("plain", name, command.get_short_help_str())
for name, command in _get_visible_commands(ctx, incomplete)
]
)
results.extend(BaseCommand.shell_complete(self, ctx, all_args, incomplete))
Expand Down
35 changes: 35 additions & 0 deletions tests/test_shellcomplete.py
Original file line number Diff line number Diff line change
Expand Up @@ -644,3 +644,38 @@ def cli(opt, args):
pass

assert get_completions(cli, args, part) == expect


def test_command_names():
@click.group()
def cli():
pass

@cli.command() # use autodetected name
def asub():
pass

cli.add_command(asub, name="bsub") # use manual name override

@click.command() # autodetect name
def csub():
pass

cli.add_command(csub) # use autodetected name
cli.add_command(csub, name="dsub") # use manual name override

@cli.command(name="esub") # manual name
def zsub():
pass

cli.add_command(zsub) # use manual name
cli.add_command(zsub, name="fsub") # use manual name override

assert get_completions(cli, [""], "") == [
"asub",
"bsub",
"csub",
"dsub",
"esub",
"fsub",
]