Beispiel #1
0
    def _hasFollowingArgument(self, args, delim):
        """ Return boolean indicating the existence of another argument """
        if not ConfigManager.has_following_argument(args):
            return 0

        # No arguments left
        if not args:
            return 0

        # The current argument ends with the proper delimiter
        if args[0].strip()[-1] == delim:
            return 1

        # The next argument begins with the proper delimiter
        if len(args) > 1 and args[1].strip()[0] == delim:
            return 1

        return 0
Beispiel #2
0
    def _hasFollowingArgument(self, args, delim):
        """ Return boolean indicating the existence of another argument """
        if not ConfigManager.has_following_argument(args):
            return 0

        # No arguments left
        if not args:
            return 0

        # The current argument ends with the proper delimiter
        if args[0].strip()[-1] == delim:
            return 1

        # The next argument begins with the proper delimiter
        if len(args) > 1 and args[1].strip()[0] == delim:
            return 1

        return 0
Beispiel #3
0
    def _getSpaceDelimitedArguments(self, args, range=[1, '*']):
        """ Slurp up any following arguments that don't begin with '-' """
        minimum, maximum = self.validateRange(range)
        arg_len = len(args)
        new_args = []
        while ConfigManager.has_following_argument(args):
            new_args.append(args.pop(0))

        # If all of the remaining command line components are arguments as
        # opposed to options, don't return them as arguments to the
        # last option.
        if isinstance(self, GenericArgument):
            return new_args, args
        else:
            if len(new_args) == arg_len:
                minimum = min(len(new_args), minimum)
                return new_args[:minimum], new_args[minimum:]
            else:
                return new_args, args
Beispiel #4
0
    def _getSpaceDelimitedArguments(self, args, range=None):
        """ Slurp up any following arguments that don't begin with '-' """
        minimum, maximum = self.validateRange(range or [1, '*'])
        arg_len = len(args)
        new_args = []
        while ConfigManager.has_following_argument(args):
            new_args.append(args.pop(0))

        # If all of the remaining command line components are arguments as
        # opposed to options, don't return them as arguments to the
        # last option.
        if isinstance(self, GenericArgument):
            return new_args, args
        else:
            if len(new_args) == arg_len:
                minimum = min(len(new_args), minimum)
                return new_args[:minimum], new_args[minimum:]
            else:
                return new_args, args
Beispiel #5
0
    def getArgument(self, args, range=[1, 1], delim=',', forcedarg=False):
        """ Parse argument """

        range = self.validateRange(range[:])

        # 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 len(new_args) < 1:
            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
Beispiel #6
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