click模块 的使用
##官方文档##
click模块 可以 轻松将一个函数变成一个命令行工具
# 1. click是基于装饰器的,我们可以在方法上使用click.command()装饰器来将该方法变成一个命令行工具
import click
@click.command()
def hello():
click.echo('Hello World!')
if __name__ == '__main__':
hello()
1 click.option
option 最基本的用法就是通过指定命令行选项的名称,从命令行读取参数值,再将其传递给函数。我们除了设置命令行选项的名称,我们还会指定默认值,help 说明等,option 常用的设置参数如下:
- default: 设置命令行参数的默认值
- help: 参数说明
- type: 参数类型,可以是 str, int, float 等
- prompt: 当在命令行中没有输入相应的参数时,会根据 prompt 提示用户输入可以指定 True, 或者特定字符串来提示用户输入
- nargs: 指定命令行参数接收的值的个数, -1 表示可以接收多个参数,
# 1 可以指定默认值, help 信息, prompt 提示信息
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."""
for x in range(count):
click.echo('Hello %s!' % name)
if __name__ == '__main__':
hello()
# 2 还可以 指定 type
import click
@click.command()
@click.option('--rate', '-rate',type=int, help='rate') # 指定 rate 是 int 类型
def show(rate):
print(f'type(rate):{type(rate)}')
click.echo('rate: %s' % rate)
if __name__ == '__main__':
show()
# 我指定为int 类型 , 如果传入name 就会报错
##2-1 type 也可以设定可选值
import click
@click.command()
@click.option('--hash-type', type=click.Choice(['md5', 'sha1']))
def digest(hash_type):
click.echo('hash_type:{}'.format(hash_type))
if __name__ == '__main__':
digest()
## 4 有的时候可能需要输入密码 ,可以用 option
import click
@click.command()
@click.option('--password', prompt=True, hide_input=True, confirmation_prompt=True)
def input_password(password):
click.echo('password: %s' % password)
如果 两次 输入相同,则通过. 如果输入不相同, 则提醒你 重新输入.
# 4-1 click.password_option () 有一个专门的装饰器
import click
@click.command()
@click.password_option()
def input_password(password):
click.echo('password: %s' % password)
###5 可以通过--, 或 - 来传递参数
import click
@click.command()
@click.option('--model', '-model', type=click.Choice(['consume_loan', 'large', 'small']), default='large')
def run(model):
click.echo("model:{}".format(model))
if __name__ == '__main__':
run()
python test_click.py -model small
python test_click.py --model small
2. click.argument
除了使用 @click.option 来添加 可选参数 ,还会经常使用 @click.argument 来添加 固定参数 。它的使用和 option 类似,但支持的功能比 option 少。
可以参考 这个文档
## 1 为命令行添加 固定参数
import click
@click.command()
@click.option('--count', default=1, help='number of greetings')
@click.argument('name')
def hello(count, name):
for x in range(count):
click.echo('Hello %s!' % name)
if __name__ == '__main__':
hello()
##2 看看多个 argument 的例子:
import click
@click.command()
@click.argument('x')
@click.argument('y')
@click.argument('z')
def show(x, y, z):
click.echo('x: %s, y: %s, z:%s' % (x, y, z))
if __name__ == '__main__':
show()
##3 nrgs 的用法 ,可以 吸收多余的参数
Variadic Arguments ¶
# 其中,nargs=-1 表明参数 src 接收不定量的参数值,参数值会以 tuple 的形式传入函数。
# 如果 nargs 大于等于 1,表示接收 nargs 个参数值,上面的例子中,dst 接收一个参数值。
# The value is then passed as a tuple. Note that only one argument can be set to nargs=-1, as it will eat up all arguments.
# 用来吸收多余的参数, 作为一个元祖 传值
import click
@click.command()
@click.argument('src', nargs=-1)
@click.argument('dst', nargs=1)
def move(src, dst):
click.echo('move %s to %s' % (src, dst))
if __name__ == '__main__':
move()
## 4嵌套命令
# 可以通过group来实现命令行的嵌套,也就是让一个命令行工具具有多个命令
import click
@click.group()
def cli():
click.echo('Hello world')
@cli.command()
def initdb():
click.echo('Initialized the database')
@cli.command()
def dropdb():
click.echo('Dropped the database')
if __name__ == '__main__':
cli()
总结: 本文主要讲解了用click模块,将一个函数可以非常轻松的变成命令行函数. 讲解了常用的方法的使用. 更多还需参考官方文档.
参考文档:
分享快乐,留住感动. 2018-06-01 19:59:27 --frank