Ejemplo n.º 1
0
 def unsatisfied(self, ba):
     m = self.get_meta(ba)
     if m.sub is not None:
         raise errors.MissingRequiredArguments((self,))
     if m.get_deriv().unsatisfied:
         return super(DecoratedArgumentParameter, self).unsatisfied(ba)
     else:
         return False
Ejemplo n.º 2
0
 def coerce_value(self, arg, ba):
     val = super(DecoratedArgumentParameter, self).coerce_value(arg, ba)
     d = self.get_meta(ba).pop_sub()
     if d is None:
         if self.cli.required:
             raise errors.MissingRequiredArguments(self.cli.required)
         args = []
         kwargs = {}
     else:
         args = d.args
         kwargs = d.kwargs
     return self.decorator(val, *args, **kwargs)
Ejemplo n.º 3
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
Ejemplo n.º 4
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