1

メインリストのサブリストに対してタブ補完を実行しようとしています。

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()

私はこのチュートリアルに従おうとしましたが、運がありませんでした。

前もって感謝します。

4

0 に答える 0