@@ -246,7 +246,6 @@ def my_completer(self, text, line, begidx, endidx, arg_tokens)
246246 gettext ,
247247)
248248from typing import (
249- IO ,
250249 TYPE_CHECKING ,
251250 Any ,
252251 Callable ,
@@ -265,6 +264,8 @@ def my_completer(self, text, line, begidx, endidx, arg_tokens)
265264 Group ,
266265 RenderableType ,
267266)
267+ from rich .table import Column , Table
268+ from rich .text import Text
268269from rich_argparse import (
269270 ArgumentDefaultsRichHelpFormatter ,
270271 MetavarTypeRichHelpFormatter ,
@@ -274,8 +275,8 @@ def my_completer(self, text, line, begidx, endidx, arg_tokens)
274275)
275276
276277from . import (
277- ansi ,
278278 constants ,
279+ rich_utils ,
279280)
280281
281282if TYPE_CHECKING : # pragma: no cover
@@ -1053,10 +1054,17 @@ def _SubParsersAction_remove_parser(self: argparse._SubParsersAction, name: str)
10531054class Cmd2HelpFormatter (RichHelpFormatter ):
10541055 """Custom help formatter to configure ordering of help text"""
10551056
1056- # Render markup in usage, help, description, and epilog text.
1057- RichHelpFormatter .usage_markup = True
1058- RichHelpFormatter .help_markup = True
1059- RichHelpFormatter .text_markup = True
1057+ # Disable automatic highlighting in the help text.
1058+ highlights = []
1059+
1060+ # Disable markup rendering in usage, help, description, and epilog text.
1061+ # cmd2's built-in commands do not escape opening brackets in their help text
1062+ # and therefore rely on these settings being False. If you desire to use
1063+ # markup in your help text, inherit from a Cmd2 help formatter and override
1064+ # these settings in that child class.
1065+ usage_markup = False
1066+ help_markup = False
1067+ text_markup = False
10601068
10611069 def _format_usage (
10621070 self ,
@@ -1319,24 +1327,23 @@ def __init__(
13191327
13201328 def __rich__ (self ) -> Group :
13211329 """Custom rendering logic."""
1322- import rich
1323-
13241330 formatter = self .formatter_creator ()
13251331
1326- styled_title = rich . text . Text (
1332+ styled_title = Text (
13271333 type (formatter ).group_name_formatter (f"{ self .title } :" ),
13281334 style = formatter .styles ["argparse.groups" ],
13291335 )
13301336
13311337 # Left pad the text like an argparse argument group does
13321338 left_padding = formatter ._indent_increment
1333-
1334- text_table = rich . table . Table (
1339+ text_table = Table (
1340+ Column ( overflow = "fold" ),
13351341 box = None ,
13361342 show_header = False ,
13371343 padding = (0 , 0 , 0 , left_padding ),
13381344 )
13391345 text_table .add_row (self .text )
1346+
13401347 return Group (styled_title , text_table )
13411348
13421349
@@ -1413,12 +1420,20 @@ def error(self, message: str) -> NoReturn:
14131420 linum += 1
14141421
14151422 self .print_usage (sys .stderr )
1416- formatted_message = ansi .style_error (formatted_message )
1417- self .exit (2 , f'{ formatted_message } \n \n ' )
1423+
1424+ # Add failure style to message
1425+ console = self ._get_formatter ().console
1426+ with console .capture () as capture :
1427+ console .print (formatted_message , style = rich_utils .style_failure , crop = False )
1428+ formatted_message = f"{ capture .get ()} "
1429+
1430+ self .exit (2 , f'{ formatted_message } \n ' )
14181431
14191432 def _get_formatter (self ) -> Cmd2HelpFormatter :
1420- """Copy of _get_formatter() with a different return type to assist type checkers."""
1421- return cast (Cmd2HelpFormatter , super ()._get_formatter ())
1433+ """Copy of _get_formatter() with customizations for Cmd2HelpFormatter."""
1434+ formatter = cast (Cmd2HelpFormatter , super ()._get_formatter ())
1435+ formatter .console = rich_utils .Cmd2Console (sys .stdout )
1436+ return formatter
14221437
14231438 def format_help (self ) -> str :
14241439 """Copy of format_help() from argparse.ArgumentParser with tweaks to separately display required parameters"""
@@ -1474,13 +1489,6 @@ def format_help(self) -> str:
14741489 # determine help from format above
14751490 return formatter .format_help () + '\n '
14761491
1477- def _print_message (self , message : str , file : Optional [IO [str ]] = None ) -> None :
1478- # Override _print_message to use style_aware_write() since we use ANSI escape characters to support color
1479- if message :
1480- if file is None :
1481- file = sys .stderr
1482- ansi .style_aware_write (file , message )
1483-
14841492 def create_text_group (self , title : str , text : RenderableType ) -> TextGroup :
14851493 """Create a TextGroup using this parser's formatter creator."""
14861494 return TextGroup (title , text , self ._get_formatter )
0 commit comments