python的argpare和click模块详解

python的argpare和click模块详解
一、argparse模块
1、模块说明
1
2

argparse是python的标准库中用来解析命令行参数的模块,用来替代已经过时的optparse模块,argparse能够根据程序中的定义的sys.argv中解析出这些参数,

并自动生成帮助和使用信息

  

2、模块常用的参数
1
2
3
4
5
6
7
8
9
10

参数说明:

name/flag:参数的名字

action:遇到参数的动作,默认值是store

nargs:参数的个数,可以是具体的数字,或者是+或者是,表示0个或者多个参数,+号表示1个或者多个参数

default:不指定参数时的默认值

type:# 参数的类型

choice:参数允许的值

required:可选参数是否可以省略

help:参数的帮助信息

dest:解析后参数的名称

  

3、使用方法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import argparse

def _argparse():

parseobj = argparse.ArgumentParser(description="This is script help")
# 参数说明:
# name/flag:参数的名字
# action:遇到参数的动作,默认值是store
# nargs:参数的个数,可以是具体的数字,或者是+或者是*,*表示0个或者多个参数,+号表示1个或者多个参数
# default:不指定参数时的默认值
# type:# 参数的类型
# choice:参数允许的值
# required:可选参数是否可以省略
# help:参数的帮助信息
# dest:解析后参数的名称

parseobj.add_argument("--host",action='store',dest='host',required=True,default="127",help="This is a host ip address",type=int)
parseobj.add_argument("--P",'--passwd',action='store', dest='pwd', required=True, default="admin123.",help="This is a host password", type=str)
parseobj.add_argument("--V", '--version', action='version', version="%(prog)s 0.1")

return parseobj.parse_args()

if name == '__main__':

res = _argparse()
print(res.pwd)
print(res.host)

  

  

4、最后我们测试一下这个模块
a、测试 -h选项,这里-h和--help的效果是一样的

b、测试--V选项和--version选项

c、测试一下输入的正确的参数

二、click模块
1、模块介绍
1
2
click模块的作者就是Flask的作者,(Armin Ronacher)开发的一个第三方的模块,用于快速创建命令行。他的作用用python标准库中的argparse相同,但是
使用更加简单,click相对于标准库的argparse,就好比requests库相当于标准库的urllib库,click是一个第三的库,因此在使用之前需要安装
  

2、模块安装
1
E:python3Scripts>pip3.6.exe install click
  

3、使用步骤
a、使用@click.command()装饰一个函数,使之成为命令行的接口

b、使用@click.option()等装饰函数,为其添加命令行选项等

c、先看一个官方的例子

1
2
3
4
5
6
7
8
9
10
11
12
13
14
import click

click模块的作者就是Flask的作者,(Armin Ronacher)开发的一个第三方的模块,用于快速创建命令行。他的作用用python标准库中的argparse相同,但是

使用更加简单,click相对于标准库的argparse,就好比requests库相当于标准库的urllib库,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):

for x in range(count):
    click.echo("hello {name}".format(name = name))

if name == '__main__':

hello()

  

其他的应该大家都可以看懂,这个prompt的作用是什么呢,实际就是如果我们没有为name传参数,他就会给出一个提示

下面这个例子是完整的传参

4、常用参数
1
2
3
4
5
6
常用参数
default:设置命令行参数的默认值
help:参数说明
type:参数的类型,可以是string,int,float
prompt:当在命令行中没有输入相应的参数,会根据prompt提示用户输入
nargs:指定命令行参数接受的值的个数
  

a、测试一下nargs参数

1
2
3
4
5
6
7
8
@click.command()
@click.option('--post',nargs=2,help='Number of post')
def hello(post):

print(post)

if name == '__main__':

hello()

  

测试结果

b、测试click.choice选项

1
2
3
4
5
6
7
8
@click.command()
@click.option('--hash',type=click.Choice(["md5","sha1"]),help='type of hash')
def hello(hash):

print(hash)

if name == '__main__':

hello()

  

测试结果

c、如果使用命令行输入密码,则默认的情况是有很大的安全隐患的,因为输入密码的命令在history中,其他用户就可以通过命令的历史列表,拿到我们的密码,click可以为我们解决这个问题

1
2
3
4
5
6
7
8
@click.command()
@click.option('--pwd',prompt=True,hide_input=True,help='passwd of user',confirmation_prompt=True)
def hello(pwd):

print(pwd)

if name == '__main__':

hello()

  

1
2
3
prompt:要设置为True
hide_input:要设置为True
confirmation_prompt:会自动为我们进行密码的二次验证
  

测试结果如下

错误的输入

正确的输入

原文地址https://www.cnblogs.com/bainianminguo/p/10629785.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值