一.基本:
5.default参数设置参数默认值
import argparse
parser = argparse.ArgumentParser(description="desc")
parser.parse_args()
# 参数设置
parser.add_argument('-v', '--verbosity', help="test action", action="store_true")
二.参数:
1.参数若为单纯字符串(前无-)的时候,所输入的参数值即赋值给该参数。
eg:
parser.add_argument('verbosity', help="test action")
python arg.py test
'verbosity'=test
2.若为有-或者--的参数,则需要打开开关并输入参数,或者用action存储后直接打开开关
eg:
parser.add_argument('--verbosity', help="test action")
python arg.py --verbosity test
'verbosity'=test
或者
parser.add_argument('--verbosity', help="test action", action="store_true")
python arg.py --verbosity
'verbosity'=True 此方法常用于设置可选开关
4.add_argument()的choices参数可设置参数值的范围
eg:
parser.add_argument('-v', help='test choices', type=int, choices=[0, 1, 2])
5.default参数设置参数默认值
6.设置矛盾选项:
group = parser.add_mutually_exclusive_group()
group.add_argument("-v", "--verbose", action="store_true")
group.add_argument("-q", "--quiet", action="store_true")
至此-v和-q不能同时出现