Python中原始输入()中子命令的制表符完成
我试图在主列表的子列表上完成制表符 差不多Python中原始输入()中子命令的制表符完成,python,autocomplete,raw-input,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> 我试着遵循这一点,但没有运气 提前感谢。您查看了cm
commands = ['help', 'set', 'info']
sub_command = ['module', 'level']
modules = ['pr', 'tls', 'tld']
levels = ['high', 'mid', 'low']
有没有办法做到这一点:
>>> se<tab> mo<tab> tl<tab>
我试着遵循这一点,但没有运气
提前感谢。您查看了
cmd
模块了吗cmd.cmd.completedefault
看起来像您想要的。。。
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()