python-click:添加一个选项来修改其他参数的行为 [英] python-click: Adding an option that modifies the behavior of other parameters

查看:88
本文介绍了python-click:添加一个选项来修改其他参数的行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此问题与点击程序包有关:

Click是一个用于创建漂亮命令行的Python包以尽可能少的代码以可组合的方式连接.它的命令行界面创建工具包".高度可配置但具有明智的默认设置.

Click is a Python package for creating beautiful command line interfaces in a composable way with as little code as necessary. It’s the "Command Line Interface Creation Kit". It’s highly configurable but comes with sensible defaults out of the box.

它旨在使编写命令行工具的过程变得快速而又有趣的同时还能防止因无法实施预期的CLI API.

It aims to make the process of writing command line tools quick and fun while also preventing any frustration caused by the inability to implement an intended CLI API.

我想在 click.Command 中添加 click.Option ,这会更改该命令其他参数的行为.考虑以下示例:

I'd like to add a click.Option to my click.Command, which changes the behavior of the other parameters of that command. Consider the following example:

@click.option('--x', default=42, prompt=True)
@click.command
def cli_a(x):
  print(x)


@click.option('--x', default=42, prompt=False)
@click.command
def cli_b(x):
  print(x)

如果在未显式指定 x 的情况下调用 cli_a ,则会提示用户提供一个值(或使用ENTER确认默认值).如果在未指定 x 的情况下调用 cli_b ,则会使用默认值而不会提示用户.

If cli_a is called without explicitly specifying x the user is prompted to provide a value (or confirm the default value with ENTER). If cli_b is called without specifying x the default value is used without prompting the user.

我现在想添加一个标志 click.Option ,该标志允许用户在运行时的上述变体之一之间进行选择.因此,调用 cli_c --i 的行为类似于 cli_a ,而调用 cli_c 的行为类似于 cli_b .

I'd now like to add a flag click.Option that allows the user to choose between one of the above variants (at runtime). So, calling cli_c --i would behave like cli_a and calling cli_c would behave like cli_b.

@click.option('--i', is_flag=True, default=False, expose_value=False)
@click.option('--x', default=42, prompt=False)
@click.command
def cli_c(x):
  print(x)

当前的API是否可以使用?可行吗

Is that doable with the current API? Is it feasible?

类似的用例类似于 anwser-all-confimation-prompts-withyes 标志.通常,如果cli工具应该由用户以交互方式使用,并且可以通过脚本或类似的方式以自动化模式使用,则会出现这种情况.

A similar use-case would be something like an anwser-all-confimation-prompts-with-yes flag. Usually this comes up if the cli tool is supposed to be usable interactively by the user and in automated mode via a script or some such thing.

推荐答案

我提出了以下代码,该代码可产生所需的行为:

I have come up with following code, which produces the desired behavior:

def deactivate_prompts(ctx, param, value):
    if not value:
        click.echo("entering batch mode, deactivating all prompts ...")
        for p in ctx.command.params:
            if isinstance(p, click.Option) and p.prompt is not None:
                p.prompt = None
    return value

@click.option('--i/--b', default=True, is_eager=True, expose_value=False, callback=deactivate_prompts)
@click.option('--x', default=42, prompt=True)
@click.command
def cli_c(x):
  print(x)

这个想法是使用一个渴望的选项的回调来修改 Command 的所有(其他) Options .

The idea is to use the callback of an eager option to modify all (other) Options of the Command.

潜在的弱点:

  • 这是一种全有或全无的解决方案,即所有提示均处于活动状态或无.对于我的用例,这正是我想要的,但对于其他情况可能并非如此.
  • 这仅在一个方向上起作用,即通过关闭提示.因此,必须配置每个可能显示或不显示提示的 Option ,就像显示提示一样.
  • 如果关闭提示,我们仍然需要该 Option 的值,因此必须有一个备用的值源.IE.必须提供默认值.
  • This is an all or nothing solution, i.e. either all of the prompts are active or none. For my use-case, that's exactly what I wanted, but for others that might not be the case.
  • This works only in one direction, i.e. by turning the prompts off. So each Option that may or may not display a prompt must be configured as if displaying a prompt.
  • If we turn off the prompts, we still need a value for that Option, so an alternate value-source is a must. I.e. a default value must be provided.

这篇关于python-click:添加一个选项来修改其他参数的行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆