为什么Python3的cmd.Cmd自动完成功能无法在Mac OS上运行?

时间:2017-08-18 13:16:31

标签: python python-3.x autocomplete

在Mac OS上使用python 3.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()

它似乎不起作用,它只是添加一个空格(普通标签)。任何workaroud?

1 个答案:

答案 0 :(得分:0)

请参阅以下示例>>> https://pymotw.com/2/cmd/用于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()

测试并发现它有效。祝你好运

相关问题