Question of Optionparse

Pekka Niiranen pekka.niiranen at wlanmail.com
Mon Nov 29 17:51:07 EST 2004


Hi,

How can I STOP Optionparse to process boolean
value as parameter. See this:

 >>> parser = optparse.OptionParser()
 >>> parser.add_option("-x",dest='xxx', action="store_true",help="xxx")
 >>> parser.add_option("-r",dest='root',help="directory",type="string")
 >>> args = ["-r", "d:", "-x"]
 >>> parser.parse_args(args)
(<Values at 0x13bbbc0: {'xxx': True, 'root': 'd:'}>, [])

Last line is correct: boolean 'xxx' gets set 'True'
and parameter 'root' to 'd:'

However when value is NOT given for '-r' but '-x' exists:

 >>> args = ["-r", "-x"]
 >>> parser.parse_args(args)
(<Values at 0x13ccf80: {'xxx': None, 'root': '-x'}>, [])

This is BS: I expected 'root' to be None and 'xxx' to be 'True'.
How can I make it so?

Another question: Is there a way to store options
directly into user defined dictionary without assignment
like this:

my_environment_values = {}
(options, args) = parser.parse_args()
environment_values["xxx"] = options.xxx

-pekka-



More information about the Python-list mailing list