Skip to main content
 首页 » 编程设计

Python 点击多个组名

2025年01月19日14me-sa

是否可以用 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 multiple command names 的引用

请您参考如下方法:

click.Group 和 click.Command 的行为不同,因此您需要修改 example允许别名组访问别名组的命令:

自定义类

此类重写了用于修饰组函数的 click.Group.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): 
            aliased_group = [] 
            if isinstance(args[0], list): 
                # we have a list so create group aliases 
                _args = [args[0][0]] + list(args[1:]) 
                for alias in args[0][1:]: 
                    grp = super(CustomMultiGroup, self).group( 
                        alias, *args[1:], **kwargs)(f) 
                    grp.short_help = "Alias for '{}'".format(_args[0]) 
                    aliased_group.append(grp) 
            else: 
                _args = args 
 
            # create the main group 
            grp = super(CustomMultiGroup, self).group(*_args, **kwargs)(f) 
 
            # for all of the aliased groups, share the main group commands 
            for aliased in aliased_group: 
                aliased.commands = grp.commands 
 
            return grp 
 
        return decorator 

测试代码:

@click.group(cls=CustomMultiGroup) 
def cli(): 
    pass 
 
@cli.group(['my-group', 'my-grp']) 
def my_group(): 
    """ My Sub Command """ 
    pass 
 
@my_group.command('my-command') 
def my_command(): 
    click.echo("My Command") 
 
cli('--help'.split()) 
cli('my-grp --help'.split()) 
cli('my-group --help'.split()) 
cli('my-grp my-command'.split()) 
cli('my-group my-command'.split()) 

测试结果:

Usage: my_cli [OPTIONS] COMMAND [ARGS]... 
 
Options: 
  --help  Show this message and exit. 
 
Commands: 
  my-group  My Sub Command 
  my-grp    Alias for 'my-group' 
 
Process finished with exit code 0