bash-style auto completion in command line program. use rlcomplete?

member thudfoo thudfoo at opensuse.us
Sat May 22 15:57:43 EDT 2010


On Sat, May 22, 2010 at 11:01 AM, ntwrkd <ntwrkd at gmail.com> wrote:
> I am trying to create a bash-style auto completion in a simple
> command-line and script-based program i created using cmd.
> I saw http://docs.python.org/library/rlcompleter.html, which I'm
> thinking is the way to go for my program to intercept the tab key.
> Would anyone have thoughts on this?
>
> I wish cmd or cmd2 had this functionality built-in.
>

It is built in. Check the cmd documentation for complete_* method of Cmd class.

Following is an example of it use for a command called "open":

    def complete_open(self, text, line, begidx, endidx):
        asmfiles = glob('%s/*.asm' % ASMDIR)
        matches = []
        for path in asmfiles:
            fname = os.path.basename(path)
            if fname.startswith(text):
                matches.append(fname)
        return matches



More information about the Python-list mailing list