Skip to content
Myr

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

2 files+32 added−3 removed

modified:click/core.py+31 added-3 removed
1 unmodified line
2import sys3import codecs4from contextlib import contextmanager5from itertools import chain, repeat6from functools import update_wrapper78from .types import convert_type, IntRange, BOOL9from .utils import make_str, make_default_short_help, echo10from .exceptions import ClickException, UsageError, BadParameter, Abort
754 unmodified lines
765 allow_interspersed_args = False766767 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_help
4 unmodified lines
778 else:779 subcommand_metavar = SUBCOMMAND_METAVAR780 self.subcommand_metavar = subcommand_metavar781 self.chain = chain782 self.result_callback = result_callback783784 def collect_usage_pieces(self, ctx):785 rv = Command.collect_usage_pieces(self, ctx)786 rv.append(self.subcommand_metavar)
2 unmodified lines
789 def format_options(self, ctx, formatter):790 Command.format_options(self, ctx, formatter)791 self.format_commands(ctx, formatter)792793 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.797798 .. versionadded:: 3.0799800 :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 decorator813814 def format_commands(self, ctx, formatter):815 """Extra format methods for multi methods that adds all the commands816 after the options.817 """
24 unmodified lines
842 ctx.fail('Missing command.')843844 args = ctx.args845846 def _process_result(value):847 if self.result_callback is not None:848 value = ctx.invoke(self.result_callback, value)849 return value850851 # 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))860861 # Otherwise we make every single context and invoke them in a862 # chain.863 contexts = []
9 unmodified lines
873 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)878879 def handle_subcommand(self, ctx, args, **extra):880 cmd_name = make_str(args[0])881 original_cmd_name = cmd_name
608 unmodified lines
modified:CHANGES+1 added
29 unmodified lines
30- 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.3536Version 2.637-----------38
111 unmodified lines