Commit 89040530 authored by Georg Brandl's avatar Georg Brandl

#5991: let completion for the "help" command include help topics.

This also simplifies the Cmd.get_names() method implementation; it was written
at a time where dir() didn't consider base class attributes.
parent 5089a38a
...@@ -281,19 +281,15 @@ class Cmd: ...@@ -281,19 +281,15 @@ class Cmd:
return None return None
def get_names(self): def get_names(self):
# Inheritance says we have to look in class and # This method used to pull in base class attributes
# base classes; order is not important. # at a time dir() didn't do it yet.
names = [] return dir(self.__class__)
classes = [self.__class__]
while classes:
aclass = classes.pop(0)
if aclass.__bases__:
classes = classes + list(aclass.__bases__)
names = names + dir(aclass)
return names
def complete_help(self, *args): 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): def do_help(self, arg):
if arg: if arg:
......
...@@ -57,15 +57,17 @@ class samplecmdclass(cmd.Cmd): ...@@ -57,15 +57,17 @@ class samplecmdclass(cmd.Cmd):
>>> mycmd.completenames("12") >>> mycmd.completenames("12")
[] []
>>> mycmd.completenames("help") >>> mycmd.completenames("help")
['help', 'help'] ['help']
Test for the function complete_help(): Test for the function complete_help():
>>> mycmd.complete_help("a") >>> mycmd.complete_help("a")
['add'] ['add']
>>> mycmd.complete_help("he") >>> mycmd.complete_help("he")
['help', 'help'] ['help']
>>> mycmd.complete_help("12") >>> mycmd.complete_help("12")
[] []
>>> sorted(mycmd.complete_help(""))
['add', 'exit', 'help', 'shell']
Test for the function do_help(): Test for the function do_help():
>>> mycmd.do_help("testet") >>> mycmd.do_help("testet")
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment