所以我想要实现的是既要确保参数是某些预定义集(这里是工具1、工具2、工具3)的一部分,比如 with@click.option
和type=click.Choice()
,同时又能够传递多个参数,比如@click.argument
with 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))
这看起来像这样:
python selectTools.py --tools tool1 tool3 tool2 nonsense
Selected tools with 'tools' are tool1
Selected tools with 'programs' are (u'tool3', u'tool2', u'nonsense')
点击有没有内置的方法来实现这一点?或者我应该只使用@click.argument
并检查函数本身的输入?
由于我对命令行界面编程相当陌生,尤其是单击,并且才开始更深入地研究 python,因此我将不胜感激有关如何以简洁的方式处理此问题的建议。