Python readline, cyclic tab stop with Cmd interface

I use a class cmd.Cmdin Python to offer a simple readline interface for my program.

Do-it-yourself example:

from cmd import Cmd

class CommandParser(Cmd):

    def do_x(self, line):
        pass

    def do_xy(self, line):
        pass

    def do_xyz(self, line):
        pass

if __name__ == "__main__":
    parser = CommandParser()
    parser.cmdloop()

Double-click the tab, showing the features. Pressing the tab again does the same.

My question is: how do I get options for cycling to the third tab? In terms of readline, I think it's called Tab: menu-complete, but I don't see how to apply this to an instance Cmd.

I already tried:

readline.parse_and_bind('Tab: menu-complete')

Both before and after the instance of the parser instance. Bad luck.

I also tried passing "Tab: menu-complete"to the constructor Cmd. There is no luck.

Does anyone know how to do this?

Hooray!

+4
2

- menu-complete:

parser = CommandParser(completekey="tab: menu-complete ")

readline.parse_and_bind(self.completekey+": complete")

readline.parse_and_bind("tab: menu-complete : complete")

, , , tab: menu-complete.

readline ( ), str, :

class stubborn_str(str):
    def __add__(self, other):
        return self

parser = CommandParser(completekey=stubborn_str("tab: menu-complete"))

self.completekey+": complete" self.completekey.

+1

, , - cmdloop cmd.Cmd .

"Tab: menu-complete", , 115: readline.parse_and_bind(self.completekey+": complete"), . ( 115 cmd . : https://hg.python.org/cpython/file/2.7/Lib/cmd.py). :

import cmd


# note: taken from Python library: https://hg.python.org/cpython/file/2.7/Lib/cmd.py
def cmdloop(self, intro=None):
    """Repeatedly issue a prompt, accept input, parse an initial prefix
    off the received input, and dispatch to action methods, passing them
    the remainder of the line as argument.
    """

    self.preloop()
    if self.use_rawinput and self.completekey:
        try:
            import readline
            self.old_completer = readline.get_completer()
            readline.set_completer(self.complete)
            readline.parse_and_bind(self.completekey+": menu-complete")  # <---
        except ImportError:
            pass
    try:
        if intro is not None:
            self.intro = intro
        if self.intro:
            self.stdout.write(str(self.intro)+"\n")
        stop = None
        while not stop:
            if self.cmdqueue:
                line = self.cmdqueue.pop(0)
            else:
                if self.use_rawinput:
                    try:
                        line = raw_input(self.prompt)
                    except EOFError:
                        line = 'EOF'
                else:
                    self.stdout.write(self.prompt)
                    self.stdout.flush()
                    line = self.stdin.readline()
                    if not len(line):
                        line = 'EOF'
                    else:
                        line = line.rstrip('\r\n')
            line = self.precmd(line)
            stop = self.onecmd(line)
            stop = self.postcmd(stop, line)
        self.postloop()
    finally:
        if self.use_rawinput and self.completekey:
            try:
                import readline
                readline.set_completer(self.old_completer)
            except ImportError:
                pass

# monkey-patch - make sure this is done before any sort of inheritance is used!
cmd.Cmd.cmdloop = cmdloop

# inheritance of the class with the active monkey-patched `cmdloop`
class MyCmd(cmd.Cmd):
    pass

, ( ), ( , ).

0

All Articles