1

I have these below lines in my program

parser = OptionParser()

parser.add_option("-t","--TIMEOUT", dest="timeout", type="int",  help="timeout in seconds")

if parser.has_option("-t") and options.timeout<=0:
   print "Timeout if specified must be greater than zero"
   sys.exit(CLI_ERROR)

That print statement above is being printed because parser.has_option("-t") is evaluating to true even if no -t option is specified to this script. Am I missing something here. Thanks in advance for your help.

yalkris
  • 2,596
  • 5
  • 31
  • 51
  • Works fine for me: (python 2.6.5, ubuntu). – mgilson Oct 18 '12 at 19:09
  • Do you have another version of python you can test your code on? It's possible this is a bug in python 2.4 which was fixed in a later release... – mgilson Oct 18 '12 at 19:15
  • 1
    @dm03514 -- As far as I'm aware, argparse doesn't have the ability to query/manipulate your parser like this. I'm actually a little surprised. Maybe the devs thought it was too difficult to implement and didn't think it was worth it... – mgilson Oct 18 '12 at 19:17

2 Answers2

2

You have to actually parse the options first. parser.has_option just checks to see if the parser understands the given option (which it does, since you used add_option to add it).

Thus, use

from optparse import OptionParser

parser = OptionParser()

parser.add_option("-t","--TIMEOUT", dest="timeout", type="int",  help="timeout in seconds")

options, args = parser.parse_args()
if options.timeout is not None and options.timeout <= 0:
    print "Timeout if specified must be greater than zero"
    sys.exit(CLI_ERROR)
nneonneo
  • 171,345
  • 36
  • 312
  • 383
  • `parser.has_option` worked prior to parsing for me. Of course, the code then raised a `NameError` since `options` isn't defined yet. But that doesn't explain what OP is seeing. OP says that the statement is being printed (unconditionally) which means that "options" is defined somewhere in code which isn't shown. – mgilson Oct 18 '12 at 19:13
  • @mgilson: How can it work before parsing? Think about it: how can `parser` know what arguments to parse (hint: it's not always `sys.argv`)? `parser.has_option` checks to see if the *parser supports the option*. – nneonneo Oct 18 '12 at 19:22
  • @nneonneo -- Why does the parser need to parse `sys.argv` in order to figure out whether it supports an option? whether or not `'-t'` is in `sys.argv` (or wherever optparse pulls the arguments from) is irrelevant. As I understand it, `parser.has_option` is just checking to see if the parser knows what to do if it happens to encounter a `-t` in the iterator it parses. Of course, your solution works, but it doesn't explain the strange behavior OP is seeing. – mgilson Oct 18 '12 at 19:38
  • What happens in the event that the line `parser.add_option('-t',...)` was never executed? Then your `options` will have no `timeout` attribute and you'll get a `NameError`. That's the sort of thing that `parser.has_option` is supposed to help check for (as I understand it). – mgilson Oct 18 '12 at 19:40
  • I think there's a misunderstanding. He wants to know if `-t` was passed on the command line. `parser.has_option` doesn't do that. – nneonneo Oct 18 '12 at 20:52
1
(options, args) = parser.parse_args()
if options.timeout is not None and options.timeout <=0 :
.....

you should have a look at docopt https://github.com/docopt/docopt . great for command line interfaces

jassinm
  • 7,323
  • 3
  • 33
  • 42
  • It seems that this will raise an `AttributeError` if that option isn't defined. – mgilson Oct 18 '12 at 19:11
  • This will not work because if I specify -t 0 execution will not go inside that if block because options.timeout is zero. My intention is to print that message if timeout is less than or equal to zero – yalkris Oct 18 '12 at 19:21