【发布时间】:2016-12-09 21:34:08
【问题描述】:
我正在尝试在 Windows 上为我正在编写的命令行用户界面实现任意自动完成功能。受到that question 的第一个答案的启发,我尝试只运行那里编写的脚本,然后才意识到我在 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
但问题是,当我尝试运行该脚本时,<TAB> 不会导致自动完成。
如何让<TAB> 执行自动完成行为?
最初我虽然搞砸了 pyreadeline 与 readline 相比不同的完成器设置和绑定初始化,但从 pyreadline 文档中的模块代码和示例来看,它们看起来是相同的。
我正在尝试在 Windows 10 中的 2.7 Anaconda Python 发行版上执行它,如果这有任何用处的话。
【问题讨论】:
标签: python windows autocomplete readline