Clock Blog

Bash Completion Problems with option lists generated by PHP

Posted on Thursday, 5 May 2011 @ 00:25 GMT in tech-blogs by Paul Serby

When creating bash completion scripts that get fed from PHP, I realised I was getting the following problem:

I would type the command then press tab to get the completion

$maiden<tab><tab>
But nothing would happen except the cursor jump forward. I would then not be able to backspace over the first part of the command. After some digging I found the problem is because of PHP CLI has libedit compiled in for readline support. This causes any wordlist generated by php to break bash completion. I have only seen this problem on Ubuntu Lucid. It is suggested that you don't get the problem on other flavors of Linux, but I've not had a chance to test that yet. Until someone fixes this problem properly I have prefixed the php script with a blank echo to catch any readline control. With this in place the bash completion works beautifully. Here is the full completion script with the hack:
_maiden()
{
	local cur prev opts
	cur="${COMP_WORDS[COMP_CWORD]}"
	prev="${COMP_WORDS[COMP_CWORD-1]}"
	opts="-h -l -b -v -q"
	if [ -e "Maiden.php" ]
	then
		targets=`echo ""| maiden -b | sort -u`
		COMPREPLY=( $(compgen -W "${targets}" -- ${cur}) )
		return 0
	fi
	COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
	return 0
}
complete -F _maiden maiden
blog comments powered by Disqus