Pau*_*den 84 python unix linux command-line autocomplete
我知道如何在python解释器中设置python对象的自动完成(在unix上).
我需要知道如何在用python编写的命令行程序中启用,选项卡/自动完成任意项.
我的具体用例是一个需要发送电子邮件的命令行python程序.我希望能够在用户键入部分电子邮件地址(我在磁盘上有地址)时自动填充电子邮件地址(并且可以选择按TAB键).
我不需要它在Windows或Mac上工作,只需要linux.
Flo*_*sch 59
按照cmd文档,你会没事的
import cmd
addresses = [
'here@blubb.com',
'foo@bar.com',
'whatever@wherever.org',
]
class MyCmd(cmd.Cmd):
def do_send(self, line):
pass
def complete_send(self, text, line, start_index, end_index):
if text:
return [
address for address in addresses
if address.startswith(text)
]
else:
return addresses
if __name__ == '__main__':
my_cmd = MyCmd()
my_cmd.cmdloop()
Run Code Online (Sandbox Code Playgroud)
输出选项卡 - >选项卡 - >发送 - >选项卡 - >选项卡 - > f - >选项卡
(Cmd)
help send
(Cmd) send
foo@bar.com here@blubb.com whatever@wherever.org
(Cmd) send foo@bar.com
(Cmd)
Run Code Online (Sandbox Code Playgroud)
eph*_*ent 58
使用Python的readline
绑定.例如,
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)
Run Code Online (Sandbox Code Playgroud)
官方模块文档没有更详细,请参阅readline文档以获取更多信息.
小智 34
既然你在问题中说"NOT interpreter",我猜你不想要涉及python readline等的答案.(编辑:事后看来,情况显然不是这样.哼哼.我觉得这个信息很有意思,所以我会留在这里.)
我想你可能会在此之后.
它是关于向任意命令添加shell级别的完成,扩展bash自己的tab-completion.
简而言之,您将创建一个包含shell函数的文件,该函数将生成可能的完成,将其保存到/etc/bash_completion.d/
命令中并将其注册complete
.以下是链接页面的摘录:
_foo()
{
local cur prev opts
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
prev="${COMP_WORDS[COMP_CWORD-1]}"
opts="--help --verbose --version"
if [[ ${cur} == -* ]] ; then
COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
return 0
fi
}
complete -F _foo foo
Run Code Online (Sandbox Code Playgroud)
在这种情况下,打字foo --[TAB]
会给你的价值观在变opts
,即--help
,--verbose
和--version
.出于您的目的,您基本上希望自定义放入的值opts
.
看看链接页面上的示例,这一切都非常简单.
qed*_*qed 23
我很惊讶没有人提到argcomplete,这是一个来自文档的例子:
from argcomplete.completers import ChoicesCompleter
parser.add_argument("--protocol", choices=('http', 'https', 'ssh', 'rsync', 'wss'))
parser.add_argument("--proto").completer=ChoicesCompleter(('http', 'https', 'ssh', 'rsync', 'wss'))
Run Code Online (Sandbox Code Playgroud)
Pau*_*den 12
下面是我的话非常ephemient提供的代码的完整工作版本在这里(谢谢).
import readline
addrs = ['angela@domain.com', 'michael@domain.com', 'david@test.com']
def completer(text, state):
options = [x for x in addrs if x.startswith(text)]
try:
return options[state]
except IndexError:
return None
readline.set_completer(completer)
readline.parse_and_bind("tab: complete")
while 1:
a = raw_input("> ")
print "You entered", a
Run Code Online (Sandbox Code Playgroud)
use*_*047 10
# ~/.pythonrc
import rlcompleter, readline
readline.parse_and_bind('tab:complete')
# ~/.bashrc
export PYTHONSTARTUP=~/.pythonrc
Run Code Online (Sandbox Code Playgroud)
您可以尝试使用Python Prompt Toolkit,这是一个用于在 Python 中构建交互式命令行应用程序的库。
该库可以轻松添加交互式自动完成功能,允许用户使用Tab键在可用选项之间进行可视化循环。该库是跨平台的(Linux、OS X、FreeBSD、OpenBSD、Windows)。例子:
(图片来源:pcgli)