cmd, readline, and /path/filename completion

Dave Cinege dcinege at psychosis.com
Thu Jul 25 05:44:41 EDT 2002


On Thursday 25 July 2002 0:40, Dave Cinege wrote:

> I'm by no means a coding novice, and I already know I (or somewhere in
> readline) must walk the path and gather a list of executable files. Where
> I'm lost is how to pass this to readline. (Or activate it in readline)

Just needed to do more homework. The thing to do in my situation
is to override the 'completenames' method of cmd. The method below
completes all executeables in the path just like bash.

What confused the hell out of me is I couldn't get 'completedefault'
to do anything. Viewing the cmd modules showed that is only
run when the completer index is > 0. Thus, 'commands' (the beginning of
a line) is run against completenames... 

def completenames(self, text, line, begidx, endidx):
	l = cmd.Cmd.completenames(self, text, line, begidx, endidx)
	path = os.environ['PATH'].split(':')
	textlen = len(text)
	for dir in path:
		try:
			files = os.listdir(dir)
		except:
			continue
		for file in files:
			if textlen > 0 and file[:textlen] != text:
				continue
			if os.access(dir+'/'+file, os.X_OK):
				l += [file]
	return l



-- 
The time is now 22:48 (Totalitarian)  -  http://www.ccops.org/





More information about the Python-list mailing list