Skip to content

Commit 87aa8c0

Browse files
committed
aliased group
1 parent dd3bf2a commit 87aa8c0

File tree

1 file changed

+26
-2
lines changed
  • src/pkgmt/assets/template/src/package_name

1 file changed

+26
-2
lines changed

src/pkgmt/assets/template/src/package_name/cli.py

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,33 @@
55
import click
66

77

8-
@click.group()
8+
class AliasedGroup(click.Group):
9+
"""
10+
Allow running commands by only typing the first few characters.
11+
https://click.palletsprojects.com/en/8.1.x/advanced/#command-aliases
12+
13+
To disable, remove the `cls=AliasedGroup` argument from the `@click.group()` decorator.
14+
"""
15+
16+
def get_command(self, ctx, cmd_name):
17+
rv = click.Group.get_command(self, ctx, cmd_name)
18+
if rv is not None:
19+
return rv
20+
matches = [x for x in self.list_commands(ctx) if x.startswith(cmd_name)]
21+
if not matches:
22+
return None
23+
elif len(matches) == 1:
24+
return click.Group.get_command(self, ctx, matches[0])
25+
ctx.fail(f"Too many matches: {', '.join(sorted(matches))}")
26+
27+
def resolve_command(self, ctx, args):
28+
# always return the full command name
29+
_, cmd, args = super().resolve_command(ctx, args)
30+
return cmd.name, cmd, args
31+
32+
33+
@click.group(cls=AliasedGroup)
934
def cli():
10-
"""Command-line interface"""
1135
pass
1236

1337

0 commit comments

Comments
 (0)