OptParse and Constant values

J dreadpiratejeff at gmail.com
Thu Aug 12 12:19:22 EDT 2010


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_*)

I'm importing syslog like so:

from syslog import *

so all the syslog constants should be available to me at run time (at
least that's my understanding) so I'm doing something wrong here or
just not understanding how to do a constant as an option.

The basic gist of the above is that I'm writing a tool to do basic
syslog testing by injecting messages and making sure they end up in
the correct log file...  I can do all of this manually or in a loop by
looping through the various log levels, however, I want to be able to
specify them at run-time using an optional argument.  Any ideas on how
to make this work, or at least why it's not doing what I thought it
would do?

Cheers
Jeff



More information about the Python-list mailing list