繁体   English   中英

为什么Python3的cmd.Cmd自动完成功能在Mac OS上不起作用?

[英]Why is Python3's cmd.Cmd autocomplete not working on Mac OS?

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

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

请参阅以下示例>>> 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()

测试并查看它是否有效。 祝好运

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM