Skip to content
Myr

Merge pull request #1848 from pallets/rename-result-callback

Before adoption: this commit came in with the history the project adopted, so no landing records it.

rename resultcallback to result_callback

authored by David Lord · committed by GitHub · parents e3594e7, b1e7858 · Browse files · 6 files+37 added−19 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

6 files+37 added−19 removed

  • tests:+2 added−2 removed
  • other:+35 added−17 removed

6 files+37 added−19 removed

modified:docs/commands.rst+2 added-2 removed
349 unmodified lines
350how to do its processing. At that point it then returns a processing351function and returns.352353Where do the returned functions go? The chained multicommand can register354a callback with :meth:`MultiCommand.resultcallback` that goes over all354a callback with :meth:`MultiCommand.result_callback` that goes over all355these functions and then invoke them.356357To make this a bit more concrete consider this example:358
3 unmodified lines
362 @click.option('-i', '--input', type=click.File('r'))363 def cli(input):364 pass365366 @cli.resultcallback()366 @cli.result_callback()367 def process_pipeline(processors, input):368 iterator = (x.rstrip('\r\n') for x in input)369 for processor in processors:370 iterator = processor(iterator)
201 unmodified lines
modified:docs/upgrading.rst+1 added-1 removed
88 unmodified lines
8990If you do require the know which exact commands will be invoked there are91different ways to cope with this. The first one is to let the subcommands92all return functions and then to invoke the functions in a93:meth:`Context.resultcallback`.93:meth:`Context.result_callback`.949596.. _upgrade-to-2.0:97
34 unmodified lines
modified:examples/imagepipe/imagepipe.py+1 added-1 removed
18 unmodified lines
19 imagepipe open -i example02.jpg blur save20 """212223@cli.resultcallback()23@cli.result_callback()24def process_commands(processors):25 """This result callback is invoked with an iterable of all the chained26 subcommands. As in this example each subcommand returns a function27 we can chain them together to feed one into the other, similar to how
261 unmodified lines
modified:src/click/core.py+29 added-13 removed
307 unmodified lines
308 #:309 #: If chaining is enabled this will be set to ``'*'`` in case310 #: any commands are executed. It is however not possible to311 #: figure out which ones. If you require this knowledge you312 #: should use a :func:`resultcallback`.312 #: should use a :func:`result_callback`.313 self.invoked_subcommand = None314315 if terminal_width is None and parent is not None:316 terminal_width = parent.terminal_width
1045 unmodified lines
1362 :param chain: if this is set to `True` chaining of multiple subcommands1363 is enabled. This restricts the form of commands in that1364 they cannot have optional arguments but it allows1365 multiple commands to be chained together.1366 :param result_callback: the result callback to attach to this multi1367 command.1366 :param result_callback: The result callback to attach to this multi1367 command. This can be set or changed later with the1368 :meth:`result_callback` decorator.1369 """13701371 allow_extra_args = True1372 allow_interspersed_args = False
19 unmodified lines
1392 else:1393 subcommand_metavar = SUBCOMMAND_METAVAR1394 self.subcommand_metavar = subcommand_metavar1395 self.chain = chain1395 #: The result callback that is stored. This can be set or1396 #: overridden with the :func:`resultcallback` decorator.1397 self.result_callback = result_callback1396 # The result callback that is stored. This can be set or1397 # overridden with the :func:`result_callback` decorator.1398 self._result_callback = result_callback13991400 if self.chain:1401 for param in self.params:1402 if isinstance(param, Argument) and not param.required:
24 unmodified lines
1427 def format_options(self, ctx, formatter):1428 super().format_options(ctx, formatter)1429 self.format_commands(ctx, formatter)14301430 def resultcallback(self, replace=False):1431 def result_callback(self, replace=False):1432 """Adds a result callback to the command. By default if a1433 result callback is already registered this will chain them but1434 this can be disabled with the `replace` parameter. The result1435 callback is invoked with the return value of the subcommand
7 unmodified lines
1443 @click.option('-i', '--input', default=23)1444 def cli(input):1445 return 4214461446 @cli.resultcallback()1447 @cli.result_callback()1448 def process_result(result, input):1449 return result + input14501451 :param replace: if set to `True` an already existing result1452 callback will be removed.14531454 .. versionchanged:: 8.01455 Renamed from ``resultcallback``.14561457 .. versionadded:: 3.01458 """14591460 def decorator(f):1457 old_callback = self.result_callback1461 old_callback = self._result_callback14621463 if old_callback is None or replace:1459 self.result_callback = f1464 self._result_callback = f1465 return f14661467 def function(__value, *args, **kwargs):1468 return f(old_callback(__value, *args, **kwargs), *args, **kwargs)14691465 self.result_callback = rv = update_wrapper(function, f)1470 self._result_callback = rv = update_wrapper(function, f)1471 return rv14721473 return decorator14741475 def resultcallback(self, replace=False):1476 import warnings14771478 warnings.warn(1479 "'resultcallback' has been renamed to 'result_callback'."1480 " The old name will be removed in Click 8.1.",1481 DeprecationWarning,1482 stacklevel=2,1483 )1484 return self.result_callback(replace=replace)14851486 def format_commands(self, ctx, formatter):1487 """Extra format methods for multi methods that adds all the commands1488 after the options.1489 """
37 unmodified lines
1527 return ctx.args15281529 def invoke(self, ctx):1530 def _process_result(value):1515 if self.result_callback is not None:1516 value = ctx.invoke(self.result_callback, value, **ctx.params)1531 if self._result_callback is not None:1532 value = ctx.invoke(self._result_callback, value, **ctx.params)1533 return value15341535 if not ctx.protected_args:1536 if self.invoke_without_command:
1122 unmodified lines
modified:tests/test_chain.py+2 added-2 removed
97 unmodified lines
98 @click.group(invoke_without_command=True, chain=chain)99 def cli():100 pass101102 @cli.resultcallback()102 @cli.result_callback()103 def process_result(result):104 click.echo(str(result), nl=False)105106 result = runner.invoke(cli, [])
25 unmodified lines
132 @click.option("-i", "--input", type=click.File("r"))133 def cli(input):134 pass135136 @cli.resultcallback()136 @cli.result_callback()137 def process_pipeline(processors, input):138 iterator = (x.rstrip("\r\n") for x in input)139 for processor in processors:140 iterator = processor(iterator)
108 unmodified lines
modified:CHANGES.rst+2 added
204 unmodified lines
205 line instead of only the input when pressing backspace. :issue:`665`206- Add all kwargs passed to ``Context.invoke()`` to ``ctx.params``.207 Fixes an inconsistency when nesting ``Context.forward()`` calls.208 :issue:`1568`209- The ``MultiCommand.resultcallback`` decorator is renamed to210 ``result_callback``. The old name is deprecated. :issue:`1160`211212213Version 7.1.2214-------------
635 unmodified lines