python获取参数

argparse是python的一个命令行参数模块,可以解析命令行参数,生成帮助.

示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#!/usr/bin/python

from argparse import ArgumentParser

argv = ArgumentParser(usage='it is usage tip', description='this is a test')
argv.add_argument('--string', default='strings', type=int, help='the first string argument')
argv.add_argument('--int', default=1, type=str, help='the second argument')

args = argv.parse_args()

#打印所有参数
print args

#打印某一个参数
print args.string
print args.int

参数-h --help来显示帮助信息。