Re: python cmd.Cmd auto complete feature

Discussion in 'Python' started by Jean-Michel Pichavant, Mar 9, 2011.

  1. Peter Otten wrote:
    > Jean-Michel Pichavant wrote:
    >
    >
    >> I'm trying to autoexpand values as well as arguments using the builtin
    >> cmd.Cmd class.
    >>
    >> I.E.
    >> Consider the following command and arguments:
    >>
    >> > sayHello target=Georges

    >> 'Hello Georges !'
    >>
    >> I can easily make 'tar' expand into 'target=' however I'd like to be
    >> able to expand the value as well, choosing the target within a
    >> predefined list. ie.
    >> > sayHello target=<tab>

    >> target=Georges target=Charles
    >>
    >> However I have the feeling that cmd.Cmd consider the '=' character in
    >> the way it will not try to expand anything beyond. When double tabbing
    >> after the '=' it will print the list of available arguemnt (i.e
    >> ['target'] in the exemple above).
    >> Ddd anyone successfuly expand values with cmd.Cmd ?
    >>

    >
    > Some digging shows that your feeling is right:
    >
    > http://docs.python.org/library/readline.html#readline.get_completer_delims
    >
    >
    >>>> import readline
    >>>> readline.get_completer_delims()
    >>>>

    > ' \t\n`~!@#$%^&*()-=+[{]}\\|;:\'",<>/?'
    >
    > After some initial problems with an extra space the following seems to work:
    >
    > import cmd
    > import readline
    >
    > class SayHello(cmd.Cmd):
    > def __init__(self):
    > cmd.Cmd.__init__(self)
    > delims = readline.get_completer_delims().replace("=", "")
    > readline.set_completer_delims(delims)
    >
    > def do_sayHello(self, line):
    > print 'Hello %s !' % line.split('=')[1]
    >
    > def complete_sayHello(self, text, line, begidx, endidx):
    > target_with_value = ["target=" + v for v in "Charles
    > Georges".split()]
    > commands = ["target", "tarpit", "anotherCmd"]
    >
    > if text.startswith("target="):
    > return [c for c in target_with_value if c.startswith(text)]
    > completions = [c for c in commands if c.startswith(text)]
    > if completions == ["target"]: # avoid blank after target
    > return target_with_value
    > return completions
    >
    > def do_EOF(self, line):
    > return True
    >
    > if __name__ == '__main__':
    > SayHello().cmdloop()
    >
    >
    >

    Great ! Simple and effective. I didn't realize in the first place that
    the completion was handled by readline.

    JM
    Jean-Michel Pichavant, Mar 9, 2011
    #1
    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. Achim Domma (Procoders)

    read input for cmd.Cmd from file

    Achim Domma (Procoders), Jun 3, 2005, in forum: Python
    Replies:
    2
    Views:
    8,057
    Peter Otten
    Jun 3, 2005
  2. linkswanted
    Replies:
    1
    Views:
    897
  3. Soumen banerjee

    IDE for python 2.6 (auto complete)

    Soumen banerjee, May 4, 2009, in forum: Python
    Replies:
    2
    Views:
    696
    Detlev Offenbach
    May 4, 2009
  4. Arne Vajhøj
    Replies:
    4
    Views:
    274
    Arne Vajhøj
    Mar 1, 2011
  5. Jean-Michel Pichavant

    python cmd.Cmd auto complete feature

    Jean-Michel Pichavant, Mar 8, 2011, in forum: Python
    Replies:
    0
    Views:
    309
    Jean-Michel Pichavant
    Mar 8, 2011
Loading...

Share This Page