Skip to content

bpo-37030: hide undocumented commands in cmd module #13536

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

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
7 changes: 7 additions & 0 deletions Doc/library/cmd.rst
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,13 @@ 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).

.. versionadded:: 3.8

.. _cmd-example:

Expand Down
14 changes: 12 additions & 2 deletions Lib/cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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:
Copy link
Member

Choose a reason for hiding this comment

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

Please add a test similar to

>>> mycmd.completenames("a")
. In the doctest you can set mycmd.hide_undoc = True and make sure "shell" is not returned by the completenames function for "s". Set it mycmd.hide_undoc = False for rest of the tests to make sure to make sure other tests are not affected.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ok, figured it out and made commit.

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'.
Expand Down Expand Up @@ -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:
Expand Down
4 changes: 4 additions & 0 deletions Lib/test/test_cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
cmd.py: Hide undocumented commands in help and completenames by Aldwin Pollefeyt