optparse 模块
optparse是Python自带的模块,无需安装。
#!/usr/bin/env python
# coding: utf-8
from optparse import OptionParser
usage = 'usage: %prog -f <filename> -i <ip address>'
parser = OptionParser(usage = usage)
parser.add_option('-f', '--file', type = 'string', dest = 'filename', help = 'which file')
parser.add_option('-i', '--ip', type = 'int', dest = 'address', help = 'which host')
(options, args) = parser.parse_args()
print str(options)
print str(args)
$ python opt.py -h
Usage: opt.py -f <filename> -i <ip address>
Options:
-h, --help show this help message and exit
-f FILENAME, --file=FILENAME
which file
-i ADDRESS, --ip=ADDRESS
which host
$ python opt.py --file gps.txt -i 10