Prev: How to parse a sentence using grammars provided by nltk?
Next: inline exception handling in python
From: J on 12 Aug 2010 12:19 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
From: Robert Kern on 12 Aug 2010 12:41 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
From: J on 12 Aug 2010 13:47 On Thu, Aug 12, 2010 at 12:41, Robert Kern <robert.kern(a)gmail.com> wrote: > On 8/12/10 11:19 AM, J wrote: >> >> How do you use OptParse with constants? > 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. Thanks Robert... Sigh... head -> desk -> repeat :)
|
Pages: 1 Prev: How to parse a sentence using grammars provided by nltk? Next: inline exception handling in python |