使用Python Click可以做这样的事情吗?
@click.command(name=['my-command', 'my-cmd'])
def my_command():
pass
我希望我的命令行类似于:
mycli my-command
和
mycli my-cmd
但引用相同的功能。
我是否需要像AliasedGroup这样的课程?
答案 0 :(得分:4)
AliasedGroup不是你所追求的,因为它允许最短的前缀匹配,看起来你需要实际的别名。但是,该示例确实提供了可以起作用的方向的提示。它继承自click.Group
并覆盖了某些行为。
这是一种接近你所追求的方法:
此类覆盖用于装饰命令功能的click.Group.command()
方法。它增加了传递命令别名列表的功能。该类还添加了一个引用别名命令的简短帮助。
class CustomMultiCommand(click.Group):
def command(self, *args, **kwargs):
"""Behaves the same as `click.Group.command()` 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).command(
alias, *args[1:], **kwargs)(f)
cmd.short_help = "Alias for '{}'".format(_args[0])
else:
_args = args
cmd = super(CustomMultiCommand, self).command(
*_args, **kwargs)(f)
return cmd
return decorator
通过将cls
参数传递给click.group()
装饰器,通过group.command()
添加到组中的任何命令都可以传递一个命令名列表。
@click.group(cls=CustomMultiCommand)
def cli():
"""My Excellent CLI"""
@cli.command(['my-command', 'my-cmd'])
def my_command():
....
import click
@click.group(cls=CustomMultiCommand)
def cli():
"""My Excellent CLI"""
@cli.command(['my-command', 'my-cmd'])
def my_command():
"""This is my command"""
print('Running the command')
if __name__ == '__main__':
cli('--help'.split())
Usage: my_cli [OPTIONS] COMMAND [ARGS]...
My Excellent CLI
Options:
--help Show this message and exit.
Commands:
my-cmd Alias for 'my-command'
my-command This is my command
答案 1 :(得分:2)
这是解决同一件事的一种简单方法:
class AliasedGroup(click.Group):
def get_command(self, ctx, cmd_name):
try:
cmd_name = ALIASES[cmd_name].name
except KeyError:
pass
return super().get_command(ctx, cmd_name)
@click.command(cls=AliasedGroup)
def cli():
...
@click.command()
def install():
...
@click.command()
def remove():
....
cli.add_command(install)
cli.add_command(remove)
ALIASES = {
"it": install,
"rm": remove,
}