argparse模块主要用来解析参数
一般从这里开始,但是这段代码什么都不做,因为parser.parse_args()返回的是一个命名空间,而现在没有添加参数
import argparse
parser = argparse.ArgumentParser()
parser.parse_args()
而这段代码添加了一个参数’echo’,所以当在控制台输入参数时就可以用类似Java中 ‘.’的方式访问你的参数了:
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("echo")
args = parser.parse_args()
print('print my argument: ',args.echo)
E:\examples of coding\python\shiyanlou>python argparse_test.py my_input
print my argument: my_input
来一个高级点的。。。:
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("square", type = int,
help = 'display a square of a given number')
# 这里type = int表示只可以输入整数
parser.add_argument('-v', '--verbose', action = 'store_true',
help = 'increase out verbosity')
args = parser.parse_args()
answer = args.square**2
if args.verbose: # 如果输入了verbose参数
print("the square of {} equals {}".format(args.square, answer))
else: # 如果没有输入verbose参数
print(answer)
E:\examples of coding\python\shiyanlou>python argparse_test.py 4 -v
the square of 4 equals 16
E:\examples of coding\python\shiyanlou>python argparse_test.py -v 4
the square of 4 equals 16
E:\examples of coding\python\shiyanlou>python argparse_test.py 4
16
E:\examples of coding\python\shiyanlou>python argparse_test.py 4 --verbose
the square of 4 equals 16
还可以限制取值:
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("square", type = int,
help = 'display a square of a given number')
# 这里type = int表示只可以输入整数
parser.add_argument("-v", "--verbosity", type=int, choices=[0, 1, 2],
help = 'increase out verbosity')
# 其中verbosity 只可以取值为0,1,2
args = parser.parse_args()
answer = args.square**2
if args.verbosity == 2: # 如果输入的verbosity参数为2
print("the square of {} equals {}".format(args.square, answer))
elif args.verbosity == 1: # 如果输入的verbosity参数为1
print("{}^2 == {}".format(args.square, answer))
else: # 如果没有输入verbosity参数
print(answer)
E:\examples of coding\python\shiyanlou>python argparse_test.py 4 -v 2
the square of 4 equals 16
E:\examples of coding\python\shiyanlou>python argparse_test.py 4 -v 1
4^2 == 16
E:\examples of coding\python\shiyanlou>python argparse_test.py 4 -v 0
16
E:\examples of coding\python\shiyanlou>python argparse_test.py 4 -v 3
usage: argparse_test.py [-h] [-v {0,1,2}] square
argparse_test.py: error: argument -v/--verbosity: invalid choice: 3 (choose from 0, 1, 2)
还可以定义action=’count’, 统计对应关键字出现的次数:
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("square", type = int,
help = 'display a square of a given number')
# 这里type = int表示只可以输入整数
parser.add_argument("-v", "--verbosity", action = 'count', default=0,
help = 'increase out verbosity')
# 其中verbosity 出现的次数默认为0,可以计数
args = parser.parse_args()
answer = args.square**2
if args.verbosity >= 2: # 如果输入的verbosity参数为2
print("the square of {} equals {}".format(args.square, answer))
elif args.verbosity == 1: # 如果输入的verbosity参数为1
print("{}^2 == {}".format(args.square, answer))
else: # 如果没有输入verbosity参数
print(answer)
E:\examples of coding\python\shiyanlou>python argparse_test.py 4 -vv
the square of 4 equals 16
E:\examples of coding\python\shiyanlou>python argparse_test.py 4 -vvv
the square of 4 equals 16
E:\examples of coding\python\shiyanlou>python argparse_test.py 4 --verbosity --verbosity
the square of 4 equals 16
E:\examples of coding\python\shiyanlou>python argparse_test.py 4 -v
4^2 == 16
E:\examples of coding\python\shiyanlou>python argparse_test.py 4
16