@@ -125,11 +125,6 @@ def _std_domain(self) -> StandardDomain:
125125 def _make_id (self ) -> Callable [[str ], str ]:
126126 return make_id_lower if "force_refs_lower" in self .options else make_id
127127
128- @property
129- def _raw_format (self ) -> bool :
130- formatter = self .parser .formatter_class
131- return isinstance (formatter , type ) and issubclass (formatter , RawDescriptionHelpFormatter )
132-
133128 def _load_sub_parsers (
134129 self , sub_parser : _SubParsersAction [ArgumentParser ]
135130 ) -> Iterator [tuple [list [str ], str , ArgumentParser ]]:
@@ -169,7 +164,7 @@ def run(self) -> list[Node]:
169164 if "usage_first" in self .options :
170165 home_section += self ._mk_usage (self .parser )
171166
172- if description := self ._pre_format (self .options .get ("description" , self .parser .description )):
167+ if description := self ._pre_format (self .options .get ("description" , self .parser .description ), self . parser ):
173168 home_section += description
174169
175170 if "usage_first" not in self .options :
@@ -178,31 +173,38 @@ def run(self) -> list[Node]:
178173 for group in self .parser ._action_groups : # noqa: SLF001
179174 if actions := _visible_actions (group ):
180175 home_section += self ._mk_option_group (
181- group , actions , prefix = self .parser .prog .split ("/" )[- 1 ], prog = self .parser .prog .split ("/" )[- 1 ]
176+ group ,
177+ actions ,
178+ self .parser ,
179+ prefix = self .parser .prog .split ("/" )[- 1 ],
180+ prog = self .parser .prog .split ("/" )[- 1 ],
182181 )
183182 for aliases , help_msg , parser in self ._iter_sub_commands ():
184183 home_section += self ._mk_sub_command (aliases , help_msg , parser )
185184
186- if epilog := self ._pre_format (self .options .get ("epilog" , self .parser .epilog )):
185+ if epilog := self ._pre_format (self .options .get ("epilog" , self .parser .epilog ), self . parser ):
187186 home_section += epilog
188187
189188 if self .content :
190189 self .state .nested_parse (self .content , self .content_offset , home_section )
191190
192191 return [home_section ]
193192
194- def _pre_format (self , block : str | None ) -> paragraph | literal_block | None :
193+ def _pre_format (self , block : str | None , parser : ArgumentParser ) -> paragraph | literal_block | None :
195194 if block is None or not block .strip ():
196195 return None
197- if self ._raw_format and "\n " in block :
196+ formatter = parser .formatter_class
197+ if "\n " in block and isinstance (formatter , type ) and issubclass (formatter , RawDescriptionHelpFormatter ):
198198 lit = literal_block ("" , Text (block ), classes = ["sphinx-argparse-cli-wrap" ])
199199 lit ["language" ] = "none"
200200 return lit
201201 para = paragraph ("" , Text (block ))
202202 _protect_option_dashes (para )
203203 return para
204204
205- def _mk_option_group (self , group : _ArgumentGroup , actions : list [Action ], prefix : str , prog : str ) -> section :
205+ def _mk_option_group (
206+ self , group : _ArgumentGroup , actions : list [Action ], parser : ArgumentParser , prefix : str , prog : str
207+ ) -> section :
206208 sub_title_prefix : str = self .options .get ("group_sub_title_prefix" )
207209 title_prefix = self .options .get ("group_title_prefix" )
208210 # an untitled group borrows its description as heading so its anchor stays unique
@@ -213,7 +215,7 @@ def _mk_option_group(self, group: _ArgumentGroup, actions: list[Action], prefix:
213215 # the text sadly needs to be prefixed, because otherwise the autosectionlabel will conflict
214216 header = title ("" , Text (title_text ))
215217 group_section = section ("" , header , ids = [ref_id ], names = [ref_id ])
216- if group .title and (description := self ._pre_format (group .description )):
218+ if group .title and (description := self ._pre_format (group .description , parser )):
217219 group_section += description
218220 self ._register_ref (ref_id , title_text , group_section )
219221 opt_group = bullet_list ()
@@ -331,20 +333,19 @@ def _mk_sub_command(self, aliases: list[str], help_msg: str, parser: ArgumentPar
331333 if "usage_first" in self .options :
332334 group_section += self ._mk_usage (parser )
333335
334- command_desc = (parser .description or help_msg or "" ).strip ()
335- if command_desc :
336- desc_paragraph = paragraph ("" , Text (command_desc ))
337- _protect_option_dashes (desc_paragraph )
338- group_section += desc_paragraph
336+ if command_desc := (parser .description or help_msg ).strip ():
337+ group_section += self ._pre_format (command_desc , parser )
339338
340339 if "usage_first" not in self .options :
341340 group_section += self ._mk_usage (parser )
342341
343342 for group in parser ._action_groups : # noqa: SLF001
344343 if actions := _visible_actions (group ):
345344 group_section += self ._mk_option_group (
346- group , actions , prefix = parser .prog , prog = self .parser .prog .split ("/" )[- 1 ]
345+ group , actions , parser , prefix = parser .prog , prog = self .parser .prog .split ("/" )[- 1 ]
347346 )
347+ if epilog := self ._pre_format (parser .epilog , parser ):
348+ group_section += epilog
348349 return group_section
349350
350351 def _build_sub_cmd_title (self , parser : ArgumentParser , sub_title_prefix : str , title_prefix : str ) -> str :
@@ -388,8 +389,8 @@ def _apply_sub_title(title_text: str, sub_title_prefix: str, prog: str, sub_cmd:
388389 return title_text
389390
390391 def _mk_usage (self , parser : ArgumentParser ) -> literal_block :
391- parser . formatter_class = lambda prog : HelpFormatter ( prog , width = self .options .get ("usage_width" , 100 ) )
392- with self ._no_color ():
392+ width = self .options .get ("usage_width" , 100 )
393+ with patch . object ( parser , "formatter_class" , lambda prog : HelpFormatter ( prog , width = width )), self ._no_color ():
393394 texts = parser .format_usage ()[len ("usage: " ) :].splitlines ()
394395 texts = [line if at == 0 else f"{ ' ' * (len (parser .prog ) + 1 )} { line .lstrip ()} " for at , line in enumerate (texts )]
395396 return literal_block ("" , Text ("\n " .join (texts )), classes = ["sphinx-argparse-cli-wrap" ])
0 commit comments