Skip to content

Commit

Permalink
Don't use parser description in verbose help output since it may be a…
Browse files Browse the repository at this point in the history
… Rich object.
  • Loading branch information
kmvanbrunt committed Nov 28, 2024
1 parent bfca4e9 commit 4616b09
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 9 deletions.
10 changes: 3 additions & 7 deletions cmd2/cmd2.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,8 +273,8 @@ def get(self, command_method: CommandFunc) -> Optional[argparse.ArgumentParser]:
_set_parser_prog(parser, command)

# If the description has not been set, then use the method docstring if one exists
if parser.description is None and hasattr(command_method, '__wrapped__') and command_method.__wrapped__.__doc__:
parser.description = strip_doc_annotations(command_method.__wrapped__.__doc__)
if parser.description is None and command_method.__doc__:
parser.description = strip_doc_annotations(command_method.__doc__)

self._parsers[full_method_name] = parser

Expand Down Expand Up @@ -3753,12 +3753,8 @@ def _print_topics(self, header: str, cmds: list[str], verbose: bool) -> None:

doc: Optional[str]

# If this is an argparse command, use its description.
if (cmd_parser := self._command_parsers.get(cmd_func)) is not None:
doc = cmd_parser.description

# Non-argparse commands can have help_functions for their documentation
elif command in topics:
if command in topics:
help_func = getattr(self, constants.HELP_FUNC_PREFIX + command)
result = io.StringIO()

Expand Down
5 changes: 3 additions & 2 deletions tests/test_cmd2.py
Original file line number Diff line number Diff line change
Expand Up @@ -1256,9 +1256,10 @@ def test_help_multiline_docstring(help_app):
assert help_app.last_result is True


def test_help_verbose_uses_parser_description(help_app: HelpApp):
def test_help_verbose_uses_parser_docstring(help_app: HelpApp):
out, err = run_cmd(help_app, 'help --verbose')
verify_help_text(help_app, out, verbose_strings=[help_app.parser_cmd_parser.description])
expected_verbose = utils.strip_doc_annotations(help_app.do_parser_cmd.__doc__)
verify_help_text(help_app, out, verbose_strings=[expected_verbose])


class HelpCategoriesApp(cmd2.Cmd):
Expand Down

0 comments on commit 4616b09

Please sign in to comment.