From 19598b68106333d44a91aa6f62590232dff443ed Mon Sep 17 00:00:00 2001 From: Bambu <564972+bambu@users.noreply.github.com> Date: Fri, 9 Oct 2020 23:27:02 -0400 Subject: Format multiline docstrings to match other help messages Changed cmd2 do_cmd to dedent docstrings using `pydoc.getdoc`. This patch provides output for docstrings that look like using argparse or a single line docstring --- cmd2/cmd2.py | 14 ++++++++++---- tests/test_cmd2.py | 15 +++++++++++++++ 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/cmd2/cmd2.py b/cmd2/cmd2.py index ea4e01de..12332f87 100644 --- a/cmd2/cmd2.py +++ b/cmd2/cmd2.py @@ -34,6 +34,7 @@ import glob import inspect import os import pickle +import pydoc import re import sys import threading @@ -3155,16 +3156,21 @@ class Cmd(cmd.Cmd): # Set end to blank so the help output matches how it looks when "command -h" is used self.poutput(completer.format_help(tokens), end='') + # If there is a help func delegate to do_help + elif help_func: + super().do_help(args.command) + + # If there's no help_func __doc__ then format and output it + elif func and func.__doc__: + self.poutput(pydoc.getdoc(func)) + # If there is no help information then print an error - elif help_func is None and (func is None or not func.__doc__): + else: err_msg = self.help_error.format(args.command) # Set apply_style to False so help_error's style is not overridden self.perror(err_msg, apply_style=False) - # Otherwise delegate to cmd base class do_help() - else: - super().do_help(args.command) def _help_menu(self, verbose: bool = False) -> None: """Show a list of commands which help can be displayed for""" diff --git a/tests/test_cmd2.py b/tests/test_cmd2.py index 034f7096..554f2ba7 100755 --- a/tests/test_cmd2.py +++ b/tests/test_cmd2.py @@ -981,6 +981,16 @@ class HelpApp(cmd2.Cmd): def do_undoc(self, arg): pass + def do_multiline_docstr(self, arg): + """ + This documentation + is multiple lines + and there are no + tabs + """ + pass + + @pytest.fixture def help_app(): app = HelpApp() @@ -1004,6 +1014,11 @@ def test_help_overridden_method(help_app): expected = normalize('This overrides the edit command and does nothing.') assert out == expected +def test_help_multiline_docstring(help_app): + out, err = run_cmd(help_app, 'help multiline_docstr') + expected = normalize('This documentation\nis multiple lines\nand there are no\ntabs') + assert out == expected + class HelpCategoriesApp(cmd2.Cmd): """Class for testing custom help_* methods which override docstring help.""" -- cgit v1.2.1 From 6203e2d24ce974272ad57e397227bb393f0733ee Mon Sep 17 00:00:00 2001 From: Bambu <564972+bambu@users.noreply.github.com> Date: Mon, 19 Oct 2020 21:48:31 -0400 Subject: fix flake8 error --- cmd2/cmd2.py | 1 - 1 file changed, 1 deletion(-) diff --git a/cmd2/cmd2.py b/cmd2/cmd2.py index 12332f87..dd1f4866 100644 --- a/cmd2/cmd2.py +++ b/cmd2/cmd2.py @@ -3171,7 +3171,6 @@ class Cmd(cmd.Cmd): # Set apply_style to False so help_error's style is not overridden self.perror(err_msg, apply_style=False) - def _help_menu(self, verbose: bool = False) -> None: """Show a list of commands which help can be displayed for""" cmds_cats, cmds_doc, cmds_undoc, help_topics = self._build_command_info() -- cgit v1.2.1 From c77c240ce02c81973b57da322082da51b9bc08d2 Mon Sep 17 00:00:00 2001 From: Bambu <564972+bambu@users.noreply.github.com> Date: Wed, 11 Nov 2020 15:14:15 -0500 Subject: specifying is not none on coditions --- cmd2/cmd2.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cmd2/cmd2.py b/cmd2/cmd2.py index dd1f4866..f7d1fc2a 100644 --- a/cmd2/cmd2.py +++ b/cmd2/cmd2.py @@ -3157,11 +3157,11 @@ class Cmd(cmd.Cmd): self.poutput(completer.format_help(tokens), end='') # If there is a help func delegate to do_help - elif help_func: + elif help_func is not None: super().do_help(args.command) # If there's no help_func __doc__ then format and output it - elif func and func.__doc__: + elif func is not None and func.__doc__ is not None: self.poutput(pydoc.getdoc(func)) # If there is no help information then print an error -- cgit v1.2.1