git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[FYI] generated completions

From
Johannes Schindelin <johannes.schindelin@gmx.de>
Date
Apr 16, 2006, 02:05 UTC
Message-ID
<Pine.LNX.4.63.0604160401330.31461@wbgn013.biozentrum.uni-wuerzburg.de>
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 ---

Message 1 of 1 in “[FYI] generated completions”
  1. Johannes SchindelinApr 16, 2006

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.