return resolved name, not original name
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by David Lord · committed by David Lord · parent 329b100 · 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
Provenance
- Commit
0db91e2- Tree
2196bcf- Author
David Lord <davidism@gmail.com>- Committer
David Lord <davidism@gmail.com>- Parent
329b100
5 files changed+30 added−2 removed
modified:docs/advanced.rst+5 added-1 removed
33 unmodified lines34 35 .. click:example::36 37 class AliasedGroup(click.Group):38-38 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 rv4 unmodified lines46 elif len(matches) == 1:47 return click.Group.get_command(self, ctx, matches[0])48 ctx.fail(f"Too many matches: {', '.join(sorted(matches))}")49 50+ 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, args54+55 And it can then be used like this:56 57 .. click:example::58 430 unmodified linesmodified:examples/aliases/aliases.py+5 added
65 unmodified lines66 elif len(matches) == 1:67 return click.Group.get_command(self, ctx, matches[0])68 ctx.fail(f"Too many matches: {', '.join(sorted(matches))}")69 70+ 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, args74 75+76 def 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 always64 unmodified linesmodified:src/click/core.py+1 added-1 removed
1715 unmodified lines1716 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:]1721 1722 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 linesmodified:tests/test_commands.py+14 added
283 unmodified lines284 class AliasedGroup(click.Group):285 def get_command(self, ctx, cmd_name):286 return push287 288+ def resolve_command(self, ctx, args):289+ _, command, args = super().resolve_command(ctx, args)290+ return command.name, command, args291+292 cli = AliasedGroup()293 294 @cli.command()295 def push():3 unmodified lines299 assert not result.exception300 assert result.output.startswith("Usage: root push [OPTIONS]")301 302 303+def 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 == 0311+312+313 def 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 linesmodified:CHANGES.rst+5 added
18 unmodified lines19 - 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`28 29 30 Version 8.0.031 -------------856 unmodified lines