在Python Click中,是否可以像这样做?我想要为同一个click.Group
使用不同的名称。
import click
class CustomMultiGroup(click.Group):
def group(self, *args, **kwargs):
"""Behaves the same as `click.Group.group()` except if passed
a list of names, all after the first will be aliases for the first.
"""
def decorator(f):
if isinstance(args[0], list):
_args = [args[0][0]] + list(args[1:])
for alias in args[0][1:]:
cmd = super(CustomMultiCommand, self).group(
alias, *args[1:], **kwargs)(f)
cmd.short_help = "Alias for '{}'".format(_args[0])
else:
_args = args
cmd = super(CustomMultiCommand, self).group(
*_args, **kwargs)(f)
return cmd
return decorator
@click.group(cls=CustomMultiGroup)
def mycli():
pass
@cli.group(['my-group', 'my-grp'])
def my_group():
pass
@my_group.command()
def my_command():
pass
我希望我的命令行看起来像这样:
mycli my-group my-command
并且。
mycli my-grp my-command
但是引用同一函数。
这篇文章参考了Python Click 多个命令名称
@click.group()
,而应该是@my_outer_group.group()
,其中my_outer_group
是先前声明的组。您能确认这是正确的吗? - Stephen Rauch