Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit aac467e

Browse files
authoredNov 12, 2020
Merge pull request #1007 from bambu/docstr_fmt
Format multiline doc strings to match style of other help messages
2 parents 887bda4 + 86d0e9c commit aac467e

File tree

2 files changed

+25
-5
lines changed

2 files changed

+25
-5
lines changed
 

‎cmd2/cmd2.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import inspect
3535
import os
3636
import pickle
37+
import pydoc
3738
import re
3839
import sys
3940
import threading
@@ -3155,17 +3156,21 @@ def do_help(self, args: argparse.Namespace) -> None:
31553156
# Set end to blank so the help output matches how it looks when "command -h" is used
31563157
self.poutput(completer.format_help(tokens), end='')
31573158

3159+
# If there is a help func delegate to do_help
3160+
elif help_func is not None:
3161+
super().do_help(args.command)
3162+
3163+
# If there's no help_func __doc__ then format and output it
3164+
elif func is not None and func.__doc__ is not None:
3165+
self.poutput(pydoc.getdoc(func))
3166+
31583167
# If there is no help information then print an error
3159-
elif help_func is None and (func is None or not func.__doc__):
3168+
else:
31603169
err_msg = self.help_error.format(args.command)
31613170

31623171
# Set apply_style to False so help_error's style is not overridden
31633172
self.perror(err_msg, apply_style=False)
31643173

3165-
# Otherwise delegate to cmd base class do_help()
3166-
else:
3167-
super().do_help(args.command)
3168-
31693174
def _help_menu(self, verbose: bool = False) -> None:
31703175
"""Show a list of commands which help can be displayed for"""
31713176
cmds_cats, cmds_doc, cmds_undoc, help_topics = self._build_command_info()

‎tests/test_cmd2.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -981,6 +981,16 @@ def do_edit(self, arg):
981981
def do_undoc(self, arg):
982982
pass
983983

984+
def do_multiline_docstr(self, arg):
985+
"""
986+
This documentation
987+
is multiple lines
988+
and there are no
989+
tabs
990+
"""
991+
pass
992+
993+
984994
@pytest.fixture
985995
def help_app():
986996
app = HelpApp()
@@ -1004,6 +1014,11 @@ def test_help_overridden_method(help_app):
10041014
expected = normalize('This overrides the edit command and does nothing.')
10051015
assert out == expected
10061016

1017+
def test_help_multiline_docstring(help_app):
1018+
out, err = run_cmd(help_app, 'help multiline_docstr')
1019+
expected = normalize('This documentation\nis multiple lines\nand there are no\ntabs')
1020+
assert out == expected
1021+
10071022

10081023
class HelpCategoriesApp(cmd2.Cmd):
10091024
"""Class for testing custom help_* methods which override docstring help."""

0 commit comments

Comments
 (0)
Please sign in to comment.