Python argparse 模块的使用

0、argparse模块可用于添加和解析命令行的参数。

1、根据位置添加

# encoding=utf-8
import argparse

parser = argparse.ArgumentParser(description='My test argparse description')  # 创建解析对象
parser.add_argument('first_arg')  # 根据位置添加参数,第一个参数
args = parser.parse_args()  # 解析参数
print(args.first_arg)  # 获取第一个位置的参数的值
"""
测试
python test1.py                 ==>  报错 too few arguments
python test1.py aaa             ==>  输出 aaa
python test1.py aaa bbb         ==>  报错 unrecognized arguments: bbb
python test1.py aaa bbb ccc     ==>  报错 unrecognized arguments: bbb ccc
"""

2、添加指定key的参数

# encoding=utf-8
import argparse

parser = argparse.ArgumentParser(description='My test argparse description')  # 创建解析对象
parser.add_argument('-v', '--version')  # 添加指定key的参数
args = parser.parse_args()  # 解析参数
print('args.version:{}'.format(args.version))  # 获取key为version的参数
"""
测试
python test2.py                     ==>      输出 args.version:None
python test2.py aaa                 ==>      错误 unrecognized arguments: a
python test2.py -v                  ==>      错误 argument -v/--version: expected one argument
python test2.py -v aaa              ==>      输出 args.version:aaa
python test2.py --version bbb       ===>     输出 args.version:bbb
python test2.py -v aaa vvv          ==>      错误 unrecognized arguments: vvv
python test2.py --version bbb  vvv  ===>     错误 unrecognized arguments: vvv
"""

2.1、2中的例子-v后面都需要跟参数,否则报错argument -v/--version: expected one argument

可以通过添加action='store_true'参数,修改成-v后面不跟参数,默认不跟就是True,跟啥就是啥,如果没有-v就是False

# encoding=utf-8
import argparse

parser = argparse.ArgumentParser(description='My test argparse description')  # 创建解析对象
parser.add_argument('-v', '--version', action='store_true')  # 添加指定key的参数
args = parser.parse_args()  # 解析参数
print('args.version:{}'.format(args.version))  # 获取key为version的参数
"""
测试
python test2.py                     ==>      输出 args.version:False
python test2.py aaa                 ==>      错误 unrecognized arguments: aaa
python test2.py -v                  ==>      输出 args.version:True
python test2.py -v aaa              ==>      输出 args.version:aaa
python test2.py --version bbb       ===>     输出 args.version:bbb
python test2.py -v aaa vvv          ==>      错误 unrecognized arguments: vvv
python test2.py --version bbb  vvv  ===>     错误 unrecognized arguments: vvv
"""

3、定义参数的类型type

# encoding=utf-8
import argparse

parser = argparse.ArgumentParser(description='My test argparse description')  # 创建解析对象
parser.add_argument('-v', '--version', type=int, help='int类型的版本号')  # 添加指定key的参数
args = parser.parse_args()  # 解析参数
print('args.version:{}'.format(args.version))  # 获取key为version的参数
"""
测试
python test2.py                     ==>      输出 args.version:None
python test2.py aaa                 ==>      错误 unrecognized arguments: aaa
python test2.py -v                  ==>      错误 argument -v/--version: expected one argument
python test2.py -v aaa              ==>      错误 argument -v/--version: invalid int value: 'aaa'
python test2.py -v 111              ==>      输出 args.version:111 
python test2.py -v 111  222         ==>      错误 unrecognized arguments: 222
"""

4、定义选择的范围choices

# encoding=utf-8
import argparse

parser = argparse.ArgumentParser(description='My test argparse description')  # 创建解析对象
parser.add_argument('-v', '--version', type=int, help='int类型的版本号', choices=[1, 2, 3])  # 添加指定key的参数
args = parser.parse_args()  # 解析参数
print('args.version:{}'.format(args.version))  # 获取key为version的参数
"""
测试
python test2.py                     ==>      输出 args.version:None
python test2.py aaa                 ==>      错误 unrecognized arguments: aaa
python test2.py -v                  ==>      错误 argument -v/--version: expected one argument
python test2.py -v aaa              ==>      错误 argument -v/--version: invalid int value: 'aaa'
python test2.py -v 111              ==>      argument -v/--version: invalid choice: 111 (choose from 1, 2, 3
python test2.py -v 1                ==>      输出 args.version:1
"""

备注:以上例子默认不加- v参数时,获取到的参数是None,可以通过设置default=xxx来设置默认值。

 

学习链接 :https://www.jianshu.com/p/a41fbd4919f8

posted @ 2021-07-29 17:32  南风丶轻语  阅读(177)  评论(0编辑  收藏  举报