Passing along cmd.Cmd completion to contained class

T

Tim Chase

I have sub-classes of cmd.Cmd in an arrangement somewhat like

class Config(cmd.Cmd):
def do_foo(self, line): print "Fooing %r" % line
def complete_foo(self, text, line, begidx, endidx):
...

class MyTUI(cmd.Cmd):
def __init__(self, configger=Config, *args, **kwargs):
cmd.Cmd.__init__(self, *args, **kwargs)
self.config = configger()
def complete_config(self, text, line, begidx, endidx):
magic_here(self.config, text, line, begidx, endidx)

I've been sparring with getting MyTUI.complete_config() to reach
into self.config for completions so I can type things like

(cmd) config <tab>
(cmd) config foo <tab>

and have it suggest from Config as if I had done something like

def do_config(self, line)
self.config.cmdloop()

and then asked for completions. That would mean that the first
one would act like Config.completenames() and the second one
would act like Config.complete_foo(...)

Is there a best way to get this pass-along behavior?

Thanks,

-tkc
 
M

Miki Tebeka

This is not tested, but maybe it'll work :)

def complete(self, text, state):
if text.startswith('config):
return self.config.complete(text, state)
return Cmd.complete(self, text, state)
 
M

Miki Tebeka

This is not tested, but maybe it'll work :)

def complete(self, text, state):
if text.startswith('config):
return self.config.complete(text, state)
return Cmd.complete(self, text, state)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top