Skip to content
Myr

Merge pull request #1915 from pallets/command-name

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

return resolved name, not original name

authored by David Lord · committed by GitHub · parents 329b100, 0db91e2 · Browse files · 5 files+30 added−2 removed
This commit

State

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

Diff stats

5 files+30 added−2 removed

  • tests:+14 added−0 removed
  • other:+16 added−2 removed

5 files+30 added−2 removed

modified:docs/advanced.rst+5 added-1 removed
33 unmodified lines
3435.. click:example::3637 class AliasedGroup(click.Group):3838 def get_command(self, ctx, cmd_name):39 rv = click.Group.get_command(self, ctx, cmd_name)40 if rv is not None:41 return rv
4 unmodified lines
46 elif len(matches) == 1:47 return click.Group.get_command(self, ctx, matches[0])48 ctx.fail(f"Too many matches: {', '.join(sorted(matches))}")4950 def resolve_command(self, ctx, args):51 # always return the full command name52 _, cmd, args = super().resolve_command(ctx, args)53 return cmd.name, cmd, args5455And it can then be used like this:5657.. click:example::58
430 unmodified lines
modified:examples/aliases/aliases.py+5 added
65 unmodified lines
66 elif len(matches) == 1:67 return click.Group.get_command(self, ctx, matches[0])68 ctx.fail(f"Too many matches: {', '.join(sorted(matches))}")6970 def resolve_command(self, ctx, args):71 # always return the command's name, not the alias72 _, cmd, args = super().resolve_command(ctx, args)73 return cmd.name, cmd, args747576def read_config(ctx, param, value):77 """Callback that is used whenever --config is passed. We use this to78 always load the correct config. This means that the config is loaded79 even if the group itself never executes so our aliases stay always
64 unmodified lines
modified:src/click/core.py+1 added-1 removed
1715 unmodified lines
1716 if cmd is None and not ctx.resilient_parsing:1717 if split_opt(cmd_name)[0]:1718 self.parse_args(ctx, ctx.args)1719 ctx.fail(_("No such command {name!r}.").format(name=original_cmd_name))1720 return cmd.name if cmd else None, cmd, args[1:]1720 return cmd_name if cmd else None, cmd, args[1:]17211722 def get_command(self, ctx: Context, cmd_name: str) -> t.Optional[Command]:1723 """Given a context and a command name, this returns a1724 :class:`Command` object if it exists or returns `None`.
1206 unmodified lines
modified:tests/test_commands.py+14 added
283 unmodified lines
284 class AliasedGroup(click.Group):285 def get_command(self, ctx, cmd_name):286 return push287288 def resolve_command(self, ctx, args):289 _, command, args = super().resolve_command(ctx, args)290 return command.name, command, args291292 cli = AliasedGroup()293294 @cli.command()295 def push():
3 unmodified lines
299 assert not result.exception300 assert result.output.startswith("Usage: root push [OPTIONS]")301302303def test_group_add_command_name(runner):304 cli = click.Group("cli")305 cmd = click.Command("a", params=[click.Option(["-x"], required=True)])306 cli.add_command(cmd, "b")307 # Check that the command is accessed through the registered name,308 # not the original name.309 result = runner.invoke(cli, ["b"], default_map={"b": {"x": 3}})310 assert result.exit_code == 0311312313def test_unprocessed_options(runner):314 @click.command(context_settings=dict(ignore_unknown_options=True))315 @click.argument("args", nargs=-1, type=click.UNPROCESSED)316 @click.option("--verbose", "-v", count=True)
36 unmodified lines
modified:CHANGES.rst+5 added
18 unmodified lines
19- Flag options guess their type from ``flag_value`` if given, like20 regular options do from ``default``. :issue:`1886`21- Added documentation that custom parameter types may be passed22 already valid values in addition to strings. :issue:`1898`23- Resolving commands returns the name that was given, not24 ``command.name``, fixing an unintended change to help text and25 ``default_map`` lookups. When using patterns like ``AliasedGroup``,26 override ``resolve_command`` to change the name that is returned if27 needed. :issue:`1895`282930Version 8.0.031-------------
856 unmodified lines