OptParse and Constant values
Robert Kern
robert.kern at gmail.com
Thu Aug 12 12:41:40 EDT 2010
On 8/12/10 11:19 AM, J wrote:
> How do you use OptParse with constants?
>
> Example:
> usage = 'Usage: %prog [OPTIONS]'
> parser = OptionParser(usage)
> parser.add_option('-l','--level',
> action='store_const',
> default=LOG_INFO,
> help='Set the log level to inject into syslog (either an\
> integer [0 - 7] or one of LOG_EMERG, LOG_ALERT,LOG_CRIT,\
> LOG_ERR, LOG_WARNING, LOG_NOTICE, LOG_INFO, LOG_DEBUG')
> (options,args) = parser.parse_args()
>
> print options
>
> always returns {'level':None} regarless of whether I call it with an
> int (0 - 7) or the const name (LOG_*)
http://docs.python.org/library/optparse#standard-option-actions
'store_const' means that the option is a flag without arguments and stores the
value provided by the 'const' keyword to add_option() (it defaults to None so
that's what you get when you use that flag). This is not what you want. You just
want the default 'store' action.
--
Robert Kern
"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco
More information about the Python-list
mailing list