Code


Version 2 (modified by anonymous, 7 years ago) (diff)

--

Bash auto-completion for django-admin.py/manage.py

Can't you remember the options for django-admin.py or are you just plain lazy as me, then the following little script might be something for you.

The following script will add auto completion to django-admin.py and manage.py for actions and options. It has the latest actions and options from trunk 5833. The script is limited to only auto-completing the actions and options. The script won't e.g find your application names if you type manage.py adminindex somepartialappname<TAB>.

Installation

To install it, copy the script below and save it e.g. in a file ~/.djangocompletion.sh. In your ~/.profile you just run the script by e.g. adding the follwoing line:

source ~/.djangocompletion.sh

You may also add the script directly in your .profile file.

Bash script

#!/bin/sh
#
# Initial author: Lars Holm Nielsen <lars@hankat.dk>
# Copyright-free

_djangoadmin_complete()
{
	# manage.py action [options]
	local cur prev actions base
	COMPREPLY=()
	cur="${COMP_WORDS[COMP_CWORD]}"
	prev="${COMP_WORDS[COMP_CWORD-1]}"
	base="${COMP_WORDS[1]}"

	#
	# Completion list of commands
	#
	actions='adminindex createcachetable dbshell diffsettings dumpdata flush inspectdb loaddata reset runfcgi runserver shell sql sqlall sqlclear sqlcustom sqlflush sqlindexes sqlreset sqlsequencereset startapp syncdb test validate'
	
	if [ "${prev##*/}" = "django-admin.py" ]
	then
		COMPREPLY=($(compgen -W "$actions" -- "$cur"))
		return 0
	fi
	
	if [ "${prev##*/}" = "manage.py" ]
	then
		COMPREPLY=($(compgen -W "$actions" -- "$cur"))
		return 0
	fi
	
	local results
	
	#
    #  Complete the arguments to some of the basic commands.
    #
    case "${base}" in
		# [appname ...]
		adminindex|sql|sqlall|sqlclear|sqlcustom|sqlindexes|sqlreset|sqlsequencereset)
		 	results=''
			;;
		# [tablename]
		createcachetable)
		 	results=''
			;;
		# [--format=FORMAT] [--indent=INDENT] [appname ...]
		dumpdata)
			results='--format= --indent='
			;;
		# [--verbosity=VERBOSITY] [--noinput]
		syncdb|flush)
		 	results='--verbosity= --noinput'
			;;
		# [--verbosity=VERBOSITY] fixture, fixture, ...
		loaddata)
			results='--verbosity=VERBOSITY'
			;;
		# [--noinput][appname ...]
		reset)
		 	results='--noinput'
			;;
		# [various KEY=val options, use `runfcgi help` for help]
		runfcgi)
		 	results='help'
			;;
		# [--noreload] [--adminmedia=ADMIN_MEDIA_PATH] [optional port number, or ipaddr:port]
		runserver)
		 	results='--noreload --adminmedia='
			;;
		# [--plain]
		shell)
		 	results='--plain'
			;;
		# [appname]
		startapp)
		 	results=''
			;;
		# [--verbosity=VERBOSITY] [--noinput] [appname ...]
		test)
		 	results='--verbosity= --noinput'
			;;
    esac
	
	results="$results --version -h --help --settings= --pythonpath="

	COMPREPLY=($(compgen -W "$results" -- "$cur"))
	return 0
}

complete -F _djangoadmin_complete django-admin.py
complete -F _djangoadmin_complete manage.py

Bugs

Since I'm not a big shell programmer, there's probably some bugs in the script. Feel free to edit the script if you find any or add more features to it.