如何使一个Python,命令行程序自动完成任意事情并非互为preTER [英] How to make a python, command-line program autocomplete arbitrary things NOT interpreter
问题描述
我知道如何安装Python对象自动完成在Python间preTER(在UNIX上)。
I am aware of how to setup autocompletion of python objects in the python interpreter (on unix).
- 谷歌展示了如何做到这一点的解释许多命中。
- 遗憾的是,到这是很难找到什么,我需要做的,这是略有不同的那么多的引用。
我需要知道如何启用,在用Python编写的一个命令行程序的任意项选项卡/自动完成。
I need to know how to enable, tab/auto completion of arbitrary items in a command-line program written in python.
我的具体使用情况是需要发送电子邮件的命令行Python程序。我希望能够自动完成电子邮件地址(我有在磁盘上的地址)时,它的用户类型部分(可选择presses TAB键)。
My specific use case is a command-line python program that needs to send emails. I want to be able to autocomplete email addresses (I have the addresses on disk) when the user types part of it (and optionally presses the TAB key).
我并不需要它在Windows或Mac,linux的只是工作。
I do not need it to work on windows or mac, just linux.
推荐答案
使用Python的的readline
绑定。例如,
Use Python's readline
bindings. For example,
import readline
def completer(text, state):
options = [i for i in commands if i.startswith(text)]
if state < len(options):
return options[state]
else:
return None
readline.parse_and_bind("tab: complete")
readline.set_completer(completer)
官方模块文档没有更详细的,看的 readline的文档以获得更多信息。
The official module docs aren't much more detailed, see the readline docs for more info.
这篇关于如何使一个Python,命令行程序自动完成任意事情并非互为preTER的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!