Python的命令行参数解析利器:OptionParser
from optparse import OptionParser usage = \ """%prog [args] Example: %prog -k school -n 20 -s""" parser = OptionParser(usage=usage) parser.add_option("-k", "--keywords", dest="keywords", help="keywords to search") parser.add_option("-n", "--number", dest="number_threads", type="int", help="number of threads", default=10) parser.add_option("-s", "--skip", dest="skip", action="store_true", help="whether skip snonymous account or not", default=False) (options, args) = parser.parse_args() print options
测试结果如下(看起来相当专业):
C:\Documents and Settings\Administrator\桌面>test -help
Usage: test.py [args]
Example: test.py -k school -n 20 -s
Options:
-h, --help show this help message and exit
-k KEYWORDS, --keywords=KEYWORDS
keywords to search
-n NUMBER_THREADS, --number=NUMBER_THREADS
number of threads
-s, --skip whether skip snonymous account or not
C:\Documents and Settings\Administrator\桌面>test
{'keywords': None, 'skip': False, 'number_threads': 10}
C:\Documents and Settings\Administrator\桌面>test -k school -n 20 -s
{'keywords': 'school', 'skip': True, 'number_threads': 20}
C:\Documents and Settings\Administrator\桌面>test --keywords=school --number=20
--skip
{'keywords': 'school', 'skip': True, 'number_threads': 20}
Python OptionParser 官方文档:http://docs.python.org/library/optparse.html
呵呵,谢谢
VaTG790i.最好的<a href=http://www.kyfei.com>网站推广软件</a>,
非常好
....................
;ui;普i;uighur;ui;ui;个
在unix网络编程中看到了关于TCP/IP的一些内容,我感觉还是写的不够。正在下载中,一定
下载地址呢