1、用python写的程序往往需要处理命令行的参数,如何才能简单地控制这些参数,并且要求使其尽量符合Linux的规范。
方法就是使用optparse模块
2、例子程序如下:
from opt parse import OptionParser
parser=OptionParser()#定义一个对象
#添加一个参数,第一个引号表示短格式提示符,第二个引号表示长格式提示符,dest表示参数内容,help表示帮助的提示信息
parser.add_option( "-f","--file",dest="filename",help="The file to display")
options,arguments=parser.parse_args()
#使用命令行参数
if options.filename:
.......
else:
.......
3、当需要多个命令行参数时候,可以使用多条如下语句
parser.add_option("","",dest=,help=)