Skip to content
Myr

Merge branch '3.x-maintenance'

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 · parents 5cf7f2d, 20c9ccb · Browse files · 6 files+114 added−51 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+114 added−51 removed

  • tests:+30 added−2 removed
  • other:+84 added−49 removed

6 files+114 added−51 removed

modified:click/core.py+43 added-40 removed
180 unmodified lines
181 and parent.default_map is not None:182 default_map = parent.default_map.get(info_name)183 self.default_map = default_map184185 #: This is similar to :attr:`invoked_subcommand` but it holds a186 #: list of multiple subcommand names. This is useful if multi command187 #: chaining is enabled.185 #: This flag indicates if a subcommand is going to be executed. A186 #: group callback can use this information to figure out if it's187 #: being executed directly or because the execution flow passes188 #: onwards to a subcommand. By default it's None, but it can be189 #: the name of the subcommand to execute.190 #:189 #: This property and :attr:`invoked_subcommand` are synchronized.190 #:191 #: .. versionadded:: 3.0192 self.invoked_subcommands = []191 #: If chaining is enabled this will be set to ``'*'`` in case192 #: any commands are executed. It is however not possible to193 #: figure out which ones. If you require this knowledge you194 #: should use a :func:`resultcallback`.195 self.invoked_subcommand = None196197 if terminal_width is None and parent is not None:198 terminal_width = parent.terminal_width199 #: The width of the terminal (None is autodetection).
65 unmodified lines
265 self._depth -= 1266 if self._depth == 0:267 self.close()268266 def _get_invoked_subcommand(self):267 """This flag indicates if a subcommand is going to be executed. A268 group callback can use this information to figure out if it's269 being executed directly or because the execution flow passes270 onwards to a subcommand. By default it's `None`, but it can be271 the name of the subcommand to execute.269 def _get_invoked_subcommands(self):270 from warnings import warn271 warn(Warning('This API does not work properly and has been largely '272 'removed in Click 3.2 to fix a regression the '273 'introduction of this API caused. Consult the '274 'upgrade documentation for more information. For '275 'more information about this see '276 'http://click.pocoo.org/upgrading/#upgrading-to-3.2'),277 stacklevel=2)278 if self.invoked_subcommand is None:279 return []280 return [self.invoked_subcommand]281 def _set_invoked_subcommands(self, value):282 self.invoked_subcommand = \283 len(value) > 1 and '*' or value and value[0] or None284 invoked_subcommands = property(_get_invoked_subcommands,285 _set_invoked_subcommands)286 del _get_invoked_subcommands, _set_invoked_subcommands287273 If chaining is enabled and more than one subcommand is invoked274 then the value will be the string ``'*'``. For chaining you275 should be using :attr:`invoked_subcommands` instead.276 """277 if len(self.invoked_subcommands) == 1:278 return self.invoked_subcommands[0]279 if len(self.invoked_subcommands) > 1:280 return '*'281 def _set_invoked_subcommand(self, value):282 if value is not None:283 value = [value]284 self.invoked_subcommands = value285 invoked_subcommand = property(_get_invoked_subcommand,286 _set_invoked_subcommand)287 del _get_invoked_subcommand, _set_invoked_subcommand288288 def make_formatter(self):289 """Creates the formatter for the help and usage output."""290 return HelpFormatter(width=self.terminal_width)291
649 unmodified lines
941 if not self.chain:942 # Make sure the context is entered so we do not clean up943 # resources until the result processor has worked.944 with ctx:945 cmd_name, cmd, args = self.resolve_command(ctx, args)946 ctx.invoked_subcommand = cmd_name947 Command.invoke(self, ctx)947 sub_ctx = self.handle_subcommand(ctx, args)948 ctx.invoked_subcommands = [sub_ctx.info_name]948 sub_ctx = cmd.make_context(cmd_name, args, parent=ctx)949 with sub_ctx:950 return _process_result(sub_ctx.command.invoke(sub_ctx))951952 # Make sure the context is entered so we do not clean up953 # resources until the result processor has worked.952 # In chain mode we create the contexts step by step, but after the953 # base command has been invoked. Because at that point we do not954 # know the subcommands yet, the invoked subcommand attribute is955 # set to ``*`` to inform the command that subcommands are executed956 # but nothing else.957 with ctx:958 ctx.invoked_subcommand = args and '*' or None959 Command.invoke(self, ctx)960961 # Otherwise we make every single context and invoke them in a962 # chain. In that case the return value to the result processor963 # is the list of all invoked subcommand's results.964 contexts = []965 while args:966 cmd_name, cmd, args = self.resolve_command(ctx, args)967 sub_ctx = cmd.make_context(cmd_name, args, parent=ctx,962 sub_ctx = self.handle_subcommand(ctx, args, allow_extra_args=True,963 allow_interspersed_args=False)968 allow_extra_args=True,969 allow_interspersed_args=False)970 contexts.append(sub_ctx)971 args = sub_ctx.args972967 # Now that we have all contexts, we can invoke them.968 ctx.invoked_subcommands = [x.info_name for x in contexts]969973 rv = []974 for sub_ctx in contexts:975 with sub_ctx:976 rv.append(sub_ctx.command.invoke(sub_ctx))977 return _process_result(rv)978976 def handle_subcommand(self, ctx, args, **extra):979 def resolve_command(self, ctx, args):980 cmd_name = make_str(args[0])981 original_cmd_name = cmd_name982983 # Get the command
15 unmodified lines
999 if split_opt(cmd_name)[0]:1000 self.parse_args(ctx, ctx.args)1001 ctx.fail('No such command "%s".' % original_cmd_name)10021000 return cmd.make_context(cmd_name, args[1:], parent=ctx, **extra)1003 return cmd_name, cmd, args[1:]10041005 def get_command(self, ctx, cmd_name):1006 """Given a context and a command name, this returns a1007 :class:`Command` object if it exists or returns `None`.
582 unmodified lines
modified:docs/commands.rst+8 added-9 removed
166 unmodified lines
167 @cli.command()168 def sync():169 click.echo('The subcommand')170171And how it works in practice::171And how it works in practice:172173 $ python tool.py174 I was invoked without subcommand175 $ python tool.py sync176 I am about to invoke sync177 The subcommand173.. click:run::174175 invoke(cli, prog_name='tool', args=[])176 invoke(cli, prog_name='tool', args=['sync'])177178.. _custom-multi-commands:179180Custom Multi Commands181---------------------
133 unmodified lines
315normal.316317Another note: the :attr:`Context.invoked_subcommand` attribute is a bit318useless for multi commands as it will give ``'*'`` as value if more than320one command is invoked. Instead you should be using the321:attr:`Context.invoked_subcommands` attribute instead which is a list322(note the trailing "s").319one command is invoked. This is necessary because the handling of320subcommands happens one after another so the exact subcommands that will321be handled are not yet available when the callback fires.322323324Multi Command Pipelines325-----------------------
227 unmodified lines
modified:docs/upgrading.rst+28 added
9 unmodified lines
1011Upgrading to 3.212----------------1314Click 3.2 had to perform two changes to multi commands which were15triggered by a change between Click 2 and Click 3 that had bigger16consequences than anticipated.1718Context Invokes19```````````````2021Click 3.2 contains a fix for the :meth:`Context.invoke` function when used22with other commands. The original intention of this function was to23invoke the other command as as if it came from the command line when it24was passed a context object instead of a function. This use was only
15 unmodified lines
4041This also allowed us to fix the issue that defaults were not handled42properly by this function.4344Multicommand Chaining API45`````````````````````````4647Click 3 introduced multicommand chaning. This required a change in how48Click internally dispatches. Unfortunately this change was not correctly49implemented and it appeared that it was possible to provide an API that50can inform the super command about all the subcommands that will be51invoked.5253This assumption however does not work with one of the API guarantees that54have been given in the past. As such this functionality has been removed55in 3.2 as it was already broken. Instead the accidentally broken56functionality of the :attr:`Context.invoked_subcommand` attribute was57restored.5859If you do require the know which exact commands will be invoked there are60different ways to cope with this. The first one is to let the subcommands61all return functions and then to invoke the functions in a62:meth:`Context.resultcallback`.636465.. _upgrade-to-2.0:6667Upgrading to 2.068----------------
32 unmodified lines
modified:tests/test_chain.py+7 added-2 removed
101 unmodified lines
102 'sdist called 2',103 ]104105106def test_context_subcommand_info_sync():106def test_context_subcommand_info_sync(recwarn):107 @click.command()108 def cli():109 pass110111 def _assert_warning():112 assert 'removed in Click 3.2' in str(recwarn.pop(Warning).message)113114 ctx = click.Context(cli, info_name='cli')115116 assert ctx.invoked_subcommand is None117
10 unmodified lines
128 assert ctx.invoked_subcommands == []129130 ctx.invoked_subcommands = ['foo', 'bar']131 assert ctx.invoked_subcommand == '*'129 assert ctx.invoked_subcommands == ['foo', 'bar']132 assert ctx.invoked_subcommands == ['*']133134 assert 'removed in Click 3.2' in str(recwarn.pop(Warning).message)135136137def test_pipeline(runner):138 @click.group(chain=True, invoke_without_command=True)139 @click.option('-i', '--input', type=click.File('r'))140 def cli(input):
45 unmodified lines
modified:tests/test_commands.py+23 added
227 unmodified lines
228229 result = runner.invoke(cli, [])230 assert not result.exception231 assert result.output == '42\n'232233234def test_invoked_subcommand(runner):235 @click.group(invoke_without_command=True)236 @click.pass_context237 def cli(ctx):238 if ctx.invoked_subcommand is None:239 click.echo('no subcommand, use default')240 ctx.invoke(sync)241 else:242 click.echo('invoke subcommand')243244 @cli.command()245 def sync():246 click.echo('in subcommand')247248 result = runner.invoke(cli, ['sync'])249 assert not result.exception250 assert result.output == 'invoke subcommand\nin subcommand\n'251252 result = runner.invoke(cli)253 assert not result.exception254 assert result.output == 'no subcommand, use default\nin subcommand\n'
modified:CHANGES+5 added
20 unmodified lines
21- Added missing `err` parameter forwarding to the `secho` function.22- Fixed default parameters not being handled properly by the context23 invoke method. This is a backwards incompatible change if the function24 was used improperly. See :ref:`upgrade-to-3.2` for more information.25- Removed the `invoked_subcommands` attribute largely. It is not possible26 to provide it to work error free due to how the parsing works so this27 API has been deprecated. See :ref:`upgrade-to-3.2` for more information.28- Restored the functionality of `invoked_subcommand` which was broken as29 a regression in 3.1.3031Version 3.132-----------33
153 unmodified lines