2013-04-18 39 views
0

完成功能的工作,我已经签了几个例子,包括怎样的readline

http://pymotw.com/2/readline/

class SimpleCompleter(object): 

    def __init__(self, options): 
     self.options = sorted(options) 
     return 

    def complete(self, text, state): 
     response = None 
     if state == 0: 
      # This is the first time for this text, so build a match list. 
      if text: 
       self.matches = [s 
           for s in self.options 
           if s and s.startswith(text)] 
       logging.debug('%s matches: %s', repr(text), self.matches) 
      else: 
       self.matches = self.options[:] 
       logging.debug('(empty input) matches: %s', self.matches) 

     # Return the state'th item from the match list, 
     # if we have that many. 
     try: 
      response = self.matches[state] 
     except IndexError: 
      response = None 
     logging.debug('complete(%s, %s) => %s', 
         repr(text), state, repr(response)) 
     return response 

我想不通为什么状态递增,什么是它的目的。 它会工作的优良状态= 0和匹配的单词一个元素的列表,但是当国家增加我会引发错误(IndexError)

感谢

回答

0

状态可能匹配的数量,你用它来返回多个结果。 即当state == 1返回“cmd1”时,当state == 2返回“command2”时,当state == 3返回无通过2匹配结果。