Skip to content

Commit

Permalink
feat: aliased command groups
Browse files Browse the repository at this point in the history
  • Loading branch information
nikhiljha committed Sep 14, 2023
1 parent 21917c0 commit e7de0c0
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion transpire/internal/cli/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,25 @@
from . import bootstrap, dev, image, obj, secrets


@click.group()
class AliasedGroup(click.Group):
def get_command(self, ctx, cmd_name):
rv = click.Group.get_command(self, ctx, cmd_name)
if rv is not None:
return rv
matches = [x for x in self.list_commands(ctx) if x.startswith(cmd_name)]
if not matches:
return None
elif len(matches) == 1:
return click.Group.get_command(self, ctx, matches[0])
ctx.fail(f"Too many matches: {', '.join(sorted(matches))}")

def resolve_command(self, ctx, args):
# always return the full command name
_, cmd, args = super().resolve_command(ctx, args)
return cmd.name, cmd, args


@click.command(cls=AliasedGroup)
def cli() -> None:
pass

Expand Down

0 comments on commit e7de0c0

Please sign in to comment.