小编rie*_*nix的帖子

click.Choice多个参数

所以我想要实现的是确保参数是某些预定义集合(这里是tool1,tool2,tool3)的一部分,就像使用@click.optiontype=click.Choice(),同时能够传递多个参数,如@click.argumentwith nargs=-1.

import click

@click.command()
@click.option('--tools', type=click.Choice(['tool1', 'tool2', 'tool3']))
@click.argument('programs', nargs=-1)
def chooseTools(programs, tools):
    """Select one or more tools to solve the task"""

    # Selection of only one tool, but it is from the predefined set
    click.echo("Selected tools with 'tools' are {}".format(tools))

    # Selection of multiple tools, but no in-built error handling if not in set
    click.echo("Selected tools with 'programs' are {}".format(programs))
Run Code Online (Sandbox Code Playgroud)

这将是这样的例子:

python selectTools.py --tools tool1 tool3 tool2 nonsense
Selected tools with …
Run Code Online (Sandbox Code Playgroud)

python python-click

7
推荐指数
1
解决办法
6046
查看次数

标签 统计

python ×1

python-click ×1