From cb956e577f5c2df8d015e096b41fc93ce87c308a Mon Sep 17 00:00:00 2001 From: Kevin Van Brunt Date: Thu, 26 Jun 2025 15:00:53 -0400 Subject: [PATCH 1/3] Integrated rich-argparse. --- cmd2/argparse_custom.py | 23 ++++++++++++++++++++++- pyproject.toml | 1 + 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/cmd2/argparse_custom.py b/cmd2/argparse_custom.py index 01eea688..3252ffb7 100644 --- a/cmd2/argparse_custom.py +++ b/cmd2/argparse_custom.py @@ -237,6 +237,7 @@ def my_completer(self, text, line, begidx, endidx, arg_tokens) IO, TYPE_CHECKING, Any, + ClassVar, NoReturn, Optional, Protocol, @@ -245,6 +246,10 @@ def my_completer(self, text, line, begidx, endidx, arg_tokens) runtime_checkable, ) +from rich_argparse import ( + RawTextRichHelpFormatter, +) + from . import ( ansi, constants, @@ -991,9 +996,25 @@ def _SubParsersAction_remove_parser(self: argparse._SubParsersAction, name: str) ############################################################################################################ -class Cmd2HelpFormatter(argparse.RawTextHelpFormatter): +class Cmd2HelpFormatter(RawTextRichHelpFormatter): """Custom help formatter to configure ordering of help text.""" + # rich-argparse formats all group names with str.title(). + # Override their formatter to do nothing. + group_name_formatter: ClassVar[Callable[[str], str]] = str + + # Disable automatic highlighting in the help text. + highlights: ClassVar[list[str]] = [] + + # Disable markup rendering in usage, help, description, and epilog text. + # cmd2's built-in commands do not escape opening brackets in their help text + # and therefore rely on these settings being False. If you desire to use + # markup in your help text, inherit from a Cmd2 help formatter and override + # these settings in that child class. + usage_markup: ClassVar[bool] = False + help_markup: ClassVar[bool] = False + text_markup: ClassVar[bool] = False + def _format_usage( self, usage: Optional[str], diff --git a/pyproject.toml b/pyproject.toml index 163f5e42..4794983f 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -32,6 +32,7 @@ dependencies = [ "gnureadline>=8; platform_system == 'Darwin'", "pyperclip>=1.8", "pyreadline3>=3.4; platform_system == 'Windows'", + "rich-argparse>=1.7.1", "wcwidth>=0.2.10", ] From 4fe1b05c478f20d20f61f331c131e7fcf21aa183 Mon Sep 17 00:00:00 2001 From: Kevin Van Brunt Date: Thu, 26 Jun 2025 16:20:02 -0400 Subject: [PATCH 2/3] Reformatted some imports. --- cmd2/argparse_custom.py | 12 ++++++------ cmd2/cmd2.py | 14 +++++++------- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/cmd2/argparse_custom.py b/cmd2/argparse_custom.py index 3252ffb7..c4bde409 100644 --- a/cmd2/argparse_custom.py +++ b/cmd2/argparse_custom.py @@ -229,10 +229,12 @@ def my_completer(self, text, line, begidx, endidx, arg_tokens) ZERO_OR_MORE, ArgumentError, ) -from collections.abc import Callable, Iterable, Sequence -from gettext import ( - gettext, +from collections.abc import ( + Callable, + Iterable, + Sequence, ) +from gettext import gettext from typing import ( IO, TYPE_CHECKING, @@ -246,9 +248,7 @@ def my_completer(self, text, line, begidx, endidx, arg_tokens) runtime_checkable, ) -from rich_argparse import ( - RawTextRichHelpFormatter, -) +from rich_argparse import RawTextRichHelpFormatter from . import ( ansi, diff --git a/cmd2/cmd2.py b/cmd2/cmd2.py index de2ae157..f89ed9b2 100644 --- a/cmd2/cmd2.py +++ b/cmd2/cmd2.py @@ -42,14 +42,16 @@ import sys import tempfile import threading -from code import ( - InteractiveConsole, -) +from code import InteractiveConsole from collections import ( OrderedDict, namedtuple, ) -from collections.abc import Callable, Iterable, Mapping +from collections.abc import ( + Callable, + Iterable, + Mapping, +) from types import ( FrameType, ModuleType, @@ -781,9 +783,7 @@ def _build_parser( else: raise TypeError(f"Invalid type for parser_builder: {type(parser_builder)}") - from .decorators import ( - _set_parser_prog, - ) + from .decorators import _set_parser_prog _set_parser_prog(parser, prog) From 4aef80f0ea7af1fd032e149099a28aafa7f818fe Mon Sep 17 00:00:00 2001 From: Kevin Van Brunt Date: Mon, 30 Jun 2025 00:07:07 -0400 Subject: [PATCH 3/3] Updated comments. --- CHANGELOG.md | 5 +++++ cmd2/argparse_custom.py | 2 +- cmd2/cmd2.py | 4 ++-- 3 files changed, 8 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f4f96de4..500079f1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,8 @@ +## 2.7.0 (TBD) + +- Enhancements + - Integrated rich-argparse with cmd2's default argparse help formatter. + ## 2.6.2 (June 26, 2025) - Enhancements diff --git a/cmd2/argparse_custom.py b/cmd2/argparse_custom.py index c4bde409..d67ccbe8 100644 --- a/cmd2/argparse_custom.py +++ b/cmd2/argparse_custom.py @@ -1009,7 +1009,7 @@ class Cmd2HelpFormatter(RawTextRichHelpFormatter): # Disable markup rendering in usage, help, description, and epilog text. # cmd2's built-in commands do not escape opening brackets in their help text # and therefore rely on these settings being False. If you desire to use - # markup in your help text, inherit from a Cmd2 help formatter and override + # markup in your help text, inherit from Cmd2HelpFormatter and override # these settings in that child class. usage_markup: ClassVar[bool] = False help_markup: ClassVar[bool] = False diff --git a/cmd2/cmd2.py b/cmd2/cmd2.py index f89ed9b2..898aad07 100644 --- a/cmd2/cmd2.py +++ b/cmd2/cmd2.py @@ -765,8 +765,8 @@ def _build_parser( """Build argument parser for a command/subcommand. :param parent: CommandParent object which owns the command using the parser. - This function assumes that parent is where parser_builder - is defined when parser_builder is a classmethod. + When parser_builder is a classmethod, this function passes + parent's class to it. :param parser_builder: means used to build the parser :param prog: prog value to set in new parser :return: new parser