@@ -4420,6 +4420,40 @@ def _get_cli_entry_point_name(self, package_name: str) -> str | None:
44204420
44214421 return None
44224422
4423+ @staticmethod
4424+ def _strip_rich_click_markup(text: str, cmd) -> str:
4425+ """Strip rich-click markup when the command renders ``--help`` with it.
4426+
4427+ Rich-click interprets ``[b]...[/]`` and similar tags when its effective
4428+ ``text_markup`` mode is ``"rich"``. Structured fields written into the
4429+ generated page must be plain text; the full ``--help`` block is already
4430+ rendered by Click and is left alone. Commands from plain Click, Typer,
4431+ or rich-click's ``markdown``/``ansi`` modes keep their text unchanged.
4432+ """
4433+ if not text:
4434+ return text
4435+
4436+ try:
4437+ from rich.text import Text
4438+ from rich_click import RichCommand
4439+ except ImportError:
4440+ return text
4441+
4442+ if not isinstance(cmd, RichCommand):
4443+ return text
4444+
4445+ try:
4446+ formatter = cmd.context_class(cmd, info_name="").make_formatter()
4447+ if formatter.config.text_markup != "rich":
4448+ return text
4449+ except Exception: # pragma: no cover - optional dependency internals
4450+ return text
4451+
4452+ try:
4453+ return Text.from_markup(text).plain
4454+ except Exception: # pragma: no cover - malformed markup is left as-is
4455+ return text
4456+
44234457 def _extract_click_command(self, cmd, name: str, parent_path: str = "") -> dict:
44244458 """
44254459 Extract information from a Click command or group.
@@ -4454,25 +4488,29 @@ def _extract_click_command(self, cmd, name: str, parent_path: str = "") -> dict:
44544488 if kind == "option":
44554489 opt_info = self._extract_click_option(param)
44564490 if opt_info:
4491+ opt_info["help"] = self._strip_rich_click_markup(opt_info["help"], cmd)
44574492 options.append(opt_info)
44584493 elif kind == "argument":
44594494 arg_info = self._extract_click_argument(param)
44604495 if arg_info:
44614496 arguments.append(arg_info)
44624497
4498+ command_help = self._strip_rich_click_markup(cmd.help or "", cmd)
4499+
44634500 # Parse description and examples from the help text
4464- description, examples = self._parse_click_help_parts(cmd.help or "" )
4501+ description, examples = self._parse_click_help_parts(command_help )
44654502
44664503 # Extract the short help (Click may auto-derive it, but sometimes it's
44674504 # empty for subcommands). Fall back to the first line/sentence of help.
44684505 short_help = getattr(cmd, "short_help", "") or ""
4469- if not short_help and cmd.help :
4506+ if not short_help and command_help :
44704507 short_help = cmd.get_short_help_str(limit=150)
4508+ short_help = self._strip_rich_click_markup(short_help, cmd)
44714509
44724510 info = {
44734511 "name": name,
44744512 "full_path": full_path,
4475- "help": cmd.help or "" ,
4513+ "help": command_help ,
44764514 "short_help": short_help,
44774515 "help_text": help_text, # The actual --help output
44784516 "deprecated": getattr(cmd, "deprecated", False),
0 commit comments