[Python-checkins] CVS: distutils/distutils fancy_getopt.py,1.6,1.7
Greg Ward
python-dev@python.org
Thu, 20 Apr 2000 21:44:05 -0400 (EDT)
Update of /projects/cvsroot/distutils/distutils
In directory newcnri:/tmp/cvs-serv16864
Modified Files:
fancy_getopt.py
Log Message:
Continuing the refactoring: deleted the old 'fancy_getopt()' function,
leaving in its place a tiny wrapper around the FancyGetopt class
for backwards compatibility.
Index: fancy_getopt.py
===================================================================
RCS file: /projects/cvsroot/distutils/distutils/fancy_getopt.py,v
retrieving revision 1.6
retrieving revision 1.7
diff -C2 -r1.6 -r1.7
*** fancy_getopt.py 2000/04/21 01:41:54 1.6
--- fancy_getopt.py 2000/04/21 01:44:00 1.7
***************
*** 11,15 ****
# created 1999/03/03, Greg Ward
! __revision__ = "$Id: fancy_getopt.py,v 1.6 2000/04/21 01:41:54 gward Exp $"
import sys, string, re
--- 11,15 ----
# created 1999/03/03, Greg Ward
! __revision__ = "$Id: fancy_getopt.py,v 1.7 2000/04/21 01:44:00 gward Exp $"
import sys, string, re
***************
*** 265,389 ****
# class FancyGetopt
-
-
- def fancy_getopt (options, negative_opt, object, args):
-
- # The 'options' table is a list of 3-tuples:
- # (long_option, short_option, help_string)
- # if an option takes an argument, its long_option should have '='
- # appended; short_option should just be a single character, no ':' in
- # any case. If a long_option doesn't have a corresponding
- # short_option, short_option should be None. All option tuples must
- # have long options.
-
- # Build the short_opts string and long_opts list, remembering how
- # the two are tied together
-
- short_opts = [] # we'll join 'em when done
- long_opts = []
- short2long = {}
- attr_name = {}
- takes_arg = {}
-
- for option in options:
- try:
- (long, short, help) = option
- except ValueError:
- raise DistutilsGetoptError, \
- "invalid option tuple " + str (option)
-
- # Type-check the option names
- if type (long) is not StringType or len (long) < 2:
- raise DistutilsGetoptError, \
- "long option '%s' must be a string of length >= 2" % \
- long
-
- if (not ((short is None) or
- (type (short) is StringType and len (short) == 1))):
- raise DistutilsGetoptError, \
- "short option '%s' must be None or string of length 1" % \
- short
-
- long_opts.append (long)
-
- if long[-1] == '=': # option takes an argument?
- if short: short = short + ':'
- long = long[0:-1]
- takes_arg[long] = 1
- else:
-
- # Is option is a "negative alias" for some other option (eg.
- # "quiet" == "!verbose")?
- alias_to = negative_opt.get(long)
- if alias_to is not None:
- if not takes_arg.has_key(alias_to) or takes_arg[alias_to]:
- raise DistutilsGetoptError, \
- ("option '%s' is a negative alias for '%s', " +
- "which either hasn't been defined yet " +
- "or takes an argument") % (long, alias_to)
-
- long_opts[-1] = long
- takes_arg[long] = 0
-
- else:
- takes_arg[long] = 0
-
-
- # Now enforce some bondage on the long option name, so we can later
- # translate it to an attribute name in 'object'. Have to do this a
- # bit late to make sure we've removed any trailing '='.
- if not longopt_re.match (long):
- raise DistutilsGetoptError, \
- ("invalid long option name '%s' " +
- "(must be letters, numbers, hyphens only") % long
-
- attr_name[long] = string.translate (long, longopt_xlate)
- if short:
- short_opts.append (short)
- short2long[short[0]] = long
-
- # end loop over 'options'
-
- short_opts = string.join (short_opts)
- try:
- (opts, args) = getopt.getopt (args, short_opts, long_opts)
- except getopt.error, msg:
- raise DistutilsArgError, msg
-
- global _option_order # blechh! should OO-ify this module
- _option_order = []
-
- for (opt, val) in opts:
- if len (opt) == 2 and opt[0] == '-': # it's a short option
- opt = short2long[opt[1]]
-
- elif len (opt) > 2 and opt[0:2] == '--':
- opt = opt[2:]
-
- else:
- raise DistutilsInternalError, \
- "this can't happen: bad option string '%s'" % opt
-
- if not takes_arg[opt]: # boolean option?
- if val != '': # shouldn't have a value!
- raise DistutilsInternalError, \
- "this can't happen: bad option value '%s'" % value
-
- alias = negative_opt.get (opt)
- if alias:
- opt = alias
- val = 0
- else:
- val = 1
-
- attr = attr_name[opt]
- setattr (object, attr, val)
- _option_order.append ((opt, val))
-
- # for opts
-
- return args
-
- # fancy_getopt()
--- 265,268 ----