Repository: pallets/click

Issue:
Click 8.0.0 `group.add_command(..)` does not replace name in description and when looking up defaults
With click 8.0.0 `click.group.add_command(..)` changed the behavior in how new command names are used:
Both the help text for the command as well as when looking up default values in `default_map` the previous command name instead of the one added by the group are shown.

Minimal example:

```
# test.py
import click

@click.command()
@click.option(
    "--qux",
    required=True
)
def foo(qux):
    print(qux)

@click.group()
def group():
    pass

group.add_command(foo, "bar")

group(default_map={"bar": {"qux": "val"}})
```
Run the above:
* with click 7.x: 
> $ python3 test.py bar --help
> Usage: test.py bar [OPTIONS]
> // and
> $ python3 test.py bar
> val
* with click 8.0.0:
> $ python3 test.py bar --help
> Usage: test.py foo [OPTIONS]
> // and
> $ python3 test.py bar
> Usage: test.py foo [OPTIONS]
> Try 'test.py foo --help' for help.
>
> Error: Missing option '--qux'.

I would have expected that both the help shows the name of the command as "bar" (as using "foo" will result in an error)
and the lookup for the default using "bar" as a key.

Environment:

- Python version: 3.9.5
- Click version: 8.0.0



Discussion:
This was due to #1422, which was intended to return the "real" command name when using aliases. However, I think the use case you've shown here is much more common, so I'll revert that change. The aliased command issue can be addressed by updating the docs to override `resolve_command` and return the desired command name instead.

Resolving patch:
diff --git a/examples/aliases/aliases.py b/examples/aliases/aliases.py
--- a/examples/aliases/aliases.py
+++ b/examples/aliases/aliases.py
@@ -67,6 +67,11 @@ def get_command(self, ctx, cmd_name):
             return click.Group.get_command(self, ctx, matches[0])
         ctx.fail(f"Too many matches: {', '.join(sorted(matches))}")
 
+    def resolve_command(self, ctx, args):
+        # always return the command's name, not the alias
+        _, cmd, args = super().resolve_command(ctx, args)
+        return cmd.name, cmd, args
+
 
 def read_config(ctx, param, value):
     """Callback that is used whenever --config is passed.  We use this to
diff --git a/src/click/core.py b/src/click/core.py
--- a/src/click/core.py
+++ b/src/click/core.py
@@ -1717,7 +1717,7 @@ def resolve_command(
             if split_opt(cmd_name)[0]:
                 self.parse_args(ctx, ctx.args)
             ctx.fail(_("No such command {name!r}.").format(name=original_cmd_name))
-        return cmd.name if cmd else None, cmd, args[1:]
+        return cmd_name if cmd else None, cmd, args[1:]
 
     def get_command(self, ctx: Context, cmd_name: str) -> t.Optional[Command]:
         """Given a context and a command name, this returns a


Test patch:
diff --git a/tests/test_commands.py b/tests/test_commands.py
--- a/tests/test_commands.py
+++ b/tests/test_commands.py
@@ -285,6 +285,10 @@ class AliasedGroup(click.Group):
         def get_command(self, ctx, cmd_name):
             return push
 
+        def resolve_command(self, ctx, args):
+            _, command, args = super().resolve_command(ctx, args)
+            return command.name, command, args
+
     cli = AliasedGroup()
 
     @cli.command()
@@ -296,6 +300,16 @@ def push():
     assert result.output.startswith("Usage: root push [OPTIONS]")
 
 
+def test_group_add_command_name(runner):
+    cli = click.Group("cli")
+    cmd = click.Command("a", params=[click.Option(["-x"], required=True)])
+    cli.add_command(cmd, "b")
+    # Check that the command is accessed through the registered name,
+    # not the original name.
+    result = runner.invoke(cli, ["b"], default_map={"b": {"x": 3}})
+    assert result.exit_code == 0
+
+
 def test_unprocessed_options(runner):
     @click.command(context_settings=dict(ignore_unknown_options=True))
     @click.argument("args", nargs=-1, type=click.UNPROCESSED)