Exemplo n.º 1
0
    def _check_type(self):
        if self.type is None:
            if self.action in self.ALWAYS_TYPED_ACTIONS:
                if self.choices is not None:
                    # The "choices" attribute implies "choice" type.
                    self.type = "choice"
                else:
                    # No type given?  "string" is the most sensible default.
                    self.type = "string"
        else:
            # Allow type objects or builtin type conversion functions
            # (int, str, etc.) as an alternative to their names.  (The
            # complicated check of __builtin__ is only necessary for
            # Python 2.1 and earlier, and is short-circuited by the
            # first check on modern Pythons.)
            import __builtin__
            if (type(self.type) is types.TypeType or
                (hasattr(self.type, "__name__") and
                 getattr(__builtin__, self.type.__name__, None) is self.type)):
                self.type = self.type.__name__

            if self.type == "str":
                self.type = "string"

            if self.type not in self.TYPES:
                raise OptionError("invalid option type: %r" % self.type, self)
            if self.action not in self.TYPED_ACTIONS:
                raise OptionError(
                    "must not supply a type for action %r" % self.action, self)
Exemplo n.º 2
0
 def _check_choice(self):
     if self.type == "choice":
         if self.choices is None:
             raise OptionError(
                 "must supply a list of choices for type 'choice'", self)
         elif type(self.choices) not in (types.TupleType, types.ListType):
             raise OptionError(
                 "choices must be a list of strings ('%s' supplied)" %
                 str(type(self.choices)).split("'")[1], self)
     elif self.choices is not None:
         raise OptionError(
             "must not supply choices for type %r" % self.type, self)
Exemplo n.º 3
0
 def _check_nargs(self):
     if self.action in self.TYPED_ACTIONS:
         if self.nargs is None:
             self.nargs = 1
     elif self.nargs is not None:
         raise OptionError(
             "'nargs' must not be supplied for action %r" % self.action,
             self)
Exemplo n.º 4
0
 def _check_type(self):
     if self.type is None:
         # XXX should factor out another class attr here: list of
         # actions that *require* a type
         if self.action in ("store", "append"):
             if self.choices is not None:
                 # The "choices" attribute implies "choice" type.
                 self.type = "choice"
             else:
                 # No type given?  "string" is the most sensible default.
                 self.type = "string"
     else:
         if self.type not in self.TYPES:
             raise OptionError("invalid option type: %r" % self.type, self)
         if self.action not in self.TYPED_ACTIONS:
             raise OptionError(
                 "must not supply a type for action %r" % self.action, self)
Exemplo n.º 5
0
 def _set_opt_strings(self, opts):
     for opt in opts:
         if len(opt) < 2:
             raise OptionError(
                 "invalid option string %r: "
                 "must be at least two characters long" % opt, self)
         elif len(opt) == 2:
             if not (opt[0] == "-" and opt[1] != "-"):
                 raise OptionError(
                     "invalid short option string %r: "
                     "must be of the form -x, (x any non-dash char)" % opt,
                     self)
             self._short_opts.append(opt)
         else:
             if not (opt[0:2] == "--" and opt[2] != "-"):
                 raise OptionError(
                     "invalid long option string %r: "
                     "must start with --, followed by non-dash" % opt, self)
             self._long_opts.append(opt)
Exemplo n.º 6
0
    def _check_type(self):
        if self.type is None:
            if self.action in self.ALWAYS_TYPED_ACTIONS:
                if self.choices is not None:
                    # The "choices" attribute implies "choice" type.
                    self.type = "choice"
                else:
                    # No type given?  "string" is the most sensible default.
                    self.type = "string"
        else:
            # Allow type objects as an alternative to their names.
            if hasattr(self.type, "__name__"):
                self.type = self.type.__name__
            if self.type == "str":
                self.type = "string"

            if self.type not in self.TYPES:
                raise OptionError("invalid option type: %s" % repr(self.type), self)
            if self.action not in self.TYPED_ACTIONS:
                raise OptionError(
                    "must not supply a type for action %s" % repr(self.action), self)
Exemplo n.º 7
0
 def _check_callback(self):
     if self.action == "callback":
         if not callable(self.callback):
             raise OptionError("callback not callable: %r" % self.callback,
                               self)
         if (self.callback_args is not None
                 and type(self.callback_args) is not types.TupleType):
             raise OptionError(
                 "callback_args, if supplied, must be a tuple: not %r" %
                 self.callback_args, self)
         if (self.callback_kwargs is not None
                 and type(self.callback_kwargs) is not types.DictType):
             raise OptionError(
                 "callback_kwargs, if supplied, must be a dict: not %r" %
                 self.callback_kwargs, self)
     else:
         if self.callback is not None:
             raise OptionError(
                 "callback supplied (%r) for non-callback option" %
                 self.callback, self)
         if self.callback_args is not None:
             raise OptionError(
                 "callback_args supplied for non-callback option", self)
         if self.callback_kwargs is not None:
             raise OptionError(
                 "callback_kwargs supplied for non-callback option", self)
Exemplo n.º 8
0
 def _set_attrs(self, attrs):
     for attr in self.ATTRS:
         if attrs.has_key(attr):
             setattr(self, attr, attrs[attr])
             del attrs[attr]
         else:
             if attr == 'default':
                 setattr(self, attr, NO_DEFAULT)
             else:
                 setattr(self, attr, None)
     if attrs:
         raise OptionError(
             "invalid keyword arguments: %s" % ", ".join(attrs.keys()),
             self)
Exemplo n.º 9
0
    def _check_dest(self):
        # No destination given, and we need one for this action.  The
        # self.type check is for callbacks that take a value.
        takes_value = (self.action in self.STORE_ACTIONS
                       or self.type is not None)
        if self.dest is None and takes_value:

            # Glean a destination from the first long option string,
            # or from the first short option string if no long options.
            if self._long_opts:
                # eg. "--foo-bar" -> "foo_bar"
                self.dest = self._long_opts[0][2:].replace('-', '_')
                if (self._long_opts_had_minus):
                    raise OptionError(
                        "If dest is not specified explicitly, long options"
                        " must be given with underscores instead of"
                        " minus signs.", self)
            else:
                self.dest = self._short_opts[0][1]
Exemplo n.º 10
0
 def _check_const(self):
     if self.action != "store_const" and self.const is not None:
         raise OptionError(
             "'const' must not be supplied for action %r" % self.action,
             self)
Exemplo n.º 11
0
 def _check_action(self):
     if self.action is None:
         self.action = "store"
     elif self.action not in self.ACTIONS:
         raise OptionError("invalid action: %r" % self.action, self)
 def _check_const(self):
     if self.action not in self.CONST_ACTIONS and self.const is not None:
         raise OptionError(
             "'const' must not be supplied for action %s" %
             repr(self.action), self)