add Command.context_class
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Javier Domingo Cansino · committed by David Lord · parent 08a0d69 · Browse files · 1 file·+18 added−4 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
1 file+18 added−4 removed
- other:+18 added−4 removed
Provenance
- Commit
ca4e4d9- Tree
2bf2c0f- Author
Javier Domingo Cansino <javierdo1@gmail.com>- Committer
David Lord <davidism@gmail.com>- Parent
08a0d69
1 file changed+18 added−4 removed
modified:src/click/core.py+18 added-4 removed
599 unmodified lines600 # in defaults and make a new context for this command.601 if isinstance(callback, Command):602 other_cmd = callback603 callback = other_cmd.callback604- ctx = Context(other_cmd, info_name=other_cmd.name, parent=self)604+605 if callback is None:606 raise TypeError(607 "The given command does not have a callback that can be invoked."608 )609 610+ # Create a new context of the same type as this context.611+ ctx = type(self)(other_cmd, info_name=other_cmd.name, parent=self)612+613 for param in other_cmd.params:614 if param.name not in kwargs and param.expose_value:615 kwargs[param.name] = param.get_default(ctx)616 8 unmodified lines625 it. This cannot invoke callbacks directly, only other commands.626 """627 self, cmd = args[:2]628 626- # It's also possible to invoke another command which might or627- # might not have a callback.629+ # Can only forward to other commands, not direct callbacks.630 if not isinstance(cmd, Command):631 raise TypeError("Callback is not a command.")632 633 for param in self.params:53 unmodified lines687 :param context_settings: an optional dictionary with defaults that are688 passed to the context object.689 """690 691+ #: The context class to create with :meth:`make_context`.692+ #:693+ #: .. versionadded:: 8.0694+ context_class = Context695 #: the default for the :attr:`Context.allow_extra_args` flag.696 allow_extra_args = False697 #: the default for the :attr:`Context.allow_interspersed_args` flag.698 allow_interspersed_args = True24 unmodified lines723 """This function when given an info name and arguments will kick724 off the parsing and create a new :class:`Context`. It does not725 invoke the actual command callback though.726 727+ To quickly customize the context class used without overriding728+ this method, set the :attr:`context_class` attribute.729+730 :param info_name: the info name for this invokation. Generally this731 is the most descriptive name for the script or732 command. For the toplevel script it's usually733 the name of the script, for commands below it it's1 unmodified line735 :param args: the arguments to parse as list of strings.736 :param parent: the parent context if available.737 :param extra: extra keyword arguments forwarded to the context738 constructor.739+740+ .. versionchanged:: 8.0741+ Added the :attr:`context_class` attribute.742 """743 for key, value in self.context_settings.items():744 if key not in extra:745 extra[key] = value746+734- ctx = Context(self, info_name=info_name, parent=parent, **extra)747+ ctx = self.context_class(self, info_name=info_name, parent=parent, **extra)748+749 with ctx.scope(cleanup=False):750 self.parse_args(ctx, args)751 return ctx752 1364 unmodified lines