summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTodd Leonhardt <todd.leonhardt@gmail.com>2017-06-01 21:22:00 -0400
committerTodd Leonhardt <todd.leonhardt@gmail.com>2017-06-01 21:22:00 -0400
commit67c70521d6520c3cb2906ec428de4ccc6958d784 (patch)
treed6d6165e6949a03377c19e674949f206c1cdbc4c
parent80705c68646dfe7704267ffb072d5a27f8b1da92 (diff)
downloadcmd2-git-67c70521d6520c3cb2906ec428de4ccc6958d784.tar.gz
Added "exclude_from_help" list as a cmd2.Cmd public attribute
Added a list of commands to explicitly exclude displaying in the help menu of commands which detailed help can be retrieved for. By default, only "do_eof" is in this list since that isn't a command which is ever intended for an end user to directly enter on the command line. Also updated unit tests to reflect new default help menu.
-rwxr-xr-xcmd2.py44
-rw-r--r--tests/conftest.py4
-rw-r--r--tests/test_transcript.py5
-rw-r--r--tests/transcript.txt5
4 files changed, 51 insertions, 7 deletions
diff --git a/cmd2.py b/cmd2.py
index deeade73..f2642cb0 100755
--- a/cmd2.py
+++ b/cmd2.py
@@ -587,6 +587,7 @@ class Cmd(cmd.Cmd):
default_to_shell = False # Attempt to run unrecognized commands as shell commands
defaultExtension = 'txt' # For ``save``, ``load``, etc.
excludeFromHistory = '''run r list l history hi ed edit li eof'''.split()
+ exclude_from_help = ['do_eof'] # Commands to exclude from the help menu
# make sure your terminators are not in legalChars!
legalChars = u'!#$%.:?@_-' + pyparsing.alphanums + pyparsing.alphas8bit
@@ -1127,15 +1128,56 @@ class Cmd(cmd.Cmd):
def do_help(self, arg):
"""List available commands with "help" or detailed help with "help cmd"."""
if arg:
+ # Getting help for a specific command
funcname = self._func_named(arg)
if funcname:
fn = getattr(self, funcname)
try:
+ # Use Optparse help for @options commands
fn.optionParser.print_help(file=self.stdout)
except AttributeError:
+ # No special behavior needed, delegate to cmd base class do_help()
cmd.Cmd.do_help(self, funcname[3:])
else:
- cmd.Cmd.do_help(self, arg)
+ # Show a menu of what commands help can be gotten for
+ self._help_menu()
+
+ def _help_menu(self):
+ """Show a list of commands which help can be displayed for.
+ """
+ # Get a list of all method names
+ names = self.get_names()
+
+ # Remove any command names which are explicitly excluded from the help menu
+ for name in self.exclude_from_help:
+ names.remove(name)
+
+ cmds_doc = []
+ cmds_undoc = []
+ help_dict = {}
+ for name in names:
+ if name[:5] == 'help_':
+ help_dict[name[5:]] = 1
+ names.sort()
+ # There can be duplicates if routines overridden
+ prevname = ''
+ for name in names:
+ if name[:3] == 'do_':
+ if name == prevname:
+ continue
+ prevname = name
+ command = name[3:]
+ if command in help_dict:
+ cmds_doc.append(command)
+ del help_dict[command]
+ elif getattr(self, name).__doc__:
+ cmds_doc.append(command)
+ else:
+ cmds_undoc.append(command)
+ 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_dict.keys()), 15, 80)
+ self.print_topics(self.undoc_header, cmds_undoc, 15, 80)
# noinspection PyUnusedLocal
def do_shortcuts(self, args):
diff --git a/tests/conftest.py b/tests/conftest.py
index b036943d..3977de5f 100644
--- a/tests/conftest.py
+++ b/tests/conftest.py
@@ -15,8 +15,8 @@ import cmd2
# Help text for base cmd2.Cmd application
BASE_HELP = """Documented commands (type help <topic>):
========================================
-_relative_load edit help list pause quit save shell show
-cmdenvironment eof history load py run set shortcuts
+_relative_load edit history load py run set shortcuts
+cmdenvironment help list pause quit save shell show
"""
# Help text for the history command
diff --git a/tests/test_transcript.py b/tests/test_transcript.py
index 6049119f..c31b519d 100644
--- a/tests/test_transcript.py
+++ b/tests/test_transcript.py
@@ -106,8 +106,9 @@ def test_base_with_transcript(_cmdline_app):
Documented commands (type help <topic>):
========================================
-_relative_load edit help list orate py run say shell show
-cmdenvironment eof history load pause quit save set shortcuts speak
+_relative_load help load py save shell speak
+cmdenvironment history orate quit say shortcuts
+edit list pause run set show
(Cmd) help say
Repeats what you tell me to.
diff --git a/tests/transcript.txt b/tests/transcript.txt
index 1e10ee31..013e2d0f 100644
--- a/tests/transcript.txt
+++ b/tests/transcript.txt
@@ -2,8 +2,9 @@
Documented commands (type help <topic>):
========================================
-_relative_load edit help list orate py run say shell show
-cmdenvironment eof history load pause quit save set shortcuts speak
+_relative_load help load py save shell speak
+cmdenvironment history orate quit say shortcuts
+edit list pause run set show
(Cmd) help say
Repeats what you tell me to.