Rhu*_*gga 3 python optparse python-2.4
我在这个项目中使用python 2.4,所以我使用的是optparse.运行此代码时出现以下错误:
Traceback (most recent call last):
File "./clientNFSLatMonME.py", line 49, in ?
debug,verbose,interval = parseOptions()
File "./clientNFSLatMonME.py", line 43, in parseOptions
if (args.interval < 1) or (args.interval > MAX_INTERVAL):
AttributeError: 'tuple' object has no attribute 'interval'
Run Code Online (Sandbox Code Playgroud)
代码如下:
MAX_INTERVAL = 1800
def parseOptions():
parser = OptionParser()
parser.add_option("-d", "--debug", dest="debug", action="store_true", help="enable additional debugging output")
parser.add_option("-v", "--verbose", dest="verbose", action="store_true", help="enable additional console output")
parser.add_option("-i", "--interval", dest="interval", action="store", type="int", default=900, help="specify the time interval, default is 900, maximum is 1800")
args = parser.parse_args()
if (args.interval < 1) or (args.interval > MAX_INTERVAL):
print "Error: interval must be between 1 and " + str(MAX_INTERVAL) + ", terminating."
system.exit(1)
return args.debug, args.verbose, args.interval
debug,verbose,interval = parseOptions()
Run Code Online (Sandbox Code Playgroud)
该parser.parse_args()
方法返回一个元组,包含已解析的选项和其余参数.
打开那个元组; 该公约是用options
在解析开关和args
用于
options, args = parser.parse_args()
Run Code Online (Sandbox Code Playgroud)
并用于options
引用解析的命令行开关:
if options.interval < 1 or options.interval > MAX_INTERVAL:
# ...
return options.debug, options.verbose, options.interval
Run Code Online (Sandbox Code Playgroud)
该范围检查也可以使用链式比较来表示:
if not (0 > options.interval >= MAX_INTERVAL):
# ...
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
2498 次 |
最近记录: |