Windows 上的 Pyreadline 任意自动完成

chi*_*ffa 8 python windows autocomplete readline

我正在尝试在 Windows 上为我正在编写的命令行用户界面实现任意自动完成。第一答案启发这个问题,我想只是运行写有脚本,意识到我是在Windows和使用需要之前pyreadline代替readline。经过一些试验,我最终得到了下面的脚本,它基本上是一个复制粘贴,但带有 pyreader 初始化:

from pyreadline import Readline

readline = Readline()

class MyCompleter(object):  # Custom completer

    def __init__(self, options):
        self.options = sorted(options)

    def complete(self, text, state):
        if state == 0:  # on first trigger, build possible matches
            if text:  # cache matches (entries that start with entered text)
                self.matches = [s for s in self.options
                                    if s and s.startswith(text)]
            else:  # no text entered, all matches possible
                self.matches = self.options[:]

        # return match indexed by state
        try:

            return self.matches[state]
        except IndexError:
            return None

completer = MyCompleter(["hello", "hi", "how are you", "goodbye", "great"])
readline.set_completer(completer.complete)
readline.parse_and_bind('tab: complete')

input = raw_input("Input: ")
print "You entered", input
Run Code Online (Sandbox Code Playgroud)

然而,问题是当我尝试运行该脚本时,<TAB>不会导致自动完成。

如何<TAB>执行自动完成行为?

最初,我虽然搞砸了与pyreadeline相比不同的完成设置和绑定初始化readline,但从模块代码和pyreadline文档中的示例来看,它们看起来是相同的。

我正在尝试在 Windows 10 中的 2.7 Anaconda Python 发行版上执行它,如果这有任何用处。

小智 1

面对同样的问题(即使pyreadline完成在 Windows 上也不起作用),我偶然发现了一篇介绍一些 Python 库的博客文章。阅读完后,我开始使用Python Prompt Toolkit将补全功能添加到我的脚本中。