Python命令模块argparse学习笔记(二)
argparse模块可以设置两种命令参数,一个是位置参数,一个是命令参数
-
位置参数
import argparse parser = argparse.ArgumentParser(description="The Help of Python") parser.add_argument("name") args = parser.parse_args() if args.name: print(args.name)
直接不带参数运行
报错,需要传个位置参数
打印了传的参数
-
可选参数
# -*- coding:utf-8 -*- __author__ = "MuT6 Sch01aR" import argparse parser = argparse.ArgumentParser(description="The Help of Python") parser.add_argument("-t","--thread") args = parser.parse_args() if args.thread: print(args.thread)
运行结果
没传参数也没有报错,所以参数是可选的,而且参数的顺序也不是按顺序的
-t和--thread都能进行传参数
后面加等号和直接传参数是一样的
可选参数用规定的符号来识别,如上例的-和--,其他的为位置参数,如果有位置参数,不传的话就会报错
-
设置命令参数的参数
help:参数描述信息
# -*- coding:utf-8 -*- __author__ = "MuT6 Sch01aR" import argparse parser = argparse.ArgumentParser(description="The Help of Python") parser.add_argument("-t","--thread",help="The Thread To Run") args = parser.parse_args() if args.thread: print(args.thread)
运行
type:参数类型,如果类型不对就会报错
# -*- coding:utf-8 -*- __author__ = "MuT6 Sch01aR" import argparse parser = argparse.ArgumentParser(description="The Help of Python") parser.add_argument("-t","--thread",type=int,help="The Thread To Run") args = parser.parse_args() if args.thread: print(args.thread)
参数类型设置为int类型
传入一个字符串
传入一个整型
成功执行
default:默认值
# -*- coding:utf-8 -*- __author__ = "MuT6 Sch01aR" import argparse parser = argparse.ArgumentParser(description="The Help of Python") parser.add_argument("-t","--thread",default=23,help="The Thread To Run") args = parser.parse_args() if args.thread: print(args.thread)
不传入任何参数,直接运行
dest:可作为参数名
# -*- coding:utf-8 -*- __author__ = "MuT6 Sch01aR" import argparse parser = argparse.ArgumentParser(description="The Help of Python") parser.add_argument("-t","--thread",dest="thread_num",help="The Thread To Run") args = parser.parse_args() if args.thread_num: #需要改为dest的值 print(args.thread_num)
运行
choices:可选择的值
# -*- coding:utf-8 -*- __author__ = "MuT6 Sch01aR" import argparse parser = argparse.ArgumentParser(description="The Help of Python") parser.add_argument("-t","--thread",choices=["one","two"],help="The Thread To Run") args = parser.parse_args() if args.thread: print(args.thread)
参数-t/--thread可选的值为one和two
运行
required:是否为必须参数,值为True或False
# -*- coding:utf-8 -*- __author__ = "MuT6 Sch01aR" import argparse parser = argparse.ArgumentParser(description="The Help of Python") parser.add_argument("-t","--thread",required=true,help="The Thread To Run") args = parser.parse_args() if args.thread: print(args.thread)
不传参数,运行
报错
const:保存一个常量,不能单独使用,可以和nargs和action中的部分参数一起使用
nargs:参数的数量,值为整数,*为任意个,+为一个或多个,当值为?时,首先从命令行获取参数,然后是从const获取参数,最后从default获得参数
# -*- coding:utf-8 -*- __author__ = "MuT6 Sch01aR" import argparse parser = argparse.ArgumentParser(description="The Help of Python") parser.add_argument("-t","--thread",nargs="*",help="The Thread To Run") args = parser.parse_args() if args.thread: print(args.thread)
运行结果
把传入的参数以列表输出
# -*- coding:utf-8 -*- __author__ = "MuT6 Sch01aR" import argparse parser = argparse.ArgumentParser(description="The Help of Python") parser.add_argument("-t","--thread",nargs="?",const=1,default=2,help="The Thread To Run") args = parser.parse_args() if args.thread: print(args.thread)
从const获取了值
action:默认值为store,store_true和store_false把值设置为True或False,store_const把值存入const中,count统计参数出现的次数,append把传入参数存为列表,append_const根据const存为列表
store:
# -*- coding:utf-8 -*- __author__ = "MuT6 Sch01aR" import argparse parser = argparse.ArgumentParser(description="The Help of Python") parser.add_argument("-t","--thread",action="store",help="The Thread To Run") args = parser.parse_args() if args.thread: print(args.thread)
运行结果
store_true和store_false:
# -*- coding:utf-8 -*- __author__ = "MuT6 Sch01aR" import argparse parser = argparse.ArgumentParser(description="The Help of Python") parser.add_argument("-t","--thread",action="store_true") parser.add_argument("-u","--url",action="store_false") args = parser.parse_args() if args: print(args)
运行
store_const:
# -*- coding:utf-8 -*- __author__ = "MuT6 Sch01aR" import argparse parser = argparse.ArgumentParser(description="The Help of Python") parser.add_argument("-t","--thread",action="store_const",const=12) args = parser.parse_args() if args: print(args)
运行结果
count:
# -*- coding:utf-8 -*- __author__ = "MuT6 Sch01aR" import argparse parser = argparse.ArgumentParser(description="The Help of Python") parser.add_argument("-t","--thread",action="count") args = parser.parse_args() if args: print(args)
运行结果
append:
# -*- coding:utf-8 -*- __author__ = "MuT6 Sch01aR" import argparse parser = argparse.ArgumentParser(description="The Help of Python") parser.add_argument("-t","--thread",action="append") args = parser.parse_args() if args: print(args)
运行结果
append_const:
# -*- coding:utf-8 -*- __author__ = "MuT6 Sch01aR" import argparse parser = argparse.ArgumentParser(description="The Help of Python") parser.add_argument("-t","--thread",action="append_const",const=1) args = parser.parse_args() if args: print(args)
运行结果