Можно ли добавить глобальный аргумент для всех подкоманд в интерфейсах на основе Click?

Я использую Click под virtualenv и использую директиву entry_point в setuptools, чтобы сопоставить корень с функцией, называемой диспетчером.

Мой инструмент предоставляет две подкоманды serve и config. Я использую параметр в группе верхнего уровня, чтобы гарантировать, что пользователь всегда передает директиву --path. Однако использование получается следующим образом:

mycommand --path=/tmp serve

обе подкоманды serve и config должны гарантировать, что пользователь всегда передает путь, и в идеале я хотел бы представить cli как:

mycommand serve /tmp` or `mycommand config validate /tmp

Текущая реализация на основе Click выглядит следующим образом:

# cli root

@click.group()
@click.option('--path', type=click.Path(writable=True))
@click.version_option(__version__)
@click.pass_context
def dispatch(ctx, path):
    """My project description"""
    ctx.obj = Project(path="config.yaml")

# serve

@dispatch.command()
@pass_project
def serve(project):
    """Starts WSGI server using the configuration"""
    print "hello"

# config

@dispatch.group()
@pass_project
def config(project):
    """Validate or initalise a configuration file"""
    pass

@config.command("validate")
@pass_project
def config_validate(project):
    """Reports on the validity of a configuration file"""
    pass

@config.command("init")
@pass_project
def config_init(project):
    """Initialises a skeleton configuration file"""
    pass

Возможно ли это без добавления аргумента пути к каждой подкоманде?


person Devraj    schedule 10.09.2015    source источник
comment
Вы когда-нибудь находили ответ на этот вопрос?   -  person larsks    schedule 09.06.2016
comment
Нет, я этого не сделал. Но в то время я работал с Click 5 и не проверял, есть ли эта опция в Click 6.6.   -  person Devraj    schedule 11.06.2016


Ответы (1)


Если есть конкретный аргумент, который вы хотели бы применить только к группе, но применимый ко всем командам по мере необходимости, вы можете сделать это с помощью дополнительной сантехники, например:

Пользовательский класс:

import click

class GroupArgForCommands(click.Group):
    """Add special argument on group to front of command list"""

    def __init__(self, *args, **kwargs):
        super(GroupArgForCommands, self).__init__(*args, **kwargs)
        cls = GroupArgForCommands.CommandArgument

        # gather the special arguments
        self._cmd_args = {
            a.name: a for a in self.params if isinstance(a, cls)}

        # strip out the special arguments
        self.params = [a for a in self.params if not isinstance(a, cls)]

        # hook the original add_command method
        self._orig_add_command = click.Group.add_command.__get__(self)

    class CommandArgument(click.Argument):
        """class to allow us to find our special arguments"""

    @staticmethod
    def command_argument(*param_decls, **attrs):
        """turn argument type into type we can find later"""

        assert 'cls' not in attrs, "Not designed for custom arguments"
        attrs['cls'] = GroupArgForCommands.CommandArgument

        def decorator(f):
            click.argument(*param_decls, **attrs)(f)
            return f

        return decorator

    def add_command(self, cmd, name=None):

        # hook add_command for any sub groups
        if hasattr(cmd, 'add_command'):
            cmd._orig_add_command = cmd.add_command
            cmd.add_command = GroupArgForCommands.add_command.__get__(cmd)
            cmd.cmd_args = self._cmd_args

        # call original add_command
        self._orig_add_command(cmd, name)

        # if this command's callback has desired parameters add them
        import inspect
        args = inspect.signature(cmd.callback)
        for arg_name in reversed(list(args.parameters)):
            if arg_name in self._cmd_args:
                cmd.params[:] = [self._cmd_args[arg_name]] + cmd.params

Использование пользовательского класса:

Чтобы использовать пользовательский класс, передайте параметр cls декоратору click.group(), используйте декоратор @GroupArgForCommands.command_argument для специальных аргументов, а затем добавьте параметр с тем же именем, что и специальный аргумент, к любым командам по мере необходимости.

@click.group(cls=GroupArgForCommands)
@GroupArgForCommands.command_argument('special')
def a_group():
    """My project description"""

@a_group.command()
def a_command(special):
    """a command under the group"""

Как это работает?

Это работает, потому что click — это хорошо спроектированная объектно-ориентированная среда. Декоратор @click.group() обычно создает экземпляр объекта click.Group, но позволяет переопределить это поведение с помощью параметра cls. Таким образом, относительно легко наследоваться от click.Group в нашем собственном классе и переопределять нужные методы.

В этом случае мы переопределяем click.Group.add_command(), чтобы при добавлении команды мы могли проверить параметры обратного вызова команды, чтобы увидеть, имеют ли они то же имя, что и любой из наших специальных аргументов. Если они совпадают, аргумент добавляется к аргументам команды так же, как если бы он был декорирован напрямую.

Кроме того, GroupArgForCommands реализует метод command_argument(). Этот метод используется как декоратор при добавлении специального аргумента вместо использования click.argument()

Тестовый код:

def process_path_to_project(ctx, cmd, value):
    """param callback example to convert path to project"""
    # Use 'path' to construct a project.
    # For this example we will just annotate and pass through
    return 'converted {}'.format(value)


@click.group(cls=GroupArgForCommands)
@GroupArgForCommands.command_argument('path',
                                      callback=process_path_to_project)
def dispatch():
    """My project description"""


@dispatch.command()
def serve(path):
    """Starts WSGI server using the configuration"""
    click.echo('serve {}'.format(path))

@dispatch.group()
def config():
    """Validate or initalise a configuration file"""
    pass

@config.command("validate")
def config_validate():
    """Reports on the validity of a configuration file"""
    click.echo('config_validate')


@config.command("init")
def config_init(path):
    """Initialises a skeleton configuration file"""
    click.echo('config_init {}'.format(path))



if __name__ == "__main__":
    commands = (
        'config init a_path',
        'config init',
        'config validate a_path',
        'config validate',
        'config a_path',
        'config',
        'serve a_path',
        'serve',
        'config init --help',
        'config validate --help',
        '',
    )

    import sys, time

    time.sleep(1)
    print('Click Version: {}'.format(click.__version__))
    print('Python Version: {}'.format(sys.version))
    for command in commands:
        try:
            time.sleep(0.1)
            print('-----------')
            print('> ' + command)
            time.sleep(0.1)
            dispatch(command.split())

        except BaseException as exc:
            if str(exc) != '0' and \
                    not isinstance(exc, (click.ClickException, SystemExit)):
                raise

Полученные результаты:

Click Version: 6.7
Python Version: 3.6.3 (v3.6.3:2c5fed8, Oct  3 2017, 18:11:49) [MSC v.1900 64 bit (AMD64)]
-----------
> config init a_path
config_init converted a_path
-----------
> config init
Usage: test.py config init [OPTIONS] PATH

Error: Missing argument "path".
-----------
> config validate a_path
Usage: test.py config validate [OPTIONS]

Error: Got unexpected extra argument (a_path)
-----------
> config validate
config_validate
-----------
> config a_path
Usage: test.py config [OPTIONS] COMMAND [ARGS]...

Error: No such command "a_path".
-----------
> config
Usage: test.py config [OPTIONS] COMMAND [ARGS]...

  Validate or initalise a configuration file

Options:
  --help  Show this message and exit.

Commands:
  init      Initialises a skeleton configuration file
  validate  Reports on the validity of a configuration...
-----------
> serve a_path
serve converted a_path
-----------
> serve
Usage: test.py serve [OPTIONS] PATH

Error: Missing argument "path".
-----------
> config init --help
Usage: test.py config init [OPTIONS] PATH

  Initialises a skeleton configuration file

Options:
  --help  Show this message and exit.
-----------
> config validate --help
Usage: test.py config validate [OPTIONS]

  Reports on the validity of a configuration file

Options:
  --help  Show this message and exit.
-----------
> 
Usage: test.py [OPTIONS] COMMAND [ARGS]...

  My project description

Options:
  --help  Show this message and exit.

Commands:
  config  Validate or initalise a configuration file
  serve   Starts WSGI server using the configuration
person Stephen Rauch    schedule 14.07.2018