Python单击多个组名

时间:2017-10-16 08:17:26

标签: python command-line-interface alias python-click

使用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

的引用

2 个答案:

答案 0 :(得分:1)

click.Groupclick.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

答案 1 :(得分:0)

在 Click 中使用命令别名的最简单方法是使用以下软件包之一:

免责声明:我是 Cloup 的作者。

两种实现方式略有不同。

  • Cloup 在命令本身中存储命令的别名。这允许 Cloup 在命令本身的 --help 中记录命令的别名(也)。

  • click-aliases 总是在组的“命令”部分显示(子)命令的别名。在 Cloup 中,您可以根据自己的喜好启用/禁用此行为(默认情况下,不显示别名)。