对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'

美国石油学会

kwargtypeusage
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_ifprompt_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,则{}的类型将为int

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'}

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java JNI和取消异步任务   java整数n作为List<String>的第二个参数传递。子列表(fromIndex,toIndex),但是stacktrace说toIndex在Kotlin中是n+2   如何在Java中正确嵌套多个ArrayList/Maps?   在嵌套条件语句中比较数组中的值时,java List没有打印正确的值   从CRON作业运行java代码   JRE使用jvisualvm检测到java致命错误   java将字符添加到BufferedReader中   带有枚举的java单例与带有双重检查锁定的单例   Java中的编译器构造基类的更改会影响子类的字节码吗?   Java作为Windows服务用户数据路径   java clone():ArrayList。克隆人()我认为它是一个肤浅的复制品   在jfilechooser多文件选择器启用中打开对话框后,java文件将显示所选文件   java在常用的操作方法中使用反射可以吗?   java ReloadableResourceBundleMessageSource。getMessage()不使用我给它的参数