为什么Python3';s cmd.cmd自动完成在Mac OS上不工作?

为什么Python3';s cmd.cmd自动完成在Mac OS上不工作?,python,python-3.x,autocomplete,Python,Python 3.x,Autocomplete,在Mac OS上用Python3.6中的Cmd.Cmd框架测试了一段时间后,我注意到一个问题,我不知道该怎么办。自动完成似乎不起作用。我使用论坛上的一个简单代码进行了测试: import cmd addresses = [ 'here@blubb.com', 'foo@bar.com', 'whatever@wherever.org', ] class MyCmd(cmd.Cmd): def do_send(self, line): pass

在Mac OS上用Python3.6中的Cmd.Cmd框架测试了一段时间后,我注意到一个问题,我不知道该怎么办。自动完成似乎不起作用。我使用论坛上的一个简单代码进行了测试:

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

它似乎不起作用,只是添加了一个空格(普通选项卡)。有什么解决方法吗?

请参见以下python自动完成示例>>

以下是您修改的代码:

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

测试并确保它工作正常。祝你好运

请参见以下python自动完成示例>>

以下是您修改的代码:

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

测试并确保它工作正常。祝你好运

事实上它也不起作用。这可能与我使用mac电脑有关?我设法让它工作,答案是:事实上它也不工作。这可能与我使用mac电脑有关?我在这里找到了答案: