From f683815bd93dfed809b025eac1b0784badd6f6de Mon Sep 17 00:00:00 2001 From: aldwinaldwin Date: Fri, 24 May 2019 15:26:57 +0800 Subject: [PATCH 1/5] bpo-37030: Lib/cmd.py: Hide undocumented commands in help and completenames A flag, defaulting to false. If true, :meth:`do_help` and :meth:`completenames` won't include undocumented commands (that is, there are do_*() methods without corresponding help_*() methods). --- Doc/library/cmd.rst | 5 +++++ Lib/cmd.py | 14 ++++++++++++-- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/Doc/library/cmd.rst b/Doc/library/cmd.rst index d57edb7eb1698d..b7e66fed953682 100644 --- a/Doc/library/cmd.rst +++ b/Doc/library/cmd.rst @@ -217,6 +217,11 @@ Instances of :class:`Cmd` subclasses have some public instance variables: :mod:`readline`, on systems that support it, the interpreter will automatically support :program:`Emacs`\ -like line editing and command-history keystrokes.) +.. attribute:: Cmd.hide_undoc + + A flag, defaulting to false. If true, :meth:`do_help` and :meth:`completenames` + won't include undocumented commands (that is, there are do_*() methods without + corresponding help_*() methods). .. _cmd-example: diff --git a/Lib/cmd.py b/Lib/cmd.py index 859e91096d8f57..6e5101b5cb1155 100644 --- a/Lib/cmd.py +++ b/Lib/cmd.py @@ -72,6 +72,7 @@ class Cmd: undoc_header = "Undocumented commands:" nohelp = "*** No help on %s" use_rawinput = 1 + hide_undoc = 0 def __init__(self, completekey='tab', stdin=None, stdout=None): """Instantiate a line-oriented interpreter framework. @@ -245,8 +246,16 @@ def completedefault(self, *ignored): return [] def completenames(self, text, *ignored): + """ Method returns all commands. + + If hide_undoc is True, only the commands with "help_" are returned. + """ dotext = 'do_'+text - return [a[3:] for a in self.get_names() if a.startswith(dotext)] + names = self.get_names() + commands = [a[3:] for a in names if a.startswith(dotext)] + if self.hide_undoc: + commands = [a for a in commands if 'help_' + a in names] + return commands def complete(self, text, state): """Return the next possible completion for 'text'. @@ -333,7 +342,8 @@ def do_help(self, arg): self.stdout.write("%s\n"%str(self.doc_leader)) self.print_topics(self.doc_header, cmds_doc, 15,80) self.print_topics(self.misc_header, list(help.keys()),15,80) - self.print_topics(self.undoc_header, cmds_undoc, 15,80) + if not self.hide_undoc: + self.print_topics(self.undoc_header, cmds_undoc, 15,80) def print_topics(self, header, cmds, cmdlen, maxcol): if cmds: From 11627d0dfbe8c32b57668229b04998339e6a95ff Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" Date: Fri, 24 May 2019 07:40:57 +0000 Subject: [PATCH 2/5] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../NEWS.d/next/Library/2019-05-24-07-40-56.bpo-37030.xs7uwm.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2019-05-24-07-40-56.bpo-37030.xs7uwm.rst diff --git a/Misc/NEWS.d/next/Library/2019-05-24-07-40-56.bpo-37030.xs7uwm.rst b/Misc/NEWS.d/next/Library/2019-05-24-07-40-56.bpo-37030.xs7uwm.rst new file mode 100644 index 00000000000000..bb57721b507502 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2019-05-24-07-40-56.bpo-37030.xs7uwm.rst @@ -0,0 +1 @@ +cmd.py: Hide undocumented commands in help and completenames \ No newline at end of file From c90a87d2a665adc1f7b4894538cc8e6c43ab45cb Mon Sep 17 00:00:00 2001 From: aldwinaldwin Date: Fri, 24 May 2019 17:56:32 +0800 Subject: [PATCH 3/5] a .. versionadded:: 3.8 directive since it's a new attribute. --- Doc/library/cmd.rst | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Doc/library/cmd.rst b/Doc/library/cmd.rst index b7e66fed953682..dd5dbf281f7e08 100644 --- a/Doc/library/cmd.rst +++ b/Doc/library/cmd.rst @@ -223,6 +223,8 @@ Instances of :class:`Cmd` subclasses have some public instance variables: won't include undocumented commands (that is, there are do_*() methods without corresponding help_*() methods). + .. versionadded:: 3.8 + .. _cmd-example: Cmd Example From dbcbedd4a8779dc55bba1fef514ff2d28de5bb1d Mon Sep 17 00:00:00 2001 From: aldwinaldwin Date: Fri, 24 May 2019 18:11:44 +0800 Subject: [PATCH 4/5] bpo-37030: added test --- Lib/test/test_cmd.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Lib/test/test_cmd.py b/Lib/test/test_cmd.py index 96e0c30da328cf..f589b481c34326 100644 --- a/Lib/test/test_cmd.py +++ b/Lib/test/test_cmd.py @@ -60,6 +60,10 @@ class samplecmdclass(cmd.Cmd): [] >>> mycmd.completenames("help") ['help'] + >>> mycmd.hide_undoc = True + >>> mycmd.completenames("s") + [] + >>> mycmd.hide_undoc = False Test for the function complete_help(): >>> mycmd.complete_help("a") From f0187956b068d89d1e812debc76fc1593daa5bac Mon Sep 17 00:00:00 2001 From: aldwinaldwin Date: Fri, 24 May 2019 19:21:00 +0800 Subject: [PATCH 5/5] Added name --- .../next/Library/2019-05-24-07-40-56.bpo-37030.xs7uwm.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Library/2019-05-24-07-40-56.bpo-37030.xs7uwm.rst b/Misc/NEWS.d/next/Library/2019-05-24-07-40-56.bpo-37030.xs7uwm.rst index bb57721b507502..66b7f65bf219a2 100644 --- a/Misc/NEWS.d/next/Library/2019-05-24-07-40-56.bpo-37030.xs7uwm.rst +++ b/Misc/NEWS.d/next/Library/2019-05-24-07-40-56.bpo-37030.xs7uwm.rst @@ -1 +1 @@ -cmd.py: Hide undocumented commands in help and completenames \ No newline at end of file +cmd.py: Hide undocumented commands in help and completenames by Aldwin Pollefeyt