newbie Q on sdtin word completion

Bernd Fischer "bernd.fischer\" at xignal-A%&HY%$v#&
Wed Dec 7 19:28:51 CET 2005


I'm on a Linux env and try to get
word completion form sdtin done,
like Perl's
  $stdin = Complete( "\t: ", @choices );

What I have so far shows me the directory listing
on the second hit on TAB and not the list of
choices on the first like I wanted to have.

Any help appreciated,

#! /usr/bin/python

import readline
import string
import readline

## completer function

def comp( text, state ):
	global compList
         matches = [ ]
	text = readline.get_line_buffer( )
         n = len( text )
         for word in compList:
                 if word[ :n ] == text:
                     matches.append( word )
         return matches
## chosser function

def chooser( inList, msgString ):
	stdin = None
	compList = inList

	while stdin not in inList:
		print "\nSelect a %s <>:\n" % msgString
		for elem in inList:
			print "\t", elem
		print "" # equivalent to newline /n
		stdin = string.rstrip( raw_input( "\t: " ) )
		if stdin not in inList:
			print "\nWrong %s, select again!" % msgString
	return sdtin 	
readline.parse_and_bind( "tab: complete" )
readline.set_completer( comp )
choices = [ "appel", "pear" ]

chooser( choices , "fruit" )

More information about the Python-list mailing list