Click doesn’t provide built-in command aliases. The common workarounds are:
- Prefix matching (native, via custom
Group
) - True aliases (via
click-aliases
package)
What’s the Difference?
Prefix matching
Type any unique prefix of a command name.
True aliases
Multiple distinct names for the same command.
Native Approach: Prefix Matching
Click’s documentation shows how to implement git-style abbreviations:
import click
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))}")
@click.group(cls=AliasedGroup)
def cli():
pass
Limitation
Adding a start
command would make st
ambiguous if you already have status
.