【问题标题】:Why is Python3's cmd.Cmd autocomplete not working on Mac OS?为什么 Python3 的 cmd.Cmd 自动完成功能在 Mac OS 上不起作用?
【发布时间】:2018-01-27 04:46:27
【问题描述】:

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

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

【问题讨论】:

    标签: python python-3.x autocomplete


    【解决方案1】:

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

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

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-29
    • 2023-03-15
    • 1970-01-01
    • 2011-06-01
    • 1970-01-01
    相关资源
    最近更新 更多