Issue3079
Created on 2008-06-11 17:23 by skip.montanaro, last changed 2008-06-13 16:46 by skip.montanaro.
|
msg67999 - (view) |
Author: Skip Montanaro (skip.montanaro) |
Date: 2008-06-11 17:23 |
|
This seems like a bug in optparse.OptionParser:
def exit(self, status=0, msg=None):
if msg:
sys.stderr.write(msg)
sys.exit(status)
def error(self, msg):
"""error(msg : string)
Print a usage message incorporating 'msg' to stderr and exit.
If you override this in a subclass, it should not return -- it
should either exit or raise an exception.
"""
self.print_usage(sys.stderr)
self.exit(2, "%s: error: %s\n" % (self.get_prog_name(), msg))
By default I think it should raise an exception when it encounters an
error, not exit. Programmers shouldn't be forced to subclass code in
the standard library to get recommended practice.
If you feel this behavior can't be changed in 2.6 it should at least
be corrected in 3.0.
The cruel irony is that inside OptionParser.parse_args it actually
catches both BadOptionError and OptionValueError but suppresses them
by calling self.error() within the except block... *arrgggghhh*...
The correct behavior there is (in my opinion) to get rid of the
try/except statement altogether and just let the exceptions propagate.
Other calls to self.error() should be replaced with suitable raise
statements.
Skip
|
|
msg68149 - (view) |
Author: Antoine Pitrou (pitrou) |
Date: 2008-06-13 12:34 |
|
The current behaviour is useful in that most of time, it is convenient
to let OptionParser display a standard error message and bail out.
However, having an attribute on the OptionParser object (e.g.
exit_on_errors) to be able to change this behaviour is a reasonable
proposition.
|
|
msg68156 - (view) |
Author: Georg Brandl (georg.brandl) |
Date: 2008-06-13 13:27 |
|
I agree with Antoine that the standard behavior is what you want in most
simple command-line scripts.
It's easy enough to replace the parser's exit function to just print the
message, or raise an exception.
|
|
msg68171 - (view) |
Author: Skip Montanaro (skip.montanaro) |
Date: 2008-06-13 16:46 |
|
Georg> I agree with Antoine that the standard behavior is what you want in most
Georg> simple command-line scripts.
Georg> It's easy enough to replace the parser's exit function to just print the
Georg> message, or raise an exception.
Check the code. Most of the time error is called without an exception
having been raised. In one case it actually is called and swallows an
exception that the code did raise. It would be preferable in my mind to
always raise an exception. If you want, the default can be to catch them
and ignore them as error() does now, but as it stands you can't raise
anything more specific than OptParseError, and that's just a punt even
though specific problems were detected by the code.
Skip
|
|
| Date |
User |
Action |
Args |
| 2008-06-13 16:46:57 | skip.montanaro | set | messages:
+ msg68171 |
| 2008-06-13 13:27:01 | georg.brandl | set | nosy:
+ georg.brandl messages:
+ msg68156 |
| 2008-06-13 12:34:12 | pitrou | set | nosy:
+ pitrou messages:
+ msg68149 |
| 2008-06-13 02:12:37 | gagenellina | set | nosy:
+ gagenellina |
| 2008-06-11 23:46:47 | skip.montanaro | link | issue3084 superseder |
| 2008-06-11 17:30:55 | skip.montanaro | set | versions:
+ Python 2.6, Python 3.0 |
| 2008-06-11 17:23:27 | skip.montanaro | create | |
|