OptParse and Constant values

Discussion in 'Python' started by J, Aug 12, 2010.

  1. J

    J Guest

    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
     
    J, Aug 12, 2010
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Christopher M. Lusardi
    Replies:
    1
    Views:
    4,094
  2. Martin Magnusson
    Replies:
    2
    Views:
    508
    John Harrison
    Oct 8, 2004
  3. Replies:
    4
    Views:
    339
    Keith Thompson
    Dec 14, 2006
  4. Nirnimesh

    optparse: list out entered values

    Nirnimesh, May 25, 2007, in forum: Python
    Replies:
    2
    Views:
    694
    Steven Bethard
    May 25, 2007
  5. G G
    Replies:
    3
    Views:
    93
    Ben Bacarisse
    Apr 20, 2014
Loading...

Share This Page