如何让python自动补全显示匹配?
我有一个带有自动完成功能的完成类。简单版本:
class Completer:
def __init__(self):
self.words = ["mkdir","mktbl", "help"]
self.prefix = None
def complete(self, prefix, index):
if prefix != self.prefix:
self.matching_words = [w for w in self.words if w.startswith(prefix)]
self.prefix = prefix
else:
pass
try:
return self.matching_words[index]
except IndexError:
return None
并执行类似这样的操作以使用 readline 获得自动完成:
import readline
readline.parse_and_bind("tab: complete")
completer = Completer()
readline.set_completer(completer.complete)
user_input =raw_input("> ")
因此,自动完成有 3 个单词 ["help", "mkdir"," mktbl"] 在示例中。
如果用户执行:
<代码>>他
用户得到:
<代码>> help
但如果用户执行
<代码>> mk
没有发生任何事情,因为没有单个匹配项(mkdir 和 mktbl)
如果存在多个匹配项,如何显示选项?就像 Bash 那样自动完成文件名吗?
因此用户会得到类似的结果:
<代码>> mkmktbl mkdir
<代码>> mk
PS 我尝试过
_readline.insert_text(...)_
和
打印...
进入完成函数,但它会阻止插入,因此用户会得到如下所示的内容:
<代码>> mk
<代码>> mkmktbl mkdir
PPS 我需要一个 Linux 解决方案。
I have kind of a completer class with an autocompletion function. Simple version:
class Completer:
def __init__(self):
self.words = ["mkdir","mktbl", "help"]
self.prefix = None
def complete(self, prefix, index):
if prefix != self.prefix:
self.matching_words = [w for w in self.words if w.startswith(prefix)]
self.prefix = prefix
else:
pass
try:
return self.matching_words[index]
except IndexError:
return None
And execute something like this to get auto-completion with readline:
import readline
readline.parse_and_bind("tab: complete")
completer = Completer()
readline.set_completer(completer.complete)
user_input =raw_input("> ")
So, there are 3 words for auto-completion ["help", "mkdir","mktbl"] in the example.
if a user executes:> he<tab>
the user gets:> help
but if the user executes> mk<tab>
nothing is happening because there are not a single match (mkdir and mktbl)
How to display options in case there are several matches? Like the Bash do with a file names autocompletion?
Thus user whold get something like:> mk<tab>
mktbl mkdir
> mk<cursor>
P.S. I have tried to put
_readline.insert_text(...)_
and
print ...
into completer function but it brakes the insertion, so a user gets something like this:> mk<tab>
> mkmktbl mkdir <cursor>
P.P.S I need a linux solution.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
请设置 readline 选项
如果您希望在第一个
之后完成, 。否则只需点击
两次即可。参考:http://caliban.org/bash/,阅读行提示和技巧部分 >
PS。在 OS X 和 Linux 上测试了你的代码,它运行良好(在我的机器上;)
Set the readline option
if you want completions after the first
<tab>
. Otherwise just hit<tab>
twice.Reference: http://caliban.org/bash/, Section readline Tips and Tricks
PS. Tested your code on OS X and Linux, it works well (on my machines ;)
有人建议我一个解决方案来完成答案。它允许组织自动完成选项的完成输出。
对于linux readline 有函数
readline.set_completion_display_matches_hook
http://docs.python.org/library/readline。 html?highlight=readline#readline.set_completion_display_matches_hook
因此,对于上面列出的示例,
此代码将产生:
<代码>> mk
> mk
对于 Windows readline,堆栈溢出有一个答案:
如何让 IPython 通过以下方式组织选项卡完成可能性类?
不知道它在 mac 上如何工作。
I was suggested a solution that complete the answer. It allows to organize completion output of autocompletion options.
For linux readline there are function
readline.set_completion_display_matches_hook
http://docs.python.org/library/readline.html?highlight=readline#readline.set_completion_display_matches_hook
So, for the example listed above this code
this will produse:
> mk<tab>
> mk<cursor>
For windows readline there is an answer at stack overflow:
How do I make IPython organize tab completion possibilities by class?
Don't know how it works for mac.