click是python第三方库,用于快速创建命令行。
运行代码相关的github地址:
1.click.option()函数
1.1 利用装饰器原理(对函数增加功能而不修改原函数的条件下),根据代码来了解click.option()的作用:
import click
@click.command()
@click.option('--count', default=1, help='the count of greetings')
@click.option('--name', prompt='your name', help='the name of greetings')
def greeting(count, name):
for i in range(count):
click.echo('Hello,%s' % name)
if __name__ == '__main__':
greeting()
@click.command()装饰函数greeting,使其成为命令行接口
@click.option()第一个参数是命令行选项的名称,必须与greeting的参数名称一致,第二个设置命令行选项的默认值,第三个参数是帮助信息。
click.echo()也能输出结果,而且有更好兼容性,而print在python2和python3有些区别。
打开pycharm的terminal执行结果:
option通过指定命令行选项名称,从命令行读取参数值,传递给函数,option常用的设置参数如下:
default: 设置命令行参数的默认值
help:参数的帮助说明
type:指定参数类型,可以是常用的string,int,float等,也可以是列表等
prompt:当在命令行没有输入参数,进行提示
nargs:指定传参的个数
1.2利用nargs指定多值参数
python代码:
import click
@click.command()
@click.option('--center', nargs=2, type=float, help='the center of the circle')
@click.option('--radius', default=1, type=float, help='the radius of the circle')
def circle(center, radius):
click.echo('center=%s,radius=%s' % (center, radius))
if __name__ == '__main__':
circle()
执行结果:
虽然命令行参数是以元组形式传递给函数的,但是命令行不能写成元组形式(大概跟shell命令行规范相关)
2.click.Choice()实例化对象
指定某一参数,可选值
import click
@click.command()
@click.option('--gender', type=click.Choice(['man', 'woman']), help='the gender of the person')
def person(gender):
click.echo('gender=%s' % gender)
if __name__ == '__main__':
person()
执行结果:
3.隐式输入密码和验证
hide_input用于隐式输入密码,comfirmation_promt用于隐藏密码输入
python 代码:
import click
@click.command()
@click.option('--password', prompt='please enter the password', hide_input=True, confirmation_promt=True)
def input_passwd(password):
click.echo('password=%s' % password)
if __name__ == '__main__':
input_passwd()
执行结果:
由于python的简洁性,还有另外一种写法:
import click
@click.command()
@click.password_option()
def input_passwd(password):
click.echo('password=%s' % password)
if __name__ == '__main__':
input_passwd()
执行结果:
根据输出结果来看,第二种基本就是前一种的简化版,接口也完全一样。
4.终止命令行程序执行
命令行参数有些是已经规定好的,比如终端输入python,进入python console控制台;输入python --version,打印python版本。click提供eager标识对参数名进行标识,如果输入此参数,则拦截既定的命令行执行流程,跳转执行一个回调函数。
example:
import click
def print_hello(context,parma,value):
if not value or context.resilient_parsing:
return
click.echo('hello,everyone')
context.exit()
# context是click.core.Context对象,内部有resilient_parsing参数,默认是False。if True,Click将解析而不进行任何交互或回调调用。
# param为,命令行参数
# value 判断命令行是否输入了--version。若输入,为True。
@click.command()
@click.option('--version', callback=print_hello, is_flag=True, expose_value=False, is_eager=True) # 这三个bool是一体的。
@click.option('--name', default='Helen', help='name')
def hello(name):
click.echo('hello,%s' % name)
if __name__ == '__main__':
hello()
is_flag 标识命令行参数–version
is_eager=True 表明该命令行选项优先级高于其他选项;
expose_value=False 表示如果没有输入该命令行选项,会执行既定的命令行流程;
callback 指定了输入该命令行选项时,要跳转执行的函数;
执行结果:
5.click.argument()函数
click.option()可以添加可选参数,但是click.argument()只可以指定一个固定的参数,比click.option()简单
example:
import click
# 固定参数
@click.command()
@click.argument('coordinates')
# 不支持写help帮助说明,help='coordinate',也不能指定参数个数,比如nargs=2。然而可以指定不定量参数,例如:nargs=-1,这种好处在于输入多维下的坐标,而不指定维数,但是后面必须再装饰一次单参数的,比如radius,nargs=1
def show(coordinates):
click.echo('coordinates: %s' % coordinates)
if __name__ == '__main__':
show()
执行结果:
如果想指定多个参数,需要重复利用@click.argument()装饰。
6.彩色输出
使用click.secho进行彩色输出
import click
@click.command()
@click.option('--name', default='Nancy', help='the name to greet')
def hello(name):
click.secho('Hello %s!' % name, fg='green',bg='black',underline=True,bold=True)
click.secho('Hello %s!' % name, fg='yellow', bg='black')
if __name__ == '__main__':
hello()
fg 表示前景颜色(即字体颜色),可选值有:BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE 等;
bg表示背景颜色,可选值有:BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE 等;
underline 表示下划线,可选的样式还有:dim=True,bold=True等;
使用pycharm 2018.3,python 3.6.8调试
原文链接:https://blog.csdn.net/weixin_41830779/article/details/106681565