Skip to content
Myr

non-chained group invoked without command invokes result callback

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

authored by IamCathal · committed by David Lord · parent 2815455 · Browse files · 3 files+28 added−12 removed
This commit

State

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

Diff stats

3 files+28 added−12 removed

  • tests:+19 added−0 removed
  • other:+9 added−12 removed

3 files+28 added−12 removed

modified:src/click/core.py+7 added-12 removed
1168 unmodified lines
1169 Command.format_options(self, ctx, formatter)1170 self.format_commands(ctx, formatter)11711172 def resultcallback(self, replace=False):1173 """Adds a result callback to the chain command. By default if a1173 """Adds a result callback to the command. By default if a1174 result callback is already registered this will chain them but1175 this can be disabled with the `replace` parameter. The result1176 callback is invoked with the return value of the subcommand1177 (or the list of return values from all subcommands if chaining
10 unmodified lines
1188 @cli.resultcallback()1189 def process_result(result, input):1190 return result + input11911192 .. versionadded:: 3.011931192 :param replace: if set to `True` an already existing result1193 callback will be removed.11941195 .. versionadded:: 3.01196 """11971198 def decorator(f):1199 old_callback = self.result_callback
57 unmodified lines
1257 value = ctx.invoke(self.result_callback, value, **ctx.params)1258 return value12591260 if not ctx.protected_args:1261 # If we are invoked without command the chain flag controls1262 # how this happens. If we are not in chain mode, the return1263 # value here is the return value of the command.1264 # If however we are in chain mode, the return value is the1265 # return value of the result processor invoked with an empty1266 # list (which means that no subcommand actually was executed).1261 if self.invoke_without_command:1268 if not self.chain:1269 return Command.invoke(self, ctx)1262 # No subcommand was invoked, so the result callback is1263 # invoked with None for regular groups, or an empty list1264 # for chained groups.1265 with ctx:1266 Command.invoke(self, ctx)1272 return _process_result([])1267 return _process_result([] if self.chain else None)1268 ctx.fail("Missing command.")12691270 # Fetch args back out1271 args = ctx.protected_args + ctx.args
803 unmodified lines
modified:tests/test_chain.py+19 added
86 unmodified lines
87 assert not result.exception88 assert result.output.splitlines() == ["bdist called 1", "sdist called 2"]899091@pytest.mark.parametrize(("chain", "expect"), [(False, "None"), (True, "[]")])92def test_no_command_result_callback(runner, chain, expect):93 """When a group has ``invoke_without_command=True``, the result94 callback is always invoked. A regular group invokes it with95 ``None``, a chained group with ``[]``.96 """9798 @click.group(invoke_without_command=True, chain=chain)99 def cli():100 pass101102 @cli.resultcallback()103 def process_result(result):104 click.echo(str(result), nl=False)105106 result = runner.invoke(cli, [])107 assert result.output == expect108109110def test_chaining_with_arguments(runner):111 @click.group(chain=True)112 def cli():113 pass
135 unmodified lines
modified:CHANGES.rst+2 added
27 unmodified lines
28 matched name. This makes behavior such as help text and29 ``Context.invoked_subcommand`` consistent when using patterns like30 ``AliasedGroup``. :issue:`1422`31- The ``BOOL`` type accepts the values "on" and "off". :issue:`1629`32- A ``Group`` with ``invoke_without_command=True`` will always invoke33 its result callback. :issue:`1178`343536Version 7.1.237-------------
635 unmodified lines