python cmd completedefault() method not called

2019-07-03 02:06发布

Consider the following Python 2.7 script:

#!/usr/bin/python

import cmd

class T(cmd.Cmd):
    def completedefault(self, *a):
        print 'completedefault called'
        return []

t=T()
t.cmdloop()

When I expect:

I type a character into the shell, then hit tab, I expect to see "completedfault called" printed.

What actually happens:

I type a character into the shell, then hit tab, and nothing happens.

Tested with Python 2.7.3.

1条回答
我欲成王,谁敢阻挡
2楼-- · 2019-07-03 02:58

completedefault is called to complete a input line after you entered a command for which no complete_<commandname>-method is available.

Try this:

#!/usr/bin/python

import cmd

class T(cmd.Cmd):
    def completedefault(self, *a):
        print 'completedefault called'
        return []

    def test(self, *args):
        print "test args: ", args

t=T()
t.cmdloop()

now enter test [space] and press tab, completedefault should be executed now.

If you want to control completion for command names, you can use completenames to do so, not completedefault.

查看更多
登录 后发表回答