Esempio n. 1
0
    def _validate(self, args, kwargs):
        """Validate option registration arguments."""
        def error(exception_type, arg_name=None, **msg_kwargs):
            if arg_name is None:
                arg_name = args[0] if args else '<unknown>'
            raise exception_type(self.scope, arg_name, **msg_kwargs)

        if not args:
            error(NoOptionNames)
        # validate args.
        for arg in args:
            if not arg.startswith('-'):
                error(OptionNameDash, arg_name=arg)
            if not arg.startswith('--') and len(arg) > 2:
                error(OptionNameDoubleDash, arg_name=arg)

        # Validate kwargs.
        if 'implicit_value' in kwargs and kwargs['implicit_value'] is None:
            error(ImplicitValIsNone)

        # Note: we check for list here, not list_option, because we validate the provided kwargs,
        # not the ones we modified.  However we temporarily also allow list_option, until the
        # deprecation is complete.
        if 'member_type' in kwargs and kwargs.get(
                'type', str) not in [list, list_option]:
            error(MemberTypeNotAllowed, type_=kwargs.get('type', str).__name__)

        if kwargs.get('member_type', str) not in self._allowed_member_types:
            error(InvalidMemberType,
                  member_type=kwargs.get('member_type', str).__name__)

        for kwarg in kwargs:
            if kwarg not in self._allowed_registration_kwargs:
                error(InvalidKwarg, kwarg=kwarg)

            # Ensure `daemon=True` can't be passed on non-global scopes (except for `recursive=True`).
            if (kwarg == 'daemon' and self._scope != GLOBAL_SCOPE
                    and kwargs.get('recursive') is False):
                error(InvalidKwargNonGlobalScope, kwarg=kwarg)

        removal_version = kwargs.get('removal_version')
        if removal_version is not None:
            validate_removal_semver(removal_version)
Esempio n. 2
0
  def _validate(self, args, kwargs):
    """Validate option registration arguments."""
    def error(exception_type, arg_name=None, **msg_kwargs):
      if arg_name is None:
        arg_name = args[0] if args else '<unknown>'
      raise exception_type(self.scope, arg_name, **msg_kwargs)

    if not args:
      error(NoOptionNames)
    # validate args.
    for arg in args:
      if not arg.startswith('-'):
        error(OptionNameDash, arg_name=arg)
      if not arg.startswith('--') and len(arg) > 2:
        error(OptionNameDoubleDash, arg_name=arg)

    # Validate kwargs.
    if 'implicit_value' in kwargs and kwargs['implicit_value'] is None:
      error(ImplicitValIsNone)

    # Note: we check for list here, not list_option, because we validate the provided kwargs,
    # not the ones we modified.  However we temporarily also allow list_option, until the
    # deprecation is complete.
    if 'member_type' in kwargs and kwargs.get('type', str) not in [list, list_option]:
      error(MemberTypeNotAllowed, type_=kwargs.get('type', str).__name__)

    if kwargs.get('member_type', str) not in self._allowed_member_types:
      error(InvalidMemberType, member_type=kwargs.get('member_type', str).__name__)

    for kwarg in kwargs:
      if kwarg not in self._allowed_registration_kwargs:
        error(InvalidKwarg, kwarg=kwarg)

      # Ensure `daemon=True` can't be passed on non-global scopes (except for `recursive=True`).
      if (kwarg == 'daemon' and self._scope != GLOBAL_SCOPE and kwargs.get('recursive') is False):
        error(InvalidKwargNonGlobalScope, kwarg=kwarg)

    removal_version = kwargs.get('removal_version')
    if removal_version is not None:
      validate_removal_semver(removal_version)