使用python click,如何避免重复使用多个子命令使用的参数代码

时间:2019-02-20 21:37:09

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

我有一组子命令,所有子命令都对URL列表进行操作,这些URL可以有选择地作为参数传递。如何将这个参数分配给组,以避免在每个子命令上重复参数定义?

当前代码:

from config import site_list

@click.group()
def cli():
    pass

@cli.command()
@cli.argument('sites', nargs=-1)
def subcommand_one():
    if sites:
        site_list = sites
    etc...

@cli.command()
@cli.argument('sites', nargs=-1)
def subcommand_two():
    if sites:
        site_list = sites
    etc...

示例调用:

$ python sites.py subcommand_one www.example.com www.example2.com

我尝试将参数装饰器移动到这样的组:

@click.group()
@click.argument('sites', nargs=-1)
def cli(sites):
    if sites:
        site_list = sites

但是我会收到此错误:

$ python sites.py subcommand_one
Usage: sites.py [OPTIONS] [SITES] COMMAND [ARGS]...
Try "sites.py --help" for help.

Error: Missing command.

3 个答案:

答案 0 :(得分:1)

click.argument只会返回一个装饰器,就像其他装饰器一样,因此您可以将其分配给某个变量:

import click

@click.group()
def cli():
    pass

sites_argument = click.argument('sites', nargs=-1)

@cli.command()
@sites_argument
def subcommand_one(sites):
    ...

@cli.command()
@sites_argument
def subcommand_two(sites):
    ...

答案 1 :(得分:1)

如果有一个特定的nargs = -1参数,您只想装饰到组上,但适用于 所有需要的命令,都可以使用一些额外的管道来完成,例如:

此答案的灵感来自this答案。

自定义类

class GroupNArgsForCommands(click.Group):
    """Add special arguments on group"""

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

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

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

    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'] = GroupNArgsForCommands.CommandArgument

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

        return decorator

    def group(self, *args, **kwargs):
        # any derived groups need to be the same type
        kwargs['cls'] = GroupNArgsForCommands

        def decorator(f):
            grp = super(GroupNArgsForCommands, self).group(
                *args, **kwargs)(f)
            self.add_command(grp)

            # any sub commands need to hook the same special args
            grp._cmd_args = self._cmd_args

            return grp

        return decorator

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

        # call original add_command
        super(GroupNArgsForCommands, self).add_command(cmd, name)

        # if this command's callback has desired parameters add them
        import inspect
        args = inspect.signature(cmd.callback)
        if len(args.parameters):
            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()装饰器,请使用 特殊参数的@GroupNArgsForCommands.command_argument装饰器,然后添加一个 与所需命令的特殊参数同名的参数。

@click.group(cls=GroupNArgsForCommands)
@GroupNArgsForCommands.command_argument('special', nargs=-1)
def a_group():
    """My project description"""

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

这是如何工作的?

之所以可行,是因为click是一个设计良好的OO框架。 @click.group()装饰器通常 实例化一个click.Group对象,但允许使用cls参数覆盖此行为。
因此,在我们自己的类中继承click.Group并超越所需的方法是相对容易的事情。

在这种情况下,我们越过click.Group.add_command(),以便在添加命令时可以检查 命令回调参数以查看它们是否与我们的任何特殊参数具有相同的名称。 如果它们匹配,则将参数添加到命令的参数中,就像直接修饰该参数一样。

此外,GroupNArgsForCommands实现了command_argument()方法。此方法用作 添加特殊参数而不是使用click.argument()

时的修饰器

测试类

import click

@click.group(cls=GroupNArgsForCommands)
@GroupNArgsForCommands.command_argument('sites', nargs=-1)
def cli():
    click.echo("cli group")

@cli.command()
def command_one(sites):
    click.echo("command_one: {}".format(sites))

@cli.group()
def subcommand():
    click.echo("subcommand group")

@subcommand.command()
def one():
    click.echo("subcommand_one")

@subcommand.command()
def two(sites):
    click.echo("subcommand_two: {}".format(sites))

if __name__ == "__main__":
    commands = (
        'command_one site1 site2',
        'command_one site1',
        'command_one',
        'subcommand',
        'subcommand one site1 site2',
        'subcommand one site1',
        'subcommand one',
        'subcommand two site1 site2',
        'subcommand two site1',
        'subcommand two',
        '--help',
        'command_one --help',
        'subcommand --help',
        'subcommand one --help',
        'subcommand two --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)
            cli(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)]
-----------
> command_one site1 site2
cli group
command_one: ('site1', 'site2')
-----------
> command_one site1
cli group
command_one: ('site1',)
-----------
> command_one
cli group
command_one: ()
-----------
> subcommand
cli group
Usage: test.py subcommand [OPTIONS] COMMAND [ARGS]...

Options:
  --help  Show this message and exit.

Commands:
  one
  two
-----------
> subcommand one site1 site2
Usage: test.py subcommand one [OPTIONS]

Error: Got unexpected extra arguments (site1 site2)
cli group
subcommand group
-----------
> subcommand one site1
cli group
subcommand group
Usage: test.py subcommand one [OPTIONS]

Error: Got unexpected extra argument (site1)
-----------
> subcommand one
cli group
subcommand group
subcommand_one
-----------
> subcommand two site1 site2
cli group
subcommand group
subcommand_two: ('site1', 'site2')
-----------
> subcommand two site1
cli group
subcommand group
subcommand_two: ('site1',)
-----------
> subcommand two
cli group
subcommand group
subcommand_two: ()
-----------
> --help
Usage: test.py [OPTIONS] COMMAND [ARGS]...

Options:
  --help  Show this message and exit.

Commands:
  command_one
  subcommand
-----------
> command_one --help
cli group
Usage: test.py command_one [OPTIONS] [SITES]...

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

Options:
  --help  Show this message and exit.

Commands:
  one
  two
-----------
> subcommand one --help
cli group
subcommand group
Usage: test.py subcommand one [OPTIONS]

Options:
  --help  Show this message and exit.
-----------
> subcommand two --help
cli group
subcommand group
Usage: test.py subcommand two [OPTIONS] [SITES]...

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

Options:
  --help  Show this message and exit.

Commands:
  command_one
  subcommand

答案 2 :(得分:0)

我认为Click使用@click.pass_context支持一种实际的解决方案。

如果要定义一组全部共享例如公共参数和公共选项的命令,则可以在组级别上定义它们,并将它们添加到诸如described in the Click documentation之类的上下文对象中。

@click.group(chain=True)
@click.argument("dataset_directory", type=click.Path(exists=True))
@click.option("-s", "--split-names", help="The splits to preprocess.", required=True,
              default=["trainset", "devset", "testset"], show_default=True)
@click.pass_context
def cli(ctx, dataset_directory, split_names):
    """
    Prepare the dataset for training

    DATASET_DIRECTORY The absolute path to the data directory.
    """
    ctx.ensure_object(dict)
    ctx.obj["DIRECTORY"] = dataset_directory
    ctx.obj["SPLITS"] = split_names

然后,该组中的各个命令可以传递上下文并使用上下文对象中的值,而不用定义它们自己的参数和选项。


@cli.command("create")
@click.pass_context
def create(ctx):
    create_semantics_json_from_csv(ctx.obj["DIRECTORY"], ctx.obj["SPLITS"])


@cli.command("tokenize")
@click.pass_context
def tokenize(ctx):
    preprocess_tokenize_semantics_json(ctx.obj["DIRECTORY"], ctx.obj["SPLITS"])

然后可以像下面这样调用命令:

my-cli-app /path/to/data create tokenize