Repository: ewels/rich-click

Issue:
Failure with click 8.2.0 - TypeError: Parameter.make_metavar() missing 1 required positional argument: 'ctx'
Hi,

Click 2.8.0 recently entered in Debian [1], and the example provided in your main page fails:

```python
 Usage: 11_hello.py [OPTIONS]                                                                                                                                   
                                                                                                                                                             
 Simple program that greets NAME for a total of COUNT times.                                                                                                 
                                                                                                                                                             
Traceback (most recent call last):
  File "/build/11_hello.py", line 12, in <module>
    hello()
    ~~~~~^^
  File "/usr/lib/python3/dist-packages/click/core.py", line 1429, in __call__
    return self.main(*args, **kwargs)
           ~~~~~~~~~^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3/dist-packages/rich_click/rich_command.py", line 151, in main
    with self.make_context(prog_name, args, **extra) as ctx:
         ~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3/dist-packages/rich_click/rich_command.py", line 260, in make_context
    return super().make_context(info_name, args, parent, **extra)  # type: ignore[return-value]
           ~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3/dist-packages/click/core.py", line 1172, in make_context
    self.parse_args(ctx, args)
    ~~~~~~~~~~~~~~~^^^^^^^^^^^
  File "/usr/lib/python3/dist-packages/click/core.py", line 1183, in parse_args
    value, args = param.handle_parse_result(ctx, opts, args)
                  ~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3/dist-packages/click/core.py", line 2403, in handle_parse_result
    value = self.process_value(ctx, value)
  File "/usr/lib/python3/dist-packages/click/core.py", line 2342, in process_value
    value = self.callback(ctx, self, value)
  File "/usr/lib/python3/dist-packages/click/core.py", line 1025, in show_help
    echo(ctx.get_help(), color=ctx.color)
         ~~~~~~~~~~~~^^
  File "/usr/lib/python3/dist-packages/click/core.py", line 723, in get_help
    return self.command.get_help(self)
           ~~~~~~~~~~~~~~~~~~~~~^^^^^^
  File "/usr/lib/python3/dist-packages/click/core.py", line 1050, in get_help
    self.format_help(ctx, formatter)
    ~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^
  File "/usr/lib/python3/dist-packages/rich_click/rich_command.py", line 215, in format_help
    self.format_options(ctx, formatter)
    ~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^
  File "/usr/lib/python3/dist-packages/rich_click/rich_command.py", line 232, in format_options
    get_rich_options(self, ctx, formatter)  # type: ignore[arg-type]
    ~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3/dist-packages/rich_click/rich_help_rendering.py", line 500, in get_rich_options
    metavar_str = param.make_metavar()
TypeError: Parameter.make_metavar() missing 1 required positional argument: 'ctx'
```

Affected lines seem to be:
https://github.com/ewels/rich-click/blob/b3f4d8693c58cc7193a2b1a7fe5bac776e95a109/src/rich_click/rich_help_rendering.py#L214
and:
https://github.com/ewels/rich-click/blob/b3f4d8693c58cc7193a2b1a7fe5bac776e95a109/src/rich_click/rich_help_rendering.py#L500

System info:
 - Debian Sid
 - Python 3.13
 - Click 8.2.0
 - rich-click 1.8.5

Kind Regards

[1] https://tracker.debian.org/news/1618122/accepted-python-click-820-1-source-into-unstable/

Hints from issue comments:
@dwreeves: Thanks for bringing this to our attention!

Solution patch:
diff --git a/src/rich_click/_compat_click.py b/src/rich_click/_compat_click.py
index 51caae30..42b934ea 100644
--- a/src/rich_click/_compat_click.py
+++ b/src/rich_click/_compat_click.py
@@ -5,6 +5,7 @@
     click_version = click.__version__
 except Exception:
     # Click 9+ deprecated __version__, so all these checks must necessarily be False if __version__ doesn't exist.
+    CLICK_IS_BEFORE_VERSION_82 = False
     CLICK_IS_BEFORE_VERSION_8X = False
     CLICK_IS_BEFORE_VERSION_9X = False
     CLICK_IS_VERSION_80 = False
@@ -12,6 +13,7 @@
     _major = int(click_version.split(".")[0])
     _minor = int(click_version.split(".")[1])
 
+    CLICK_IS_BEFORE_VERSION_82 = (_major, _minor) < (8, 2)
     CLICK_IS_BEFORE_VERSION_8X = _major < 8
     CLICK_IS_BEFORE_VERSION_9X = _major < 9
     CLICK_IS_VERSION_80 = _major == 8 and _minor == 0
diff --git a/src/rich_click/rich_help_rendering.py b/src/rich_click/rich_help_rendering.py
index caf0e281..e74adc7d 100644
--- a/src/rich_click/rich_help_rendering.py
+++ b/src/rich_click/rich_help_rendering.py
@@ -22,7 +22,12 @@
 from rich.text import Text
 from typing_extensions import Literal
 
-from rich_click._compat_click import CLICK_IS_BEFORE_VERSION_8X, CLICK_IS_BEFORE_VERSION_9X, CLICK_IS_VERSION_80
+from rich_click._compat_click import (
+    CLICK_IS_BEFORE_VERSION_8X,
+    CLICK_IS_BEFORE_VERSION_9X,
+    CLICK_IS_BEFORE_VERSION_82,
+    CLICK_IS_VERSION_80,
+)
 from rich_click.rich_help_formatter import RichHelpFormatter
 from rich_click.utils import CommandGroupDict, OptionGroupDict
 
@@ -211,7 +216,7 @@ def _get_option_help(
 
     # Append metavar if requested
     if config.append_metavars_help:
-        metavar_str = param.make_metavar()
+        metavar_str = param.make_metavar() if CLICK_IS_BEFORE_VERSION_82 else param.make_metavar(ctx)  # type: ignore
         # Do it ourselves if this is a positional argument
         if isinstance(param, click.core.Argument) and re.match(rf"\[?{param.name.upper()}]?", metavar_str):
             metavar_str = param.type.name.upper()
@@ -497,7 +502,7 @@ def get_rich_options(
 
             # Column for a metavar, if we have one
             metavar = Text(style=formatter.config.style_metavar, overflow="fold")
-            metavar_str = param.make_metavar()
+            metavar_str = param.make_metavar() if CLICK_IS_BEFORE_VERSION_82 else param.make_metavar(ctx)  # type: ignore
 
             if TYPE_CHECKING:
                 assert isinstance(param.name, str)


Test patch:
diff --git a/tests/test_exit_code.py b/tests/test_exit_code.py
index 90d46794..11778c70 100644
--- a/tests/test_exit_code.py
+++ b/tests/test_exit_code.py
@@ -6,7 +6,7 @@
 from click.testing import CliRunner
 
 from rich_click import RichContext, command, group, pass_context
-from rich_click._compat_click import CLICK_IS_BEFORE_VERSION_8X
+from rich_click._compat_click import CLICK_IS_BEFORE_VERSION_8X, CLICK_IS_BEFORE_VERSION_82
 
 
 # Don't use the 'invoke' fixture because we want control over the standalone_mode kwarg.
@@ -92,7 +92,7 @@ def subcommand() -> int:
 
     runner = CliRunner()
     res = runner.invoke(cli, [])
-    assert res.exit_code == 0
+    assert res.exit_code == 0 if CLICK_IS_BEFORE_VERSION_82 else 2
 
 
 @pytest.mark.skipif(CLICK_IS_BEFORE_VERSION_8X, reason="Result does not have return_value attribute.")