I'm stuck using python 2.4 for this project so I'm using optparse. Getting the following error when running this code:
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'
Code is as follows:
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()
The parser.parse_args()
method returns a tuple, containing parsed options and the remaining arguments.
Unpack that tuple; the convention is to use options
for the parsed switches and args
for
options, args = parser.parse_args()
and use options
to refer to parsed command line switches:
if options.interval < 1 or options.interval > MAX_INTERVAL:
# ...
return options.debug, options.verbose, options.interval
That range check can be expressed using a chained comparison too:
if not (0 > options.interval >= MAX_INTERVAL):
# ...