def _validate_args(self, swfuncs, tailargs): if six.get_method_function(self.help) in swfuncs: raise ShowHelp() if six.get_method_function(self.helpall) in swfuncs: raise ShowHelpAll() if six.get_method_function(self.version) in swfuncs: raise ShowVersion() requirements = {} exclusions = {} for swinfo in self._switches_by_func.values(): if swinfo.mandatory and not swinfo.func in swfuncs: raise MissingMandatorySwitch( "Switch %s is mandatory" % ("/".join( ("-" if len(n) == 1 else "--") + n for n in swinfo.names), )) requirements[swinfo.func] = set(self._switches_by_name[req] for req in swinfo.requires) exclusions[swinfo.func] = set(self._switches_by_name[exc] for exc in swinfo.excludes) # TODO: compute topological order gotten = set(swfuncs.keys()) for func in gotten: missing = set(f.func for f in requirements[func]) - gotten if missing: raise SwitchCombinationError( "Given %s, the following are missing %r" % (swfuncs[func].swname, [self._switches_by_func[f].names[0] for f in missing])) invalid = set(f.func for f in exclusions[func]) & gotten if invalid: raise SwitchCombinationError( "Given %s, the following are invalid %r" % (swfuncs[func].swname, [swfuncs[f].swname for f in invalid])) m_args, m_varargs, _, m_defaults = inspect.getargspec(self.main) max_args = six.MAXSIZE if m_varargs else len(m_args) - 1 min_args = len(m_args) - 1 - (len(m_defaults) if m_defaults else 0) if len(tailargs) < min_args: raise PositionalArgumentsError( "Expected at least %d positional arguments, got %r" % (min_args, tailargs)) elif len(tailargs) > max_args: raise PositionalArgumentsError( "Expected at most %d positional arguments, got %r" % (max_args, tailargs)) ordered = [(f, a) for _, f, a in sorted([(sf.index, f, sf.val) for f, sf in swfuncs.items()])] return ordered, tailargs
def _validate_args(self, swfuncs, tailargs): if six.get_method_function(self.help) in swfuncs: raise ShowHelp() if six.get_method_function(self.helpall) in swfuncs: raise ShowHelpAll() if six.get_method_function(self.version) in swfuncs: raise ShowVersion() requirements = {} exclusions = {} for swinfo in self._switches_by_func.values(): if swinfo.mandatory and not swinfo.func in swfuncs: raise MissingMandatorySwitch( "Switch %s is mandatory" % ("/".join(("-" if len(n) == 1 else "--") + n for n in swinfo.names),) ) requirements[swinfo.func] = set(self._switches_by_name[req] for req in swinfo.requires) exclusions[swinfo.func] = set(self._switches_by_name[exc] for exc in swinfo.excludes) # TODO: compute topological order gotten = set(swfuncs.keys()) for func in gotten: missing = set(f.func for f in requirements[func]) - gotten if missing: raise SwitchCombinationError( "Given %s, the following are missing %r" % (swfuncs[func].swname, [self._switches_by_func[f].names[0] for f in missing]) ) invalid = set(f.func for f in exclusions[func]) & gotten if invalid: raise SwitchCombinationError( "Given %s, the following are invalid %r" % (swfuncs[func].swname, [swfuncs[f].swname for f in invalid]) ) m_args, m_varargs, _, m_defaults = inspect.getargspec(self.main) max_args = six.MAXSIZE if m_varargs else len(m_args) - 1 min_args = len(m_args) - 1 - (len(m_defaults) if m_defaults else 0) if len(tailargs) < min_args: raise PositionalArgumentsError("Expected at least %d positional arguments, got %r" % (min_args, tailargs)) elif len(tailargs) > max_args: raise PositionalArgumentsError("Expected at most %d positional arguments, got %r" % (max_args, tailargs)) ordered = [(f, a) for _, f, a in sorted([(sf.index, f, sf.val) for f, sf in swfuncs.items()])] return ordered, tailargs
def _validate_args(self, swfuncs, tailargs): if six.get_method_function(self.help) in swfuncs: raise ShowHelp() if six.get_method_function(self.helpall) in swfuncs: raise ShowHelpAll() if six.get_method_function(self.version) in swfuncs: raise ShowVersion() requirements = {} exclusions = {} for swinfo in self._switches_by_func.values(): if swinfo.mandatory and not swinfo.func in swfuncs: raise MissingMandatorySwitch( T_("Switch {0} is mandatory").format("/".join( ("-" if len(n) == 1 else "--") + n for n in swinfo.names))) requirements[swinfo.func] = set( self._switches_by_name[req] for req in swinfo.requires) exclusions[swinfo.func] = set( self._switches_by_name[exc] for exc in swinfo.excludes) # TODO: compute topological order gotten = set(swfuncs.keys()) for func in gotten: missing = set(f.func for f in requirements[func]) - gotten if missing: raise SwitchCombinationError( T_("Given {0}, the following are missing {1}").format( swfuncs[func].swname, [self._switches_by_func[f].names[0] for f in missing])) invalid = set(f.func for f in exclusions[func]) & gotten if invalid: raise SwitchCombinationError( T_("Given {0}, the following are invalid {1}").format( swfuncs[func].swname, [swfuncs[f].swname for f in invalid])) m = six.getfullargspec(self.main) max_args = six.MAXSIZE if m.varargs else len(m.args) - 1 min_args = len(m.args) - 1 - (len(m.defaults) if m.defaults else 0) if len(tailargs) < min_args: raise PositionalArgumentsError( ngettext( "Expected at least {0} positional argument, got {1}", "Expected at least {0} positional arguments, got {1}", min_args).format(min_args, tailargs)) elif len(tailargs) > max_args: raise PositionalArgumentsError( ngettext("Expected at most {0} positional argument, got {1}", "Expected at most {0} positional arguments, got {1}", max_args).format(max_args, tailargs)) # Positional arguement validataion if hasattr(self.main, 'positional'): tailargs = self._positional_validate( tailargs, self.main.positional, self.main.positional_varargs, m.args[1:], m.varargs) elif hasattr(m, 'annotations'): args_names = list(m.args[1:]) positional = [None] * len(args_names) varargs = None # All args are positional, so convert kargs to positional for item in m.annotations: if item == m.varargs: varargs = m.annotations[item] elif item != 'return': positional[args_names.index(item)] = m.annotations[item] tailargs = self._positional_validate(tailargs, positional, varargs, m.args[1:], m.varargs) ordered = [(f, a) for _, f, a in sorted([(sf.index, f, sf.val) for f, sf in swfuncs.items()])] return ordered, tailargs