Beispiel #1
0
    def redispatch_short_arg(self, rest, ba, i):
        """Processes the rest of an argument as if it was a new one prefixed
        with one dash.

        For instance when ``-a`` is a flag in ``-abcd``, the object implementing
        it will call this to proceed as if ``-a -bcd`` was passed."""
        if not rest:
            return
        try:
            nparam = ba.sig.aliases['-' + rest[0]]
        except KeyError as e:
            raise errors.UnknownOption(e.args[0])
        orig_args = ba.in_args
        ba.in_args = ba.in_args[:i] + ('-' + rest, ) + ba.in_args[i + 1:]
        try:
            nparam.read_argument(ba, i)
        finally:
            ba.in_args = orig_args
        ba.unsatisfied.discard(nparam)
Beispiel #2
0
    def process_arguments(self):
        """Process the arguments in `.in_args`, setting the `.func`,
        `.post_name`, `.args` and `.kwargs` attributes as a result.

        This methods reads `str`'s from `.in_args`. For each one, it finds the
        relevant `Parameter` instance in `.posparam` or `.namedparam` and
        delegates processing to it """
        self.posparam = iter(self.sig.positional)
        self.namedparams = dict(self.sig.aliases)
        self.unsatisfied = set(self.sig.required)
        self.not_provided = set(self.sig.optional)
        self.sticky = None
        self.posarg_only = False
        self.skip = 0

        with _SeekFallbackCommand():
            for i, arg in enumerate(self.in_args):
                if self.skip > 0:
                    self.skip -= 1
                    continue
                with errors.SetArgumentErrorContext(pos=i, val=arg, ba=self):
                    if self.posarg_only or len(arg) < 2 or arg[0] != '-':
                        if self.sticky is not None:
                            param = self.sticky
                        else:
                            try:
                                param = next(self.posparam)
                            except StopIteration:
                                exc = errors.TooManyArguments(self.in_args[i:])
                                exc.__cause__ = None
                                raise exc
                    elif arg == '--':
                        self.posarg_only = True
                        continue
                    else:
                        if arg.startswith('--'):
                            name = arg.partition('=')[0]
                        else:
                            name = arg[:2]
                        try:
                            param = self.namedparams[name]
                        except KeyError:
                            raise errors.UnknownOption(name)
                    with errors.SetArgumentErrorContext(param=param):
                        param.read_argument(self, i)
                        param.apply_generic_flags(self)

        if not self.func:
            if self.unsatisfied:
                unsatisfied = []
                for p in self.unsatisfied:
                    with errors.SetArgumentErrorContext(param=p):
                        if p.unsatisfied(self):
                            unsatisfied.append(p)
                if unsatisfied:
                    raise errors.MissingRequiredArguments(unsatisfied)

            for p in self.sig.parameters.values():
                p.post_parse(self)

        del self.sticky, self.posarg_only, self.skip, self.unsatisfied, self.not_provided
Beispiel #3
0
    def __init__(self, sig, args, name):
        self.sig = sig
        self.name = name
        self.in_args = tuple(args)
        self.func = None
        self.post_name = []
        self.args = []
        self.kwargs = {}
        self.meta = {}

        self.posparam = iter(self.sig.positional)
        self.sticky = None
        self.posarg_only = False
        self.skip = 0
        self.unsatisfied = set(self.sig.required)

        with _SeekFallbackCommand():
            for i, arg in enumerate(self.in_args):
                if self.skip > 0:
                    self.skip -= 1
                    continue
                with errors.SetArgumentErrorContext(pos=i, val=arg, ba=self):
                    if self.posarg_only or len(arg) < 2 or arg[0] != '-':
                        if self.sticky is not None:
                            param = self.sticky
                        else:
                            try:
                                param = next(self.posparam)
                            except StopIteration:
                                exc = errors.TooManyArguments(self.in_args[i:])
                                exc.__cause__ = None
                                raise exc
                    elif arg == '--':
                        self.posarg_only = True
                        continue
                    else:
                        if arg.startswith('--'):
                            name = arg.partition('=')[0]
                        else:
                            name = arg[:2]
                        try:
                            param = self.sig.aliases[name]
                        except KeyError:
                            raise errors.UnknownOption(name)
                    with errors.SetArgumentErrorContext(param=param):
                        param.read_argument(self, i)
                        param.apply_generic_flags(self)

        if not self.func:
            if self.unsatisfied:
                unsatisfied = []
                for p in self.unsatisfied:
                    with errors.SetArgumentErrorContext(param=p):
                        if p.unsatisfied(self):
                            unsatisfied.append(p)
                if unsatisfied:
                    raise errors.MissingRequiredArguments(unsatisfied)

            for p in self.sig.parameters.values():
                p.post_parse(self)

        del self.sticky, self.posarg_only, self.skip, self.unsatisfied