]> granicus.if.org Git - python/commitdiff
#5991: let completion for the "help" command include help topics.
authorGeorg Brandl <georg@python.org>
Wed, 6 Jan 2010 18:02:16 +0000 (18:02 +0000)
committerGeorg Brandl <georg@python.org>
Wed, 6 Jan 2010 18:02:16 +0000 (18:02 +0000)
This also simplifies the Cmd.get_names() method implementation; it was written
at a time where dir() didn't consider base class attributes.

Lib/cmd.py
Lib/test/test_cmd.py

index 3f82b487103fffb3b8e67320dc78f0d0ac363bba..5847f763608d9639bafb9677e84d63f0f02776d4 100644 (file)
@@ -281,19 +281,15 @@ class Cmd:
             return None
 
     def get_names(self):
-        # Inheritance says we have to look in class and
-        # base classes; order is not important.
-        names = []
-        classes = [self.__class__]
-        while classes:
-            aclass = classes.pop(0)
-            if aclass.__bases__:
-                classes = classes + list(aclass.__bases__)
-            names = names + dir(aclass)
-        return names
+        # This method used to pull in base class attributes
+        # at a time dir() didn't do it yet.
+        return dir(self.__class__)
 
     def complete_help(self, *args):
-        return self.completenames(*args)
+        commands = set(self.completenames(*args))
+        topics = set(a[5:] for a in self.get_names()
+                     if a.startswith('help_' + args[0]))
+        return list(commands | topics)
 
     def do_help(self, arg):
         if arg:
index 8898a32cb91e4ecd3f2a9b76d60b429cf52943c6..2ee5cef4727406bb1dcdba08763e2e237226b691 100644 (file)
@@ -57,15 +57,17 @@ class samplecmdclass(cmd.Cmd):
     >>> mycmd.completenames("12")
     []
     >>> mycmd.completenames("help")
-    ['help', 'help']
+    ['help']
 
     Test for the function complete_help():
     >>> mycmd.complete_help("a")
     ['add']
     >>> mycmd.complete_help("he")
-    ['help', 'help']
+    ['help']
     >>> mycmd.complete_help("12")
     []
+    >>> sorted(mycmd.complete_help(""))
+    ['add', 'exit', 'help', 'shell']
 
     Test for the function do_help():
     >>> mycmd.do_help("testet")