Skip to content

Instantly share code, notes, and snippets.

@msonnabaum
Created June 8, 2011 18:38
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save msonnabaum/e2231815a99d9395fb76 to your computer and use it in GitHub Desktop.
Save msonnabaum/e2231815a99d9395fb76 to your computer and use it in GitHub Desktop.
drush autocomplete
#!bash
# bash completion support for Drush.
have drush &&
__drushdir () {
echo $(drush dd)
}
__drush_all_opts=
__drush_compute_all_opts () {
: ${__drush_all_opts:=$(__drush_global_opts)}
}
__drush_global_opts () {
local i IFS=" "$'\n'
for i in $(drush ev '$opts = drush_global_options_command();drush_print("--" . implode("\n--", array_keys($opts["options"])));')
do
echo $i
done
}
__drush_all_commands=
__drush_compute_all_commands () {
: ${__drush_all_commands:=$(__drush_list_all_commands)}
}
__drush_list_all_commands () {
local i IFS=" "$'\n' dir="$(__drushdir "${1-}")"
for i in $(drush --root="$dir" --pipe)
do
case $i in
*--*) : helper pattern;;
*) echo $i;;
esac
done
}
# generates completion reply with compgen
__drushcomp () {
local cur
_get_comp_words_by_ref -n =: cur
if [ $# -gt 2 ]; then
cur="$3"
fi
case "$cur" in
--*=)
COMPREPLY=()
;;
*)
local IFS=$'\n'
COMPREPLY=($(compgen -P "${2-}" \
-W "$(__drushcomp_1 "${1-}" "${4-}")" \
-- "$cur"))
;;
esac
}
# __drushcomp_1 requires 2 arguments
__drushcomp_1 ()
{
local c IFS=' '$'\t'$'\n'
for c in $1; do
case "$c$2" in
--*=*) printf %s$'\n' "$c$2" ;;
*.) printf %s$'\n' "$c$2" ;;
*) printf %s$'\n' "$c$2 " ;;
esac
done
}
_drush() {
local i c=1 command
if [[ -n ${ZSH_VERSION-} ]]; then
emulate -L bash
setopt KSH_TYPESET
fi
local cur words cword
_get_comp_words_by_ref -n =: cur words cword
if [ -z "$command" ]; then
case "$cur" in
--*) __drush_compute_all_opts
__drushcomp "$__drush_all_opts";;
*) __drush_compute_all_commands
__drushcomp "$__drush_all_commands";;
esac
return
fi
}
complete -o bashdefault -o default -o nospace -F _drush drush 2>/dev/null \
|| complete -o default -o nospace -F _drush drush
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment