Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tab completion with Python's Cmd.cmd

After testing a while with the Cmd.cmd framework in python, I noticed a problem I don't know what to do about. Plus I believe to have this working some hours before (or I'm just crazy), so this is even more weird.

I have the following example code, tested on both Windows and Linux systems (so it's not a Windows problem), but tab completion simply doesn't work.

If I use the exact same code in Python 2 it does work on the Linux system (not on the Windows one though)

import cmd
class Shell ( cmd.Cmd ):    
    def do_test ( self, params ):
        print( 'test: ' + params )

    def do_exit ( self, params ):
        return True

    def do_quit ( self, params ):
        return True

if __name__ == '__main__':
    x = Shell()
    x.cmdloop()

Do you know why this happens, or what I can do, to make tab completion possible?

like image 731
poke Avatar asked Dec 21 '09 23:12

poke


People also ask

How do I autocomplete in CMD?

Using autocomplete is as simple as pressing the [TAB] and the active command line options will fill-in. If more than one option is available, you can hit [TAB] twice to display all possible choices and continue typing until there is only one matching choice left.

What is tab completion in Python?

Tab Completion and History Editing. Completion of variable and module names is automatically enabled at interpreter startup so that the Tab key invokes the completion function; it looks at Python statement names, the current local variables, and the available module names.

How does command line completion work?

Command-line completion allows the user to type the first few characters of a command, program, or filename, and press a completion key (normally Tab ↹ ) to fill in the rest of the item. The user then presses Return or ↵ Enter to run the command or open the file.

What does Python do in CMD?

Simply put, it's a way to manage a program script from outside by typing the script name and the input arguments into the command line prompt when trying to execute that particular script. In Python, command line arguments can be used to: Adjust the operation of a certain program. Define a source of information.


2 Answers

It actually works for me on Linux on both Python 2 and 3. However, my python setup was compiled with readline support, which is required for it to be automatic per the cmd documentation. I suspect your Linux Python 3 wasn't compiled with it.

Unfortunately, readline is Unix-specific. See python tab completion in windows for a discussion of other options on Windows.

like image 159
Arthur Shipkowski Avatar answered Nov 08 '22 20:11

Arthur Shipkowski


I got it to work on windows after I installed the pyreadline module from here https://pypi.python.org/pypi/pyreadline/2.0

like image 20
hucqym Avatar answered Nov 08 '22 20:11

hucqym