[FYI] generated completions

From: Johannes Schindelin <Johannes.Schindelin@gmx.de>
Date: 2006-04-16 12:05:26
Hi,

I just played around with command line completion in bash, mainly to 
distract me from the work I have to do.

This is a shell script which generates a completion script from the 
documentation. It is very dumb at the moment, but at least a start. I am 
not likely to continue with this in the near future, so everyone who's 
interested: go wild.

--- generate-completions.sh ---
#!/bin/sh

cat << EOF
function gitcomplete ()
{
	case "\$COMP_LINE" in
EOF

cmds=""
for file in Documentation/git-*.txt; do
	cmd=$(expr $file : "Documentation/git-\(.*\).txt")
	cmds="$cmds $cmd"
	echo "	git\ $cmd\ *)"
	echo '		case "$3" in'

	# get all lines which begin with '-' or '<' and end with '::',
	# because they likely contain a command line option
	sed -n "s/^\([-<].*\)::/\1/p" < $file | { \
		opts=""
		while read line; do
			# some lines have the form '-a|--all'
			opt1=$(expr "$line" : '^\([^ |<]*\)')
			opt2=$(expr "$line" : '|\([^ |<]*\)')
			opts="$opts $opt1 $opt2"
			# this is an example how to handle non-simple args:
			# '-m <msg>' means: if the second-last arg is '-m',
			# the current arg is the message.
			test "$(expr "$line" : ".*<msg>")" != 0 &&
				echo "		'$opt1')" &&
				echo '			COMPREPLY=(\"a \"z);;'
		done
		echo "		*)"
		echo "			COMPREPLY=(\$(compgen -W \"$opts\" -- \$2));;"
	}
	echo "		esac;;"
done

cat << EOF
	git\ *\ *)
		COMPREPLY=();;
	git\ *)
		COMPREPLY=(\$(compgen -W '$cmds' -- \$2));;
	esac
}

complete -o default -F gitcomplete git
EOF
--- cut ---

-
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Received on Sun Apr 16 12:06:03 2006

This archive was generated by hypermail 2.1.8 : 2006-04-16 12:06:24 EST