Skip to content

Commit 5746cd7

Browse files
authored
Add support for rich-argparse (#1453)
* Integrated rich-argparse. * Reformatted some imports. * Updated comments.
1 parent 2433b2f commit 5746cd7

File tree

4 files changed

+40
-13
lines changed

4 files changed

+40
-13
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## 2.7.0 (TBD)
2+
3+
- Enhancements
4+
- Integrated rich-argparse with cmd2's default argparse help formatter.
5+
16
## 2.6.2 (June 26, 2025)
27

38
- Enhancements

cmd2/argparse_custom.py

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -229,14 +229,17 @@ def my_completer(self, text, line, begidx, endidx, arg_tokens)
229229
ZERO_OR_MORE,
230230
ArgumentError,
231231
)
232-
from collections.abc import Callable, Iterable, Sequence
233-
from gettext import (
234-
gettext,
232+
from collections.abc import (
233+
Callable,
234+
Iterable,
235+
Sequence,
235236
)
237+
from gettext import gettext
236238
from typing import (
237239
IO,
238240
TYPE_CHECKING,
239241
Any,
242+
ClassVar,
240243
NoReturn,
241244
Optional,
242245
Protocol,
@@ -245,6 +248,8 @@ def my_completer(self, text, line, begidx, endidx, arg_tokens)
245248
runtime_checkable,
246249
)
247250

251+
from rich_argparse import RawTextRichHelpFormatter
252+
248253
from . import (
249254
ansi,
250255
constants,
@@ -991,9 +996,25 @@ def _SubParsersAction_remove_parser(self: argparse._SubParsersAction, name: str)
991996
############################################################################################################
992997

993998

994-
class Cmd2HelpFormatter(argparse.RawTextHelpFormatter):
999+
class Cmd2HelpFormatter(RawTextRichHelpFormatter):
9951000
"""Custom help formatter to configure ordering of help text."""
9961001

1002+
# rich-argparse formats all group names with str.title().
1003+
# Override their formatter to do nothing.
1004+
group_name_formatter: ClassVar[Callable[[str], str]] = str
1005+
1006+
# Disable automatic highlighting in the help text.
1007+
highlights: ClassVar[list[str]] = []
1008+
1009+
# Disable markup rendering in usage, help, description, and epilog text.
1010+
# cmd2's built-in commands do not escape opening brackets in their help text
1011+
# and therefore rely on these settings being False. If you desire to use
1012+
# markup in your help text, inherit from Cmd2HelpFormatter and override
1013+
# these settings in that child class.
1014+
usage_markup: ClassVar[bool] = False
1015+
help_markup: ClassVar[bool] = False
1016+
text_markup: ClassVar[bool] = False
1017+
9971018
def _format_usage(
9981019
self,
9991020
usage: Optional[str],

cmd2/cmd2.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,16 @@
4242
import sys
4343
import tempfile
4444
import threading
45-
from code import (
46-
InteractiveConsole,
47-
)
45+
from code import InteractiveConsole
4846
from collections import (
4947
OrderedDict,
5048
namedtuple,
5149
)
52-
from collections.abc import Callable, Iterable, Mapping
50+
from collections.abc import (
51+
Callable,
52+
Iterable,
53+
Mapping,
54+
)
5355
from types import (
5456
FrameType,
5557
ModuleType,
@@ -763,8 +765,8 @@ def _build_parser(
763765
"""Build argument parser for a command/subcommand.
764766
765767
:param parent: CommandParent object which owns the command using the parser.
766-
This function assumes that parent is where parser_builder
767-
is defined when parser_builder is a classmethod.
768+
When parser_builder is a classmethod, this function passes
769+
parent's class to it.
768770
:param parser_builder: means used to build the parser
769771
:param prog: prog value to set in new parser
770772
:return: new parser
@@ -781,9 +783,7 @@ def _build_parser(
781783
else:
782784
raise TypeError(f"Invalid type for parser_builder: {type(parser_builder)}")
783785

784-
from .decorators import (
785-
_set_parser_prog,
786-
)
786+
from .decorators import _set_parser_prog
787787

788788
_set_parser_prog(parser, prog)
789789

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ dependencies = [
3232
"gnureadline>=8; platform_system == 'Darwin'",
3333
"pyperclip>=1.8",
3434
"pyreadline3>=3.4; platform_system == 'Windows'",
35+
"rich-argparse>=1.7.1",
3536
"wcwidth>=0.2.10",
3637
]
3738

0 commit comments

Comments
 (0)