[Python-checkins] CVS: distutils/distutils fancy_getopt.py,1.9,1.10
Greg Ward
python-dev@python.org
Fri, 21 Apr 2000 00:22:06 -0400 (EDT)
Update of /projects/cvsroot/distutils/distutils
In directory newcnri:/tmp/cvs-serv17377
Modified Files:
fancy_getopt.py
Log Message:
Added the capability for alias options.
Index: fancy_getopt.py
===================================================================
RCS file: /projects/cvsroot/distutils/distutils/fancy_getopt.py,v
retrieving revision 1.9
retrieving revision 1.10
diff -C2 -r1.9 -r1.10
*** fancy_getopt.py 2000/04/21 02:31:07 1.9
--- fancy_getopt.py 2000/04/21 04:22:01 1.10
***************
*** 11,15 ****
# created 1999/03/03, Greg Ward
! __revision__ = "$Id: fancy_getopt.py,v 1.9 2000/04/21 02:31:07 gward Exp $"
import sys, string, re
--- 11,15 ----
# created 1999/03/03, Greg Ward
! __revision__ = "$Id: fancy_getopt.py,v 1.10 2000/04/21 04:22:01 gward Exp $"
import sys, string, re
***************
*** 67,70 ****
--- 67,74 ----
self._build_index()
+ # 'alias' records (duh) alias options; {'foo': 'bar'} means
+ # --foo is an alias for --bar
+ self.alias = {}
+
# 'negative_alias' keeps track of options that are the boolean
# opposite of some other option
***************
*** 119,122 ****
--- 123,143 ----
+ def _check_alias_dict (self, aliases, what):
+ assert type(aliases) is DictionaryType
+ for (alias, opt) in aliases.items():
+ if not self.option_index.has_key(alias):
+ raise DistutilsGetoptError, \
+ ("invalid %s '%s': "
+ "option '%s' not defined") % (what, alias, alias)
+ if not self.option_index.has_key(opt):
+ raise DistutilsGetoptError, \
+ ("invalid %s '%s': "
+ "aliased option '%s' not defined") % (what, alias, opt)
+
+ def set_aliases (self, alias):
+ """Set the aliases for this option parser."""
+ self._check_alias_dict (alias, "alias")
+ self.alias = alias
+
def set_negative_aliases (self, negative_alias):
"""Set the negative aliases for this option parser.
***************
*** 124,139 ****
option names, both the key and value must already be defined
in the option table."""
!
! assert type(negative_alias) is DictionaryType
! for (negopt, opt) in negative_alias.items():
! if not self.option_index.has_key(negopt):
! raise DistutilsGetoptError, \
! ("invalid negative alias '%s': "
! "option '%s' not defined") % (negopt, negopt)
! if not self.option_index.has_key(opt):
! raise DistutilsGetoptError, \
! ("invalid negative alias '%s': "
! "aliased option '%s' not defined") % (negopt, opt)
!
self.negative_alias = negative_alias
--- 145,149 ----
option names, both the key and value must already be defined
in the option table."""
! self._check_alias_dict (negative_alias, "negative alias")
self.negative_alias = negative_alias
***************
*** 187,190 ****
--- 197,210 ----
self.takes_arg[long] = 0
+ # If this is an alias option, make sure its "takes arg" flag is
+ # the same as the option it's aliased to.
+ alias_to = self.alias.get(long)
+ if alias_to is not None:
+ if self.takes_arg[long] != self.takes_arg[alias_to]:
+ raise DistutilsGetoptError, \
+ ("invalid alias '%s': inconsistent with "
+ "aliased option '%s' (one of them takes a value, "
+ "the other doesn't") % (long, alias_to)
+
# Now enforce some bondage on the long option name, so we can
***************
*** 243,246 ****
--- 263,270 ----
raise DistutilsInternalError, \
"this can't happen: bad option string '%s'" % opt
+
+ alias = self.alias.get(opt)
+ if alias:
+ opt = alias
if not self.takes_arg[opt]: # boolean option?