我正在编写一个通用函数来检查某些命令行选项是否存在,并返回包含这些选项的对象。到目前为止,使用optparse
(我仅限于使用python2.6.2),我得到了:
def getConfig( usage, needInputFile=False, needPhase=False ):
parser = OptionParser( usage=usage )
if ( needInputFile ):
parser.add_option("-f", "--file", dest = "filename",
help = "Read report from FILE", metavar="FILE")
if ( needPhase ):
parser.add_option("-p", "--phase", dest = "phase",
help = "Processing phase")
( options, args ) = parser.parse_args()
if ( needInputFile and options.filename == None ):
parser.error("Filename must be provided.")
if ( needPhase and options.phase == None ):
parser.error("Phase must be provided.")
return options
这很好用,但是我现在想通过相同的机制提供程序名和某些环境变量,这样它就可以用于将要使用它的一些不同脚本。在
我的直觉是将optparse选项复制到字典中,并添加程序名和额外的env。返回前将var信息放入字典中。在
我很想知道人们对这个应用程序的看法。有没有更好/更容易被接受的方法?在
目前没有回答
相关问题 更多 >
编程相关推荐