import argparse
# 导入模块
parser = argparse.ArgumentParser()# 实例化'''
argparse.Argument常用参数:
- prog -- The name of the program (default: sys.argv[0])
- description -- A description of what the program does
- epilog -- Text following the argument descriptions
- parents -- Parsers whose arguments should be copied into this one
- prefix_chars -- Characters that prefix optional arguments
- argument_default -- The default value for all arguments
'''
parser.add_argument(default ='Unknown',type=int,
required =True,
choices =range(120),help='The current age by 2020''--age')# 添加参数'''
add_argument常用参数:
- default -- 命令行无参数输入时的默认值
- required -- True/False, 该参数是否必要
- type -- 参数类型,默认str
- choices -- 参数值只能在一定范围内选择
- help -- 参数的说明信息,在命令行加-h或--help参数运行该命令
- dest -- 设置变量名,默认变量名为-或--后字符串
- nargs -- 设置参数个数,int; '?':0或1个参数; '*':0或所有参数; '+':所有且至少一个参数
'''
args = parser.parse_args()# 调用parse_args方法,返回args <class 'argparse.Namespace'>
age = args.age
# args调出添加的参数