我有以下Python2.7代码:
import readline
commandList = [ "help", "quit", "print", "ping", "curl" ]
IP = [ "127.0.0.1", "10.0.0.1", "10.0.0.2" ]
def tabComplete(text, state):
for command in commandList:
if command.startswith(text):
if not state:
return command + " "
else:
state -= 1
readline.parse_and_bind("tab: complete")
readline.set_completer(tabComplete)
command = raw_input("> ")
这只是自动完成raw_input("> ")
部分。
我想知道是否存在一种方法,可以在像IP[]
这样的特定命令之后,使用第4行的commandList[]
列表而不是ping
列表来自动完成它和curl
。
例如:
> h<TAB>
> help
> p<TAB>
print ping
> pin<TAB> <TAB>
127.0.0.1 10.0.0.1 10.0.0.2
>curl 10<TAB>
10.0.0.1 10.0.0.2
> print <TAB>
(shows nothing)
谢谢!