|
13 | 13 | logger = logging.getLogger(__name__) |
14 | 14 |
|
15 | 15 |
|
| 16 | +def _escape_mdx_text(text: str) -> str: |
| 17 | + """ |
| 18 | + Escape text that will be emitted as the *children* of an MDX/JSX component. |
| 19 | +
|
| 20 | + In MDX, `{` and `}` can start JS expressions even in otherwise plain text, |
| 21 | + which can break parsing when help strings include JSON examples. |
| 22 | + """ |
| 23 | + if not text: |
| 24 | + return "" |
| 25 | + # IMPORTANT: escape '&' first to avoid double-escaping. |
| 26 | + return ( |
| 27 | + text.replace("&", "&") |
| 28 | + .replace("<", "<") |
| 29 | + .replace(">", ">") |
| 30 | + .replace("{", "{") |
| 31 | + .replace("}", "}") |
| 32 | + ) |
| 33 | + |
| 34 | + |
16 | 35 | def _get_parser_info(parser: argparse.ArgumentParser, subparser_help: str = "") -> Dict: |
17 | 36 | """Extract information from an ArgumentParser.""" |
18 | 37 | info = { |
@@ -110,10 +129,19 @@ def _format_argument_item(arg: Dict) -> List[str]: |
110 | 129 | if arg["required"]: |
111 | 130 | attrs.append("required") |
112 | 131 |
|
113 | | - # Build description with short alias mention |
114 | | - help_text = (arg["help"] or "").replace("<", "<").replace(">", ">") |
115 | | - if short_opts: |
116 | | - alias_note = f"Short: `{short_opts[0]}`" |
| 132 | + # Build description with alias mention (short + additional long aliases) |
| 133 | + help_text = _escape_mdx_text(arg["help"] or "") |
| 134 | + |
| 135 | + aliases: List[str] = [] |
| 136 | + if arg["option_strings"]: |
| 137 | + aliases = [o for o in arg["option_strings"] if o != primary] |
| 138 | + |
| 139 | + if aliases: |
| 140 | + # Put long aliases first, then short ones for readability. |
| 141 | + long_aliases = [a for a in aliases if a.startswith("--")] |
| 142 | + short_aliases = [a for a in aliases if not a.startswith("--")] |
| 143 | + aliases_fmt = ", ".join([f"`{a}`" for a in (long_aliases + short_aliases)]) |
| 144 | + alias_note = f"Aliases: {aliases_fmt}" |
117 | 145 | if help_text: |
118 | 146 | help_text = f"{help_text} ({alias_note})" |
119 | 147 | else: |
|
0 commit comments