Skip to content

Add support for rich-argparse #1453

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 30, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
29 changes: 25 additions & 4 deletions cmd2/argparse_custom.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,14 +229,17 @@ 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,
Any,
ClassVar,
NoReturn,
Optional,
Protocol,
Expand All @@ -245,6 +248,8 @@ def my_completer(self, text, line, begidx, endidx, arg_tokens)
runtime_checkable,
)

from rich_argparse import RawTextRichHelpFormatter

from . import (
ansi,
constants,
Expand Down Expand Up @@ -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 Cmd2HelpFormatter and override
# these settings in that child class.
usage_markup: ClassVar[bool] = False
help_markup: ClassVar[bool] = False
text_markup: ClassVar[bool] = False
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the future, it might be worth exploring intentionally using this markup rendering. If the format used by rich-argparse can be compatible with that used by mkdocstrings, then this could be a good thing.


def _format_usage(
self,
usage: Optional[str],
Expand Down
18 changes: 9 additions & 9 deletions cmd2/cmd2.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -763,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
Expand All @@ -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)

Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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",
]

Expand Down
Loading