@@ -182,7 +182,7 @@ def run(self) -> list[Node]:
182182 if not group ._group_actions or group is self .parser ._subparsers : # noqa: SLF001
183183 continue
184184 home_section += self ._mk_option_group (
185- group , prefix = self .parser .prog .split ("/" )[- 1 ], prog = self .parser .prog .split ("/" )[- 1 ]
185+ self . parser , group , prefix = self .parser .prog .split ("/" )[- 1 ], prog = self .parser .prog .split ("/" )[- 1 ]
186186 )
187187 for aliases , help_msg , parser in self ._iter_sub_commands ():
188188 home_section += self ._mk_sub_command (aliases , help_msg , parser )
@@ -206,7 +206,7 @@ def _pre_format(self, block: str | None) -> paragraph | literal_block | None:
206206 _protect_option_dashes (para )
207207 return para
208208
209- def _mk_option_group (self , group : _ArgumentGroup , prefix : str , prog : str ) -> section :
209+ def _mk_option_group (self , parser : ArgumentParser , group : _ArgumentGroup , prefix : str , prog : str ) -> section :
210210 sub_title_prefix : str = self .options .get ("group_sub_title_prefix" )
211211 title_prefix = self .options .get ("group_title_prefix" )
212212 title_text = self ._build_opt_grp_title (group , prefix , prog , sub_title_prefix , title_prefix )
@@ -222,7 +222,7 @@ def _mk_option_group(self, group: _ArgumentGroup, prefix: str, prog: str) -> sec
222222 for action in group ._group_actions : # noqa: SLF001
223223 if action .help == SUPPRESS :
224224 continue
225- point = self ._mk_option_line (action , prefix )
225+ point = self ._mk_option_line (parser , action , prefix )
226226 opt_group += point
227227 group_section += opt_group
228228 return group_section
@@ -235,26 +235,22 @@ def _build_opt_grp_title(
235235 title_text += group .title or ""
236236 return title_text
237237
238- def _mk_option_line (self , action : Action , prefix : str ) -> list_item :
238+ def _mk_option_line (self , parser : ArgumentParser , action : Action , prefix : str ) -> list_item :
239239 line = paragraph ()
240- as_key = action .dest
241- if action .metavar :
242- as_key = action .metavar if isinstance (action .metavar , str ) else action .metavar [0 ]
243240 if action .option_strings :
241+ args_text = _format_args (parser , action ) if action .nargs != 0 else None
244242 for at , opt in enumerate (action .option_strings ):
245243 if at :
246244 line += Text (", " )
247245 self ._mk_option_name (line , prefix , opt )
248- if action . nargs != 0 :
246+ if args_text is not None :
249247 line += Text (" " )
250- metavar_text = (
251- " " .join (meta .upper () for meta in action .metavar )
252- if isinstance (action .metavar , tuple )
253- else as_key .upper ()
254- )
255- line += literal (text = metavar_text )
248+ line += literal (text = args_text )
256249 else :
257- self ._mk_option_name (line , prefix , as_key )
250+ metavar = action .metavar
251+ self ._mk_option_name (
252+ line , prefix , " " .join (metavar ) if isinstance (metavar , tuple ) else metavar or action .dest
253+ )
258254
259255 if action .help :
260256 help_text = load_help_text (action .help )
@@ -347,7 +343,9 @@ def _mk_sub_command(self, aliases: list[str], help_msg: str, parser: ArgumentPar
347343 continue
348344 if isinstance (group ._group_actions [0 ], _SubParsersAction ): # noqa: SLF001
349345 continue
350- group_section += self ._mk_option_group (group , prefix = parser .prog , prog = self .parser .prog .split ("/" )[- 1 ])
346+ group_section += self ._mk_option_group (
347+ parser , group , prefix = parser .prog , prog = self .parser .prog .split ("/" )[- 1 ]
348+ )
351349 return group_section
352350
353351 def _build_sub_cmd_title (self , parser : ArgumentParser , sub_title_prefix : str , title_prefix : str ) -> str :
@@ -404,6 +402,12 @@ def _no_color(self) -> Iterator[None]:
404402 yield
405403
406404
405+ def _format_args (parser : ArgumentParser , action : Action ) -> str :
406+ # argparse's formatter keeps the text in step with the usage line: nargs, choices and user metavars included
407+ formatter = parser ._get_formatter () # noqa: SLF001
408+ return formatter ._format_args (action , formatter ._get_default_metavar_for_optional (action )) # noqa: SLF001
409+
410+
407411def make_id_lower (key : str ) -> str :
408412 return re .sub ("[A-Z]" , lambda m : f"_{ m .group (0 ).lower ()} " , make_id (key ))
409413
0 commit comments