使用readline自动完成选项卡之后,Python2.7选项卡自动完成

时间:2018-07-12 00:33:00

标签: python-2.7 tab-completion

我有以下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)

谢谢!

0 个答案:

没有答案