Added result callbacks for groups.
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Armin Ronacher · committed by Armin Ronacher · parent 7d76c67 · Browse files · 2 files·+32 added−3 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+32 added−3 removed
- other:+32 added−3 removed
Provenance
- Commit
0412755- Tree
113cba1- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
7d76c67
2 files changed+32 added−3 removed
modified:click/core.py+31 added-3 removed
1 unmodified line2 import sys3 import codecs4 from contextlib import contextmanager5 from itertools import chain, repeat6+from functools import update_wrapper7 8 from .types import convert_type, IntRange, BOOL9 from .utils import make_str, make_default_short_help, echo10 from .exceptions import ClickException, UsageError, BadParameter, Abort754 unmodified lines765 allow_interspersed_args = False766 767 def __init__(self, name=None, invoke_without_command=False,768 no_args_is_help=None, subcommand_metavar=None,768- chain=False, **attrs):769+ chain=False, result_callback=None, **attrs):770 Command.__init__(self, name, **attrs)771 if no_args_is_help is None:772 no_args_is_help = not invoke_without_command773 self.no_args_is_help = no_args_is_help4 unmodified lines778 else:779 subcommand_metavar = SUBCOMMAND_METAVAR780 self.subcommand_metavar = subcommand_metavar781 self.chain = chain782+ self.result_callback = result_callback783 784 def collect_usage_pieces(self, ctx):785 rv = Command.collect_usage_pieces(self, ctx)786 rv.append(self.subcommand_metavar)2 unmodified lines789 def format_options(self, ctx, formatter):790 Command.format_options(self, ctx, formatter)791 self.format_commands(ctx, formatter)792 793+ def resultcallback(self, replace=False):794+ """Adds a result callback to the chain command. By default if a795+ result callback is already registered this will chain them but796+ this can be disabled with the `replace` parameter.797+798+ .. versionadded:: 3.0799+800+ :param replace: if set to `True` an already existing result801+ callback will be removed.802+ """803+ def decorator(f):804+ old_callback = self.result_callback805+ if old_callback is None or replace:806+ self.result_callback = f807+ return f808+ def function(value):809+ return f(old_callback(value))810+ self.result_callback = rv = update_wrapper(function, f)811+ return rv812+ return decorator813+814 def format_commands(self, ctx, formatter):815 """Extra format methods for multi methods that adds all the commands816 after the options.817 """24 unmodified lines842 ctx.fail('Missing command.')843 844 args = ctx.args845 846+ def _process_result(value):847+ if self.result_callback is not None:848+ value = ctx.invoke(self.result_callback, value)849+ return value850+851 # If we're not in chain mode, we only allow the invocation of a852 # single command but we also inform the current context about the853 # name of the command to invoke.854 if not self.chain:855 sub_ctx = self.handle_subcommand(ctx, args)856 ctx.invoked_subcommands = [sub_ctx.info_name]857 Command.invoke(self, ctx)858 with sub_ctx:831- return sub_ctx.command.invoke(sub_ctx)859+ return _process_result(sub_ctx.command.invoke(sub_ctx))860 861 # Otherwise we make every single context and invoke them in a862 # chain.863 contexts = []9 unmodified lines873 rv = []874 for sub_ctx in contexts:875 with sub_ctx:876 rv.append(sub_ctx.command.invoke(sub_ctx))849- return rv877+ return _process_result(rv)878 879 def handle_subcommand(self, ctx, args, **extra):880 cmd_name = make_str(args[0])881 original_cmd_name = cmd_name608 unmodified linesmodified:CHANGES+1 added
29 unmodified lines30 - added a way to disable the standalone mode of the `main`31 method on a Click command to be able to handle errors better.32 - added support for returning values from command callbacks.33 - added simplifications for printing to stderr from `echo`.34+- added result callbacks for groups.35 36 Version 2.637 -----------38 111 unmodified lines