Beispiel #1
0
 def __iadd__(self, other):
    """ Append a value to the list """
    if isinstance(self.callback, collections.abc.Callable):
       other = self.callback(self.cast(other))
    self.data += self.validate(other)
    range = self.validateRange(self.range)
    name = self.name
    if self.actual: name = self.actual
    if len(self.data) > range[1]:
       raise TooManyValues("Expecting at most %s values for option '%s'." % (range[1], name))
    return self
Beispiel #2
0
    def getArgument(self, args, range=None, delim=',', forcedarg=False):
        """ Parse argument """

        range = self.validateRange(range or [1, 1])

        # If we have an optional default, make sure that the range reflects that
        if range[0] < 2 and self.optional not in [None, [], ()]:
            range[0] = 0

        # No arguments left
        if not args:
            # Arguments that aren't mandatory can bail out now.
            if isinstance(self, GenericArgument) and not (self.mandatory):
                return None, args

            if range[0] > 0:
                name = self.name
                if self.actual: name = self.actual
                if range[0] != range[1]:
                    raise TooFewValues(
                        "Expecting at least %s value(s) for '%s'." %
                        (range[0], name))
                elif range[0] == 1:
                    raise TooFewValues("Expecting a value for '%s'." % name)
                else:
                    raise TooFewValues("Expecting %s values for '%s'." %
                                       (range[0], name))
            if self.optional:
                return self.optional, args
            else:
                return None, args

        # If delimiter is whitespace...
        if not delim or not str(delim).strip():
            new_args, args = self._getSpaceDelimitedArguments(args, range)
            delim = ' '

        else:
            new_args = []
            while self._hasFollowingArgument(args, delim):
                forcedarg = False

                # If the current argument ends with a delimiter
                if args[0].strip()[-1] == delim:
                    new_args.append(args.pop(0).strip()[:-1])

                # If next argument is an isolated delimiter character
                elif len(args) > 1 and args[1].strip() == delim:
                    new_args.append(args.pop(0).strip())
                    args.pop(0)

                # If next argument begins with a delimiter
                else:
                    if args[0].startswith(delim):
                        new_args.append(args.pop(0).strip()[1:])
                    else:
                        new_args.append(args.pop(0).strip())

                    # If this argument ends with a delimiter, rip it off
                    # and put it back into the stream.
                    if new_args[-1].endswith(delim):
                        args.insert(0, delim)
                        new_args[-1] = new_args[-1][:-1]

            # The following argument is always part of the list
            # unless the user accidentally put a trailing delimiter.
            if forcedarg or ConfigManager.has_following_argument(args):
                new_args.append(args.pop(0).strip())

            if new_args and new_args[-1].startswith(delim):
                new_args[-1] = new_args[-1][1:]

        if len(range) == 2 and range[0] == 1 and range[1] == 1:
            pass
        else:
            _args = []
            for i in new_args:
                _args += i.split(delim)
            new_args = _args

        # Check number of values
        name = self.name
        if self.actual: name = self.actual
        if len(new_args) < range[0]:
            plural = 's'
            if range[0] == 1:
                plural = ''
            raise TooFewValues("Expecting at least %s value%s for '%s'." %
                               (range[0], plural, name))
        elif len(new_args) > range[1]:
            raise TooManyValues("Expecting at most %s values for '%s'." %
                                (range[1], name))

        # Collapse argument list to a value if possible
        if not new_args:
            if self.optional is not None:
                new_args = self.optional
            else:
                new_args = None
        elif len(new_args) < 2:
            new_args = new_args[0]

        return new_args, args