我在 Python 中使用 cmd.Cmd
类为我的程序提供一个简单的 readline 接口(interface)。
独立示例:
from cmd import Cmd
class CommandParser(Cmd):
def do_x(self, line):
pass
def do_xy(self, line):
pass
def do_xyz(self, line):
pass
if __name__ == "__main__":
parser = CommandParser()
parser.cmdloop()
按两次 Tab 将显示可能性。再次按 Tab 键会产生同样的效果。
我的问题是,如何获得在第三次按 Tab 键时循环的选项?在 readline 术语中,我认为这称为 Tab: menu-complete
,但我看不到如何将其应用于 Cmd
实例。
我已经试过了:
readline.parse_and_bind('Tab: menu-complete')
实例化解析器实例之前和之后。运气不好。
我还尝试将 "Tab: menu-complete"
传递给 Cmd
构造函数。这里也没有运气。
有人知道这是怎么做到的吗?
干杯!
最佳答案
不幸的是,似乎唯一的解决方法是从 cmd.Cmd
类中猴子修补方法 cmdloop
,或者自己动手。
正确的方法是使用 "Tab: menu-complete"
,但是它被类覆盖了,如第 115 行所示:readline.parse_and_bind(self.completekey+": complete")
,它永远不会被激活。 (对于第 115 行和整个 cmd
包,请参见:https://hg.python.org/cpython/file/2.7/Lib/cmd.py)。我在下面展示了该函数的编辑版本,以及如何使用它:
import cmd
# note: taken from Python's library: https://hg.python.org/cpython/file/2.7/Lib/cmd.py
def cmdloop(self, intro=None):
"""Repeatedly issue a prompt, accept input, parse an initial prefix
off the received input, and dispatch to action methods, passing them
the remainder of the line as argument.
"""
self.preloop()
if self.use_rawinput and self.completekey:
try:
import readline
self.old_completer = readline.get_completer()
readline.set_completer(self.complete)
readline.parse_and_bind(self.completekey+": menu-complete") # <---
except ImportError:
pass
try:
if intro is not None:
self.intro = intro
if self.intro:
self.stdout.write(str(self.intro)+"\n")
stop = None
while not stop:
if self.cmdqueue:
line = self.cmdqueue.pop(0)
else:
if self.use_rawinput:
try:
line = raw_input(self.prompt)
except EOFError:
line = 'EOF'
else:
self.stdout.write(self.prompt)
self.stdout.flush()
line = self.stdin.readline()
if not len(line):
line = 'EOF'
else:
line = line.rstrip('\r\n')
line = self.precmd(line)
stop = self.onecmd(line)
stop = self.postcmd(stop, line)
self.postloop()
finally:
if self.use_rawinput and self.completekey:
try:
import readline
readline.set_completer(self.old_completer)
except ImportError:
pass
# monkey-patch - make sure this is done before any sort of inheritance is used!
cmd.Cmd.cmdloop = cmdloop
# inheritance of the class with the active monkey-patched `cmdloop`
class MyCmd(cmd.Cmd):
pass
一旦您对类方法进行猴子修补(或实现您自己的类),它就会提供正确的行为(尽管没有突出显示和反向跳格,但这些可以根据需要使用其他键实现)。
关于Python readline,使用 Cmd 界面的制表符完成循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25206191/