Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > python cmd.Cmd auto complete feature

Reply
Thread Tools

python cmd.Cmd auto complete feature

 
 
Jean-Michel Pichavant
Guest
Posts: n/a
 
      03-08-2011
Hello folks,

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 ?

JM

Here a code sample illutrating the issue, type the linde "sayHello
target=<tab><tab>" to trigger the problem:

import cmd

class SayHello(cmd.Cmd):
def do_sayHello(self, line):
print 'Hello %s !' % line.split('=')[1]

def complete_sayHello(self, text, line, begidx, endidx):
cmds = ['target', 'anotherCmd']
targets = ['Georges', 'Charles']
if text.startswith('target='):
completions = [target for target in ['target=Georges',
'target=Charles'] if target.startswith(text)]
else:
completions = [_cmd for _cmd in cmds if _cmd.startswith(text)]
return completions

def do_EOF(self, line):
return True

if __name__ == '__main__':
SayHello().cmdloop()
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: python cmd.Cmd auto complete feature Jean-Michel Pichavant Python 0 03-09-2011 10:17 AM
Auto Shipping Auto Shipping Scheduling:car moving auto transport linkswanted ASP .Net 0 02-16-2008 02:40 AM
FlashIME2.0, the auto-complete software with python keyword dicitionary support redguardtoo Python 0 07-26-2004 04:50 AM



Advertisments