Passing along cmd.Cmd completion to contained class

Discussion in 'Python' started by Tim Chase, Dec 2, 2011.

  1. Tim Chase

    Tim Chase Guest

    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
     
    Tim Chase, Dec 2, 2011
    #1
    1. Advertising

  2. Tim Chase

    Miki Tebeka Guest

    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)
     
    Miki Tebeka, Dec 2, 2011
    #2
    1. Advertising

  3. Tim Chase

    Miki Tebeka Guest

    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)
     
    Miki Tebeka, Dec 2, 2011
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    0
    Views:
    546
  2. Achim Domma (Procoders)

    read input for cmd.Cmd from file

    Achim Domma (Procoders), Jun 3, 2005, in forum: Python
    Replies:
    2
    Views:
    8,074
    Peter Otten
    Jun 3, 2005
  3. Sarir Khamsi

    Interpreter-like help in cmd.Cmd

    Sarir Khamsi, Jun 9, 2005, in forum: Python
    Replies:
    4
    Views:
    377
    Bengt Richter
    Jun 26, 2005
  4. Alex Maghen

    Passing "Style" along to constituent controls

    Alex Maghen, May 30, 2006, in forum: ASP .Net Web Controls
    Replies:
    9
    Views:
    222
    Walter Wang [MSFT]
    Jun 1, 2006
  5. ziggs
    Replies:
    2
    Views:
    188
    Henry Robinson
    Apr 21, 2004
Loading...

Share This Page