--- /usr/lib/python2.6/optparse.py 2009-04-18 22:43:42.000000000 -0400 +++ myoptparse.py 2009-07-21 18:02:50.000000000 -0400 @@ -81,7 +81,7 @@ _ = gettext -class OptParseError (Exception): +class OptParseError(Exception): def __init__(self, msg): self.msg = msg @@ -89,7 +89,7 @@ return self.msg -class OptionError (OptParseError): +class OptionError(OptParseError): """ Raised if an Option instance is created with invalid or inconsistent arguments. @@ -105,18 +105,18 @@ else: return self.msg -class OptionConflictError (OptionError): +class OptionConflictError(OptionError): """ Raised if conflicting options are added to an OptionParser. """ -class OptionValueError (OptParseError): +class OptionValueError(OptParseError): """ Raised if an invalid option value is encountered on the command line. """ -class BadOptionError (OptParseError): +class BadOptionError(OptParseError): """ Raised if an invalid option is seen on the command line. """ @@ -126,7 +126,7 @@ def __str__(self): return _("no such option: %s") % self.opt_str -class AmbiguousOptionError (BadOptionError): +class AmbiguousOptionError(BadOptionError): """ Raised if an ambiguous option is seen on the command line. """ @@ -139,7 +139,7 @@ % (self.opt_str, ", ".join(self.possibilities))) -class HelpFormatter: +class HelpFormatter(object): """ Abstract base class for formatting option help. OptionParser @@ -345,7 +345,7 @@ return ", ".join(opts) -class IndentedHelpFormatter (HelpFormatter): +class IndentedHelpFormatter(HelpFormatter): """Format help with indented section bodies. """ @@ -364,7 +364,7 @@ return "%*s%s:\n" % (self.current_indent, "", heading) -class TitledHelpFormatter (HelpFormatter): +class TitledHelpFormatter(HelpFormatter): """Format help with underlined section headers. """ @@ -429,7 +429,7 @@ NO_DEFAULT = ("NO", "DEFAULT") -class Option: +class Option(object): """ Instance attributes: _short_opts : [string] @@ -462,7 +462,8 @@ 'callback_args', 'callback_kwargs', 'help', - 'metavar'] + 'metavar', + 'required'] # The set of actions allowed by option parsers. Explicitly listed # here so the constructor can validate its arguments. @@ -719,6 +720,13 @@ raise OptionError( "callback_kwargs supplied for non-callback option", self) + def _check_required(self): + if self.required and not self.takes_value(): + raise OptionError("required flag set for option that doesn't take a value", self) + print self.dest, self.default + if self.required and self.default != ('NO', 'DEFAULT'): + raise OptionError("required flag set for option that has default value", self) + CHECK_METHODS = [_check_action, _check_type, @@ -726,7 +734,8 @@ _check_dest, _check_const, _check_nargs, - _check_callback] + _check_callback, + _check_required] # -- Miscellaneous methods ----------------------------------------- @@ -763,6 +772,7 @@ return tuple([self.check_value(opt, v) for v in value]) def process(self, opt, value, values, parser): + parser.option_seen[self] = 1 # First, convert the value(s) to the right type. Howl if any # value(s) are bogus. @@ -819,7 +829,7 @@ def isbasestring(x): return isinstance(x, basestring) -class Values: +class Values(object): def __init__(self, defaults=None): if defaults: @@ -884,7 +894,7 @@ return getattr(self, attr) -class OptionContainer: +class OptionContainer(object): """ Abstract base class. @@ -1072,7 +1082,7 @@ return "\n".join(result) -class OptionGroup (OptionContainer): +class OptionGroup(OptionContainer): def __init__(self, parser, title, description=None): self.parser = parser @@ -1102,7 +1112,7 @@ return result -class OptionParser (OptionContainer): +class OptionParser(OptionContainer): """ Class attributes: @@ -1251,6 +1261,7 @@ self.rargs = None self.largs = None self.values = None + self.option_seen = {} # -- Simple modifier methods --------------------------------------- @@ -1393,9 +1404,11 @@ Check that the supplied option values and leftover arguments are valid. Returns the option values and leftover arguments (possibly adjusted, possibly completely new -- whatever you - like). Default implementation just returns the passed-in - values; subclasses may override as desired. + like). """ + for option in self.option_list + sum((optiongroup.option_list for optiongroup in self.option_groups), []): + if isinstance(option, Option) and option.required and not self.option_seen.has_key(option): + self.error("%s not supplied" % (option, )) return (values, args) def _process_args(self, largs, rargs, values):