如何使一个python,命令行程序自动完成任意事物NOT解释器 [英] How to make a python, command-line program autocomplete arbitrary things NOT interpreter

查看:108
本文介绍了如何使一个python,命令行程序自动完成任意事物NOT解释器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道如何在python解释器(在unix上)设置python对象的自动完成。

I am aware of how to setup autocompletion of python objects in the python interpreter (on unix).


  • Google显示了很多关于如何执行此操作的点击。

  • 不幸的是,很多引用很难找到我需要做的,这是有点不同。

我需要知道如何启用

我的具体用例是一个需要发送电子邮件的命令行python程序。我想要能够在用户输入部分电子邮件时自动填充电子邮件地址(我有磁盘上的地址)(并可选择按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).

推荐答案

使用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 docs 了解详情。

The official module docs aren't much more detailed, see the readline docs for more info.

这篇关于如何使一个python,命令行程序自动完成任意事物NOT解释器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆