对click的约束选项支持
click-constrained-option的Python项目详细描述
单击“约束选项”
click constrained option是一个扩展包,它向Click中的选项添加约束。在
在click 7.x下测试
它的作用
生成选项:
- 相互排斥的
- 取决于其他选项
- 有条件地要求
- 有条件地晋升
- 有条件地设置其默认值
- 有条件地设置其类型
快速入门
pip install click-constrained-option^{pr2}$
$ python example.py Error: require exact one of '--apple''--orange''--pear' $ python example.py --apple --pear Error: require exact one of '--apple''--orange''--pear'
美国石油学会
kwarg | type | usage |
---|---|---|
allowed_func | ^{ | return True to indicate allowance, vice versa |
allowed_if | ^{ | name of the option |
allowed_if_not | ^{ | name of the option |
allowed_if_all_of | ^{ | list of the option names |
allowed_if_none_of | ^{ | list of the option names |
allowed_if_any_of | ^{ | list of the option names |
allowed_if_one_of | ^{ | list of the option names |
required_func | ^{ | return True to indicate requirement, vice versa |
required_if | ^{ | name of the option |
required_if_not | ^{ | name of the option |
required_if_all_of | ^{ | list of the option names |
required_if_none_of | ^{ | list of the option names |
required_if_any_of | ^{ | list of the option names |
required_if_one_of | ^{ | list of the option names |
prompt_func | ^{ | return True to indicate prompt, vice versa |
prompt_if | ^{ | name of the option |
prompt_if_not | ^{ | name of the option |
prompt_if_all_of | ^{ | list of the option names |
prompt_if_none_of | ^{ | list of the option names |
prompt_if_any_of | ^{ | list of the option names |
prompt_if_one_of | ^{ | list of the option names |
group_require_one | ^{ | list of the option names |
group_require_any | ^{ | list of the option names |
group_require_all | ^{ | list of the option names |
default_func | ^{ | return a value to use as the default |
type_func | ^{ | return a valid type of the option |
注
- 在
如果使用多个Kwarg,则必须满足所有Kwarg。例如,如果指定了
在prompt_if
和prompt_if_all_of
,则只有当这两个条件都为true时才会提示该选项。在 - 在
如果自定义函数的参数等于命令中的选项名称,则该选项的值将用作参数。例如,
在--foo
的值将传递给自定义函数bar(foo)
。在
示例
依赖与排他关系
如果设置了--username
,则允许使用--username
:
importclickfromclick_constrained_optionimportConstrainedOption@click.command()@click.option("--username",cls=ConstrainedOption,allowed_if="login")@click.option("--login",is_flag=True)defcli(**kwargs):click.echo(kwargs)if__name__=="__main__":cli()
$ python example.py --username=foo Error: Usage for'--username': require '--login'
如果指定了--userid
和{--oauth
,则允许使用--login
:
importclickfromclick_constrained_optionimportConstrainedOption@click.command()@click.option("--login",cls=ConstrainedOption,is_flag=True,allowed_if_any_of=["userid","email"],allowed_if_not="cookie")@click.option("--userid")@click.option("--email")@click.option("--oauth",is_flag=True)defcli(**kwargs):click.echo(kwargs)if__name__=="__main__":cli()
$ python example.py --login --userid=foo --oauth Error: Usage for'--login': conflict with '--oauth' $ python example.py --login Error: Usage for'--login': require at least one of '--userid''--email'
如果--listen
是IP地址,则需要--port
:
importclickimportrefromclick_constrained_optionimportConstrainedOption@click.command()@click.option("--port",cls=ConstrainedOption,required_func=lambdab:re.match(r"\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}",b))@click.option("--listen")defcli(**kwargs):click.echo(kwargs)if__name__=="__main__":cli()
$ python example.py --listen=127.0.0.1 Error: Missing option '--port'
互斥组
必须设置--order-by-date
--order-by-name
--order-by-rank
中的一个:
importclickfromclick_constrained_optionimportConstrainedOption@click.option(cls=ConstrainedOption,group_require_one=["order_by_name","order_by_rank","order_by_date"])@click.option("--order-by-name",cls=ConstrainedOption,is_flag=True)@click.option("--order-by-rank",cls=ConstrainedOption,is_flag=True)@click.option("--order-by-date",cls=ConstrainedOption,is_flag=True)defcli(**kwargs):click.echo(kwargs)if__name__=="__main__":cli()
$ python example.py Error: require exact one of '--order-by-name''--order-by-rank''--order-by-date'
条件提示
如果指定了--userid
--email
之一,则会提示--password
importclickfromclick_constrained_optionimportConstrainedOption@click.command()@click.option("--userid")@click.option("--email")@click.option("--password",hide_input=True,prompt_if_one_of=["userid","email"])defcli(**kwargs):click.echo(kwargs)if__name__=="__main__":cli()
$ python example.py --userid Password:
条件类型
如果--time
,则{
importclickfromclick_constrained_optionimportConstrainedOption@click.command()@click.option("--time-format",type=click.Choice(["iso-8601","timestamp"]))@click.option("--time",cls=ConstrainedOption,type_func=lambdatime_format:click.INTiftime_format=="timestamp"elseclick.STRING)defcli(**kwargs):click.echo(kwargs)if__name__=="__main__":cli()
$ python --time-format="timestamp" --time=str_not_int Error: Invalid value for'--time': str_not_int is not a valid integer
条件默认值
通过自定义函数设置--lucky
的默认值
importclickfromclick_constrained_optionimportConstrainedOptionfromrandomimportrandint@click.command()@click.option("--lucky",default=lambda:randint(0,9))defcli(**kwargs):click.echo(kwargs)if__name__=="__main__":cli()
$ python example.py {'lucky': '5'}
- 项目
标签: