Commit 80c33e56 authored by Guido van Rossum's avatar Guido van Rossum

Contribution from Gerrit Holl:

This patch changes the string-based exceptions to class-based
exceptions, so that you can fetch the unknown option as an
attribute.  As far as I know, it is backward compatible.

[The new exception class is called GetoptError; the name error is an
alias for compatibility.]
parent b9bdfc6a
...@@ -7,15 +7,33 @@ and `--'). Long options similar to those supported by GNU software ...@@ -7,15 +7,33 @@ and `--'). Long options similar to those supported by GNU software
may be used as well via an optional third argument. This module may be used as well via an optional third argument. This module
provides a single function and an exception: provides a single function and an exception:
Gerrit Holl <gerrit@nl.linux.org> moved the string-based exceptions
to class-based exceptions.
getopt() -- Parse command line options getopt() -- Parse command line options
error -- Exception (string) raised when bad options are found GetoptError -- exception (class) raised with 'opt' attribute, which is the
option involved with the exception.
""" """
# Long option support added by Lars Wirzenius <liw@iki.fi>. # Long option support added by Lars Wirzenius <liw@iki.fi>.
import string import string
error = 'getopt.error' class GetoptError(Exception):
opt = ''
msg = ''
def __init__(self, *args):
self.args = args
if len(args) == 1:
self.msg = args[0]
elif len(args) == 2:
self.msg = args[0]
self.opt = args[1]
def __str__(self):
return self.msg
error = GetoptError # backward compatibility
def getopt(args, shortopts, longopts = []): def getopt(args, shortopts, longopts = []):
"""getopt(args, options[, long_options]) -> opts, args """getopt(args, options[, long_options]) -> opts, args
...@@ -72,10 +90,10 @@ def do_longs(opts, opt, longopts, args): ...@@ -72,10 +90,10 @@ def do_longs(opts, opt, longopts, args):
if has_arg: if has_arg:
if optarg is None: if optarg is None:
if not args: if not args:
raise error, 'option --%s requires argument' % opt raise GetoptError('option --%s requires argument' % opt, opt)
optarg, args = args[0], args[1:] optarg, args = args[0], args[1:]
elif optarg: elif optarg:
raise error, 'option --%s must not have an argument' % opt raise GetoptError('option --%s must not have an argument' % opt, opt)
opts.append(('--' + opt, optarg or '')) opts.append(('--' + opt, optarg or ''))
return opts, args return opts, args
...@@ -90,11 +108,11 @@ def long_has_args(opt, longopts): ...@@ -90,11 +108,11 @@ def long_has_args(opt, longopts):
continue continue
if y != '' and y != '=' and i+1 < len(longopts): if y != '' and y != '=' and i+1 < len(longopts):
if opt == longopts[i+1][:optlen]: if opt == longopts[i+1][:optlen]:
raise error, 'option --%s not a unique prefix' % opt raise GetoptError('option --%s not a unique prefix' % opt, opt)
if longopts[i][-1:] in ('=', ): if longopts[i][-1:] in ('=', ):
return 1, longopts[i][:-1] return 1, longopts[i][:-1]
return 0, longopts[i] return 0, longopts[i]
raise error, 'option --' + opt + ' not recognized' raise GetoptError('option --%s not recognized' % opt, opt)
def do_shorts(opts, optstring, shortopts, args): def do_shorts(opts, optstring, shortopts, args):
while optstring != '': while optstring != '':
...@@ -102,7 +120,7 @@ def do_shorts(opts, optstring, shortopts, args): ...@@ -102,7 +120,7 @@ def do_shorts(opts, optstring, shortopts, args):
if short_has_arg(opt, shortopts): if short_has_arg(opt, shortopts):
if optstring == '': if optstring == '':
if not args: if not args:
raise error, 'option -%s requires argument' % opt raise GetoptError('option -%s requires argument' % opt, opt)
optstring, args = args[0], args[1:] optstring, args = args[0], args[1:]
optarg, optstring = optstring, '' optarg, optstring = optstring, ''
else: else:
...@@ -114,7 +132,7 @@ def short_has_arg(opt, shortopts): ...@@ -114,7 +132,7 @@ def short_has_arg(opt, shortopts):
for i in range(len(shortopts)): for i in range(len(shortopts)):
if opt == shortopts[i] != ':': if opt == shortopts[i] != ':':
return shortopts[i+1:i+2] == ':' return shortopts[i+1:i+2] == ':'
raise error, 'option -%s not recognized' % opt raise GetoptError('option -%s not recognized' % opt, opt)
if __name__ == '__main__': if __name__ == '__main__':
import sys import sys
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment