argparse 是 Python 标准库中推荐的命令行解析模块
1、创建一个解析器——创建 ArgumentParser() 对象
2、添加参数——调用 add_argument() 方法添加参数
3、解析参数——使用 parse_args() 解析添加的参数
import argparse
import cv2
parser = argparse.ArgumentParser(description='test')
# '-' 参数 可不输入
parser.add_argument('-v', '--version', help='print the version information')
parser.add_argument('-a', '--add', type=int, help='take the sum of the input plus 100')
# 无'-' / '--' 参数 必须输入
parser.add_argument('bdd', type=int, help='take the sum of the input plus 100')
# '--' 参数 必须输入
parser.add_argument("--x", help="x-coordinate", type=int)
args = parser.parse_args()
sum = args.add + 100
answer = args.bdd**2
x = args.x
if args.add > 10:
print sum
print cv2.__version__
else:
print answer
print x
python test.py -a 12 0 --x 0
# -a:12 bdd:0 --x:0