【问题标题】:how the completion function work in readline完成功能如何在 readline 中工作
【发布时间】:2013-09-10 02:38:14
【问题描述】:

我检查了一些例子,包括

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

我不明白为什么状态会增加,它的目的是什么。 它适用于 state=0 和匹配单词的一个元素列表,但是当状态增加时我会引发错误(IndexError)

谢谢

【问题讨论】:

    标签: python autocomplete readline


    【解决方案1】:

    state 是可能匹配的数量,你用它来返回多个结果。 即当state == 1返回“cmd1”时,当state == 2返回“command2”时,当state == 3返回None时传递2匹配结果。

    【讨论】:

      猜你喜欢
      • 2016-01-07
      • 2017-10-09
      • 1970-01-01
      • 1970-01-01
      • 2021-06-22
      • 2015-01-09
      • 1970-01-01
      • 1970-01-01
      • 2020-10-12
      相关资源
      最近更新 更多