argparse
命令行参数解析模块,原optparse已经停止开发,建议替换为argparse
在python2.7后默认加入
parser
ArgumentParser默认解析来源sys.argv,也可以提供显示参数进行解析。
构造参数中,通常只需关心description和epilog。前者显示程序标题,后者在帮助信息的尾部显示详细的版权、使用描述等。
程序代码:
[root@typhoeus79 20131105]# more test_argparse.py
#!/usr/bin/env python26
#-*- coding:utf-8 -*- from argparse import ArgumentParser p = ArgumentParser(description="Test Program",epilog="author:gu
osong") p.add_argument("-x",help="xxx...")
p.add_argument("-H",help="host...") p.print_help()
结果输出:
[root@typhoeus79 20131105]# ./test_argparse.py
usage: test_argparse.py [-h] [-x X] [-H H] Test Program optional arguments:
-h, --help show this help message and exit
-x X xxx...
-H H host... author:guosong
(待续)