i know i can do this to get the effect of tab completion in python sure.
import readline
COMMANDS = ['extra', 'extension', 'stuff', 'errors',
'email', 'foobar', 'foo']
def complete(text, state):
for cmd in COMMANDS:
if cmd.startswith(text):
if not state:
return cmd
else:
state -= 1
readline.parse_and_bind("tab: complete")
readline.set_completer(complete)
raw_input('Enter section name: ')
I am now interested in doing tab completion with directories. (/home/user/doc >tab)
How would i go about doing such a task?
Here is a quick example of how to perform incremental completion of file system paths. I’ve modified your example, organizing it into a class where methods named
complete_[name]indicate top-level commands.I’ve switched the completion function to use the internal readline buffer to determine the state of the overall completion, which makes the state logic a bit simpler. The path completion is in the
_complete_path(path)method, and I’ve hooked up the extra command to perform path completions on its arguments.I’m sure the code could be further simplified but it should provide you a decent starting point:
Usage:
Update It will complete paths from the root if the user types
/: