【问题标题】:enable auto-completion in Python input prompt在 Python 输入提示中启用自动完成
【发布时间】:2018-09-03 08:34:51
【问题描述】:

有没有办法使用模块cmd 或readline 在读取仅匹配特定值集的输入字符串时获得智能补全?

例如:

read_from_list(prompt"Name:", allowed_values=["Adam", "Bert", "Cesar"])

将创建提示

Name:

如果我进入

Ad

然后按完成(通常通过 Tab 键)我得到

Adam

作为奖励,除了Adam、Bert 或Cesar 以及它们的所有前缀之外,甚至不能输入字符串。

更新:本质上我想要 Emacs 的completing-read 的 Python 3 变体:https://www.gnu.org/software/emacs/manual/html_node/elisp/Minibuffer-Completion.html

【问题讨论】:

标签: python input autocomplete


【解决方案1】:

安装库:

pip install Inline-input

代码:

import inline #Import inline module
input = inline.input #replace default input

inline.commands = ["Adam", "Bert", "Cesar"]

answer = input(prefix="Name: ", free=False) # The Free=False parameter prohibits sending other commands not from the list

或者:

import inline #Import inline module
input = inline.input #replace default input
    
allowed_values = ["Adam", "Bert", "Cesar"]
    
answer = input(prefix="Name: ", free=False, command=allowed_values)

你还可以做很多其他事情:

# Examples with new input:
inp = input(prefix=None) # default ">> "
inp = input(free=False) # or free=True, default is True
inp = input(minLength=2, maxLength=10) # default minLength=0, maxLength=0

inline.autoCompleteOnEnter = True # Autocomplete on Enter and Tab, default False - only on Tab

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-01-25
    • 2019-07-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-23
    • 1970-01-01
    相关资源
    最近更新 更多