1

After testing a while with the Cmd.cmd framework in python 3.6 on Mac OS, I noticed a problem I don't know what to do about. Autocomplete doesn't seem to work. I tested with a simple code found on a forum :

import cmd

addresses = [
    'here@blubb.com',
    'foo@bar.com',
    'whatever@wherever.org',
]

class MyCmd(cmd.Cmd):
    def do_send(self, line):
        pass

    def complete_send(self, text, line, start_index, end_index):
        if text:
            return [
                address for address in addresses
                if address.startswith(text)
            ]
        else:
            return addresses


if __name__ == '__main__':
    my_cmd = MyCmd()
    my_cmd.cmdloop()

It doesn't seem to work, it just adds a blank space (normal tab). Any workaroud ?

Blax
  • 500
  • 3
  • 7
  • 18

1 Answers1

0

please see the following examples >>> https://pymotw.com/2/cmd/ for python autocompletion.

Below is your modified code:

import cmd

class MyCmd(cmd.Cmd):


addresses = [
'here@blubb.com',
'foo@bar.com',
'whatever@wherever.org']

def do_send(self, line):
    "Greet the person"
    if line and line in self.addresses:
        sending_to = 'Sending to, %s!' % line
    elif line:
        sending_to = "Send to, " + line + " ?"
    else:
        sending_to = 'noreply@example.com'
    print (sending_to)

def complete_send(self, text, line, begidx, endidx):
    if text:
        completions = [
            address for address in self.addresses
            if address.startswith(text)
        ]
    else:
        completions = self.addresses[:]

    return completions

def do_EOF(self, line):
    return True

if __name__ == '__main__':
    MyCmd().cmdloop()

Test and see that it works. Good luck

aspo
  • 374
  • 2
  • 9
  • In fact it doesn't work either. It might be something with the fact that I'm on a mac ? – Blax Aug 25 '17 at 09:20
  • 1
    I managed to make it work with the answer here: https://stackoverflow.com/questions/7116038/python-tab-completion-mac-osx-10-7-lion – Stefano Bragaglia Sep 03 '18 at 16:49