Python中raw_input()中子命令的制表符完成

时间:2012-09-07 13:50:52

标签: python autocomplete raw-input

我试图在主列表的子列表上完成制表符

之类的东西
commands = ['help', 'set', 'info']
sub_command = ['module', 'level'] 
modules = ['pr', 'tls', 'tld']
levels = ['high', 'mid', 'low']

任何想法如何做这样的事情:

>>> se<tab> mo<tab> tl<tab>

这是主要列表的代码:

from core.libs.interpreter import interpreter

import re
try:
    import readline
except ImportError:
    print '\n[!] The "readline" module is required to provide elaborate line editing and history features'
else:
    pass

COMMANDS = interpreter.commands

RE_SPACE = re.compile('.*\s+$', re.M)


class Completer(object):
    '''
    internal readline buffer to determine the state of the overall completion,
    which makes the state logic a bit simpler
        '''
    def complete(self, text, state):
        "Generic readline completion entry point."
        buffer = readline.get_line_buffer()
        line = readline.get_line_buffer().split()
        # show all commands
        if not line:
            return [c + ' ' for c in COMMANDS][state]
        # account for last argument ending in a space
        if RE_SPACE.match(buffer):
            line.append('')
        # resolve command to the implementation function
        cmd = line[0].strip()
        if cmd in COMMANDS:
            #impl = getattr(self, 'complete_%s' % cmd)
            args = line[1:]
            if args:
                return (args + [None])[state]
            return [cmd + ' '][state]
        results = [c + ' ' for c in COMMANDS if c.startswith(cmd)] + [None]
        return results[state]

    def tab(self):
        # to work with non nix systems
        try:
            readline.set_completer_delims(' \t\n;')
            readline.parse_and_bind("tab: complete")
            readline.set_completer(self.complete)
        except:
            pass

complete = Completer()

我试图遵循这个tutorial,但没有运气。

提前致谢。

0 个答案:

没有答案