Python readline,使用Cmd接口循环完成制表符

Edd*_*ett 5 python readline

我正在使用cmd.CmdPython中的类为程序提供简单的readline接口。

自包含的示例:

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()
Run Code Online (Sandbox Code Playgroud)

两次按选项卡将显示可能性。再次按Tab键即可。

我的问题是,如何获得在第三次按Tab键时循环的选项?用readline术语来说,我认为这称为Tab: menu-complete,但是我看不到如何将其应用于Cmd实例。

我已经尝试过:

readline.parse_and_bind('Tab: menu-complete')
Run Code Online (Sandbox Code Playgroud)

实例化解析器实例之前和之后。没运气。

我也尝试传递"Tab: menu-complete"给Cmd构造函数。这里也没有运气。

有人知道它是怎么做的吗?

干杯!

jrd*_*rd1 1

不幸的是,似乎唯一的解决办法就是cmdloop对cmd.Cmd类中的方法进行猴子修补,或者推出您自己的方法。

正确的方法是使用"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
Run Code Online (Sandbox Code Playgroud)

一旦您对类方法进行了猴子修补(或实现了您自己的类),它就会提供正确的行为(尽管没有突出显示和反向制表符,但这些可以根据需要使用其他键来实现)。