@@ -243,8 +243,11 @@ def get_choices(self) -> Choices:
243243)
244244
245245from rich .console import (
246+ Console ,
247+ ConsoleOptions ,
246248 Group ,
247249 RenderableType ,
250+ RenderResult ,
248251)
249252from rich .table import Column
250253from rich .text import Text
@@ -506,7 +509,7 @@ def _ActionsContainer_add_argument( # noqa: N802
506509
507510
508511# Overwrite _ActionsContainer.add_argument with our patch
509- setattr ( argparse ._ActionsContainer , ' add_argument' , _ActionsContainer_add_argument )
512+ argparse ._ActionsContainer . add_argument = _ActionsContainer_add_argument # type: ignore[method-assign]
510513
511514
512515############################################################################################################
@@ -560,6 +563,20 @@ def console(self, console: Cmd2RichArgparseConsole) -> None:
560563 """Set our console instance."""
561564 self ._console = console
562565
566+ def __rich_console__ (self , console : Console , options : ConsoleOptions ) -> RenderResult :
567+ """Provide this help formatter to renderables via the console."""
568+ if isinstance (console , Cmd2RichArgparseConsole ):
569+ old_formatter = console .help_formatter
570+ console .help_formatter = self
571+ try :
572+ yield from super ().__rich_console__ (console , options )
573+ finally :
574+ console .help_formatter = old_formatter
575+ else :
576+ # Handle rendering on a console type other than Cmd2RichArgparseConsole.
577+ # In this case, we don't set the help_formatter on the console.
578+ yield from super ().__rich_console__ (console , options )
579+
563580 def _set_color (self , color : bool , ** kwargs : Any ) -> None :
564581 """Set the color for the help output.
565582
@@ -680,25 +697,33 @@ def __init__(
680697 self ,
681698 title : str ,
682699 text : RenderableType ,
683- formatter_creator : Callable [..., Cmd2HelpFormatter ],
684700 ) -> None :
685701 """TextGroup initializer.
686702
687703 :param title: the group's title
688704 :param text: the group's text (string or object that may be rendered by Rich)
689- :param formatter_creator: callable which returns a Cmd2HelpFormatter instance
690705 """
691706 self .title = title
692707 self .text = text
693- self .formatter_creator = formatter_creator
694708
695- def __rich__ (self ) -> Group :
709+ def __rich_console__ (self , console : Console , options : ConsoleOptions ) -> RenderResult :
696710 """Return a renderable Rich Group object for the class instance.
697711
698712 This method formats the title and indents the text to match argparse
699713 group styling, making the object displayable by a Rich console.
700714 """
701- formatter = self .formatter_creator ()
715+ formatter : Cmd2HelpFormatter | None = None
716+ if isinstance (console , Cmd2RichArgparseConsole ):
717+ formatter = console .help_formatter
718+
719+ # This occurs if the console is not a Cmd2RichArgparseConsole or if the
720+ # TextGroup is printed directly instead of as part of an argparse help message.
721+ if formatter is None :
722+ # If console is the wrong type, then have Cmd2HelpFormatter create its own.
723+ formatter = Cmd2HelpFormatter (
724+ prog = "" ,
725+ console = console if isinstance (console , Cmd2RichArgparseConsole ) else None ,
726+ )
702727
703728 styled_title = Text (
704729 type (formatter ).group_name_formatter (f"{ self .title } :" ),
@@ -708,7 +733,7 @@ def __rich__(self) -> Group:
708733 # Indent text like an argparse argument group does
709734 indented_text = ru .indent (self .text , formatter ._indent_increment )
710735
711- return Group (styled_title , indented_text )
736+ yield Group (styled_title , indented_text )
712737
713738
714739class Cmd2ArgumentParser (argparse .ArgumentParser ):
@@ -762,7 +787,7 @@ def __init__(
762787 add_help = add_help ,
763788 allow_abbrev = allow_abbrev ,
764789 exit_on_error = exit_on_error ,
765- ** kwargs , # added in Python 3.14
790+ ** kwargs ,
766791 )
767792
768793 self .ap_completer_type = ap_completer_type
@@ -995,10 +1020,6 @@ def format_help(self) -> str:
9951020 """Override to add a newline."""
9961021 return super ().format_help () + '\n '
9971022
998- def create_text_group (self , title : str , text : RenderableType ) -> TextGroup :
999- """Create a TextGroup using this parser's formatter creator."""
1000- return TextGroup (title , text , self ._get_formatter )
1001-
10021023 def _get_nargs_pattern (self , action : argparse .Action ) -> str :
10031024 """Override to support nargs ranges."""
10041025 nargs_range = action .get_nargs_range () # type: ignore[attr-defined]
0 commit comments