python click
from:https://www.cnblogs.com/dachenzi/p/7995539.html
https://github.com/ageitgey/face_recognition/blob/master/face_recognition/face_recognition_cli.py
Click模块
click模块是Flask的作者开发的一个第三方模块,用于快速创建命令行。它的作用与Python标准库的argparse相同,但是,使用起来更简单。
click是一个第三方库,因此使用起来需要先行安装
安装click模块
使用pip命令即可完成模块的安装
|
1
|
pip install click
|
基本使用
Click对argparse的主要改在在于易用性,使用click模块主要分为两个步骤:
- 使用@click.command() 装饰一个函数,使之成为命令行接口
- 使用@click.option() 等装饰函数,为其添加命令行选项
下列为click官方提供的例子:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
import
click
@click
.command()
@click
.option(
'--count'
, default
=
1
,
help
=
'Number of greetings.'
)
@click
.option(
'--name'
, prompt
=
'Your name'
,
help
=
'The person to greet.'
)
def
hello(count, name):
"""Simple program that greets NAME for a total of COUNT times."""
# 会当作help信息进行输出
for
x
in
range
(count):
click.echo(
'Hello %s!'
%
name)
if
__name__
=
=
'__main__'
:
hello()
|
在上面的例子中,函数hello接受两个参数,分别是count和name,他们的取值从命令行中获取,这里我们使用了click模块中的command、option、echo,他们的作用如下:
- command:使函数hello成为命令行接口
- option:增加命令行选项
- echo:输出结果,使用echo进行输出是为了更好的兼容性,因为python 2中的print是个语句,python 3中的print 是一个函数
运行上面的脚本,可以通过命令指定--name,--count的值,由于我们在option中指定了prompt选项,那么如果我们执行脚本没有传递name这个参数时,Click会提示我们在交互模式下输入
PS:与argparse模块一样,click也会为我们自动生成提示信息
|
1
2
3
4
5
6
7
8
9
|
lidaxindeMacBook
-
Pro:hello DahlHin$ python3 click模块.py
-
-
help
Usage: click模块.py [OPTIONS]
Simple program that greets NAME
for
a total of COUNT times.
Options:
-
-
count INTEGER Number of greetings.
-
-
name TEXT The person to greet.
-
-
help
Show this message
and
exit.
|
其他参数
option最基本的用法就是通过指定命令行选项的名称,从命令行读取参数值,再将其传递给函数。option常用的参数含义:
- default: 设置命令行参数的默认值
- help:参数说明
- type:参数类型,可以是string、int、float等
- prompt:当在命令行中没有输入相应的参数时,会更具prompt提示用户输入
- nargs:指定命令行参数接受的值的个数
- required:是否为必填参数
|
1
2
3
4
5
6
7
8
9
10
|
import
click
@click
.command()
@click
.option(
'--pos'
,nargs
=
2
,
type
=
float
)
def
getfloat(pos):
click.echo(
'%s / %s'
%
pos )
if
__name__
=
=
'__main__'
:
getfloat()
|
注意:option中定义的参数名称,那么就需要用同名的变量进行接受。
更多参数请参考: http://click.pocoo.org/5/options/#choice-opts
扩展用法
场景一:我们限定用户输入的值,那么就需要使用Click模块中的Choice函数,Choice的参数是一个列表,该列表中列出所有可能的值。
|
1
2
3
4
5
6
7
8
9
|
import
click
@click
.command()
@click
.option(
'-c'
,required
=
True
,
type
=
click.Choice([
'start'
,
'stop'
]))
# 限定-c的值为start,或者stop,required表示是否为必填参数
def
getcommand(c):
click.echo(
'command is %s'
%
c )
if
__name__
=
=
'__main__'
:
getcommand()
|
场景二:应用程序从命令行读取密码。
使用标准库中的argparse模块只能像输入普通参数一样输入密码。这种方式存在一定安全隐患,例如输入的密码会保存在history中,查看命令历史列表就能获取密码
在Click中,这个问题就能完美的解决,只需要是这prompt为True,那么我们就能交互式输入密码,设置hide_input为True,就能隐藏密码,设置confirmation_prompt为True,就可以进行密码的两次验证,使用起来非常便捷。
|
1
2
3
4
5
6
7
8
9
10
|
import
click
@click
.command()
@click
.option(
'-p'
,prompt
=
'Your Password'
,hide_input
=
True
,confirmation_prompt
=
True
)
def
getpassword(p):
click.echo(
'Your Password is : %s'
%
p)
if
__name__
=
=
'__main__'
:
getpassword()
|
关注公众号
低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
-
上一篇
阿里云提示漏洞:destoon变量覆盖导致延时注入
出现漏洞 .../module/mall/my.inc.php .../backup/module/mall/my.inc.php 修复方法: my.inc.php里面所有的extract函数加上, EXTR_SKIP参数就可以 比如extract($item);修改成extract($item,EXTR_SKIP);
-
下一篇
C# 获取系统Icon、获取文件相关的Icon
原文:C# 获取系统Icon、获取文件相关的Icon 1、获取系统Icon 工具下载SystemIcon.exe using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.IO; using System.Linq; using System.Runtime.InteropServices; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace FileExplorer { /// <summary> /// 系统Icon /// 1、Get() 获取指定索引对应的系统icon /// 2、Save() 保存所有系统图像 /// 3、Show() 显示所有系统Icon图像 /// </summary> public partial class ...
相关文章
文章评论
共有0条评论来说两句吧...

微信收款码
支付宝收款码