2017-08-18 5 views
0

Nach einer Weile mit dem Cmd.cmd Framework in Python 3.6 auf Mac OS, habe ich ein Problem festgestellt, ich weiß nicht, was ich tun soll. Autocomplete scheint nicht zu funktionieren. Getestet habe ich mit einem einfachen Code auf einem Forum gefunden:Warum funktioniert Python3s Cmd.Cmd-Autocomplete nicht unter Mac OS?

import cmd 

addresses = [ 
    '[email protected]', 
    '[email protected]', 
    '[email protected]', 
] 

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() 

Es ist nicht zu funktionieren scheint, es fügt nur ein Leerzeichen (normal tab). Irgendeine Workaround?

Antwort

0

Bitte beachten Sie die folgenden Beispiele >>>https://pymotw.com/2/cmd/ für Python Autovervollständigung.

Im Folgenden finden Sie die geänderte Code:

import cmd 

class MyCmd(cmd.Cmd): 


addresses = [ 
'[email protected]', 
'[email protected]', 
'[email protected]'] 

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 = '[email protected]' 
    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 und sehen, dass es funktioniert. Viel Glück

+0

In der Tat funktioniert es auch nicht. Es könnte etwas mit der Tatsache sein, dass ich auf einem Mac bin? – Blaxou