OptParse and Constant values

J

J

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top