Beispiel #1
0
    def register(self, *args, **kwargs):
        """Register an option."""
        if self._frozen:
            raise FrozenRegistration(self.scope, args[0])

        # Prevent further registration in enclosing scopes.
        ancestor = self._parent_parser
        while ancestor:
            ancestor._freeze()
            ancestor = ancestor._parent_parser

        # Boolean options always have an implicit boolean-typed default.  They can never be None.
        # We make that default explicit here.
        if kwargs.get('type') == bool and kwargs.get('default') is None:
            kwargs['default'] = not self._ensure_bool(
                kwargs.get('implicit_value', True))

        # Record the args. We'll do the underlying parsing on-demand.
        self._option_registrations.append((args, kwargs))
        if self._parent_parser:
            for arg in args:
                existing_scope = self._parent_parser._existing_scope(arg)
                if existing_scope is not None:
                    raise Shadowing(
                        self.scope,
                        arg,
                        outer_scope=self._scope_str(existing_scope))
        for arg in args:
            if arg in self._known_args:
                raise OptionAlreadyRegistered(self.scope, arg)
        self._known_args.update(args)
Beispiel #2
0
    def register(self, *args, **kwargs) -> None:
        """Register an option."""
        if args:
            name, dest = self.parse_name_and_dest(*args, **kwargs)
            self._check_deprecated(name, kwargs, print_warning=False)

        if kwargs.get("type") == bool:
            default = kwargs.get("default")
            if default is None:
                # Unless a tri-state bool is explicitly opted into with the `UnsetBool` default value,
                # boolean options always have an implicit boolean-typed default. We make that default
                # explicit here.
                kwargs["default"] = not self._ensure_bool(
                    kwargs.get("implicit_value", True))
            elif default is UnsetBool:
                kwargs["default"] = None

        # Record the args. We'll do the underlying parsing on-demand.
        self._option_registrations.append((args, kwargs))

        # Look for shadowing options up and down the hierarchy.
        args_set = set(args)
        for parent in self._parents_transitive():
            self._check_shadowing(parent.scope, parent._known_args, self.scope,
                                  args_set)
        for child in self._children_transitive():
            self._check_shadowing(self.scope, args_set, child.scope,
                                  child._known_args)

        # And look for direct conflicts
        for arg in args:
            if arg in self._known_args:
                raise OptionAlreadyRegistered(self.scope, arg)
        self._known_args.update(args)
Beispiel #3
0
  def register(self, *args, **kwargs):
    """Register an option."""
    if self._frozen:
      raise FrozenRegistration(self.scope, args[0])

    # Prevent further registration in enclosing scopes.
    ancestor = self._parent_parser
    while ancestor:
      ancestor._freeze()
      ancestor = ancestor._parent_parser

    if kwargs.get('type') == bool:
      default = kwargs.get('default')
      if default is None:
        # Unless a tri-state bool is explicitly opted into with the `UnsetBool` default value,
        # boolean options always have an implicit boolean-typed default. We make that default
        # explicit here.
        kwargs['default'] = not self._ensure_bool(kwargs.get('implicit_value', True))
      elif default is UnsetBool:
        kwargs['default'] = None

    # Record the args. We'll do the underlying parsing on-demand.
    self._option_registrations.append((args, kwargs))
    if self._parent_parser:
      for arg in args:
        existing_scope = self._parent_parser._existing_scope(arg)
        if existing_scope is not None:
          raise Shadowing(self.scope, arg, outer_scope=self._scope_str(existing_scope))
    for arg in args:
      if arg in self._known_args:
        raise OptionAlreadyRegistered(self.scope, arg)
    self._known_args.update(args)
Beispiel #4
0
    def register(self, *args, **kwargs) -> None:
        """Register an option."""
        if args:
            dest = self.parse_dest(*args, **kwargs)
            self._check_deprecated(dest, kwargs, print_warning=False)

        if self.is_bool(kwargs):
            default = kwargs.get("default")
            if default is None:
                # Unless a tri-state bool is explicitly opted into with the `UnsetBool` default value,
                # boolean options always have an implicit boolean-typed default. We make that default
                # explicit here.
                kwargs["default"] = not self.ensure_bool(
                    kwargs.get("implicit_value", True))
            elif default is UnsetBool:
                kwargs["default"] = None

        # Record the args. We'll do the underlying parsing on-demand.
        self._option_registrations.append((args, kwargs))

        # Look for direct conflicts.
        for arg in args:
            if arg in self._known_args:
                raise OptionAlreadyRegistered(self.scope, arg)
        self._known_args.update(args)
Beispiel #5
0
    def register(self, *args, **kwargs):
        """Register an option."""
        if self._frozen:
            raise FrozenRegistration(self.scope, args[0])

        # Prevent further registration in enclosing scopes.
        ancestor = self._parent_parser
        while ancestor:
            ancestor._freeze()
            ancestor = ancestor._parent_parser

        def check_deprecated_types(kwarg_name):
            t = kwargs.get(kwarg_name)
            # First check for deprecated direct use of the internal types.
            if t == list_option:
                deprecated_conditional(
                    lambda: True, '0.0.83',
                    'list_option is deprecated for option {} in scope {}. '
                    'Use type=list.'.format(args[0], self.scope))
            elif t == dict_option:
                deprecated_conditional(
                    lambda: True, '0.0.83',
                    'dict_option is deprecated for option {} in scope {}. '
                    'Use type=dict.'.format(args[0], self.scope))

        check_deprecated_types('type')
        check_deprecated_types('member_type')

        action = kwargs.get('action')
        # Temporary munging to effectively turn type=list options into list options,
        # for uniform handling.  From here on, type=list is an error.
        # TODO: Remove after type=list deprecation.
        if action == 'append':
            if 'type' in kwargs:
                kwargs['member_type'] = kwargs['type']
            kwargs['type'] = list
            del kwargs['action']
            deprecated_conditional(
                lambda: True, '0.0.83',
                "action='append' is deprecated for option {} in scope {}. "
                "Use type=list.".format(args[0], self.scope))

        # Temporary munging to effectively turn type='target_list_option' options into list options,
        # with member type 'target_option', for uniform handling.
        # TODO: Remove after target_list_option deprecation.
        if kwargs.get('type') == target_list_option:
            kwargs['type'] = list
            kwargs['member_type'] = target_option
            deprecated_conditional(
                lambda: True, '0.0.83',
                'target_list_option is deprecated for option {} in scope {}. '
                'Use type=list, member_type=target_option.'.format(
                    args[0], self.scope))

        # Temporary munging to effectively turn action='store_true' into bool-typed options.
        # From here on, action='store_true' is an error.  Ditto for store_false.
        # TODO: Remove after action='store_true'/'store_false' deprecation.
        if action == 'store_true':
            kwargs['type'] = bool
            kwargs['implicit_value'] = True
            del kwargs['action']
            deprecated_conditional(
                lambda: True, '0.0.83',
                "action='store_true' is deprecated for option {} in scope {}. "
                "Use type=bool.".format(args[0], self.scope))
        elif action == 'store_false':
            kwargs['type'] = bool
            kwargs['implicit_value'] = False
            del kwargs['action']
            deprecated_conditional(
                lambda: True, '0.0.83',
                "action='store_false' is deprecated for option {} in scope {}. "
                "Use type=bool, implicit_value=False.".format(
                    args[0], self.scope))

        # Boolean options always have an implicit boolean-typed default.  They can never be None.
        # We make that default explicit here.
        if kwargs.get('type') == bool and kwargs.get('default') is None:
            kwargs['default'] = not self._ensure_bool(
                kwargs.get('implicit_value', True))

        # Record the args. We'll do the underlying parsing on-demand.
        self._option_registrations.append((args, kwargs))
        if self._parent_parser:
            for arg in args:
                existing_scope = self._parent_parser._existing_scope(arg)
                if existing_scope is not None:
                    raise Shadowing(
                        self.scope,
                        arg,
                        outer_scope=self._scope_str(existing_scope))
        for arg in args:
            if arg in self._known_args:
                raise OptionAlreadyRegistered(self.scope, arg)
        self._known_args.update(args)