Skip to content

Instantly share code, notes, and snippets.

@samy
Last active May 22, 2020 11:58
Show Gist options
  • Save samy/fea739e64fb373101fd8c76f249d234e to your computer and use it in GitHub Desktop.
Save samy/fea739e64fb373101fd8c76f249d234e to your computer and use it in GitHub Desktop.
Shell auto-completion script for themes and plugins list for wp-cli subcommands
_wp_complete() {
local cur=${COMP_WORDS[COMP_CWORD]}
local command=${COMP_WORDS[1]}
local subcommand=${COMP_WORDS[2]}
COMPREPLY=""
if [[ "$command" = "plugin" ]]
then
case "$subcommand" in
activate|deactivate|update|delete|get|is-active|path|status|uninstall|verify-checksums)
local plugin_list=$(wp plugin list --field=name --format=csv)
COMPREPLY=( $(compgen -W "${plugin_list}" -- ${cur} ))
;;
*)
esac
fi
if [[ "$COMPREPLY" = "" ]]
then
local opts="$(wp cli completions --line="$COMP_LINE" --point="$COMP_POINT")"
if [[ "$opts" =~ \<file\>\s* ]]
then
COMPREPLY=( $(compgen -f -- $cur) )
elif [[ $opts = "" ]]
then
COMPREPLY=( $(compgen -f -- $cur) )
else
COMPREPLY=( ${opts[*]} )
fi
fi
return 0
}
complete -o nospace -F _wp_complete wp
@lrtrln
Copy link

lrtrln commented May 22, 2020

with theme

_wp_complete() {
	local cur=${COMP_WORDS[COMP_CWORD]}
	local command=${COMP_WORDS[1]}
	local subcommand=${COMP_WORDS[2]}
	COMPREPLY=""

	if [[ "$command" = "plugin" ]]
	then
		case "$subcommand" in
		activate|deactivate|update|delete|get|is-active|path|status|uninstall|verify-checksums)
			local plugin_list=$(wp plugin list --field=name --format=csv)
			COMPREPLY=( $(compgen -W "${plugin_list}" -- ${cur} ))
			;;
		*)
		esac
	elif [[ "$command" = "theme" ]]
	then
		case "$subcommand" in
		activate|delete|disable|enable|is-active|is-installed|path|status|update)
			local theme_list=$(wp theme list --field=name --format=csv)
			COMPREPLY=( $(compgen -W "${theme_list}" -- ${cur} ))
			;;
		*)
		esac
	fi
	if [[ "$COMPREPLY" = "" ]]
	then
		local opts="$(wp cli completions --line="$COMP_LINE" --point="$COMP_POINT")"

		if [[ "$opts" =~ \<file\>\s* ]]
		then
			COMPREPLY=( $(compgen -f -- $cur) )
		elif [[ $opts = "" ]]
		then
			COMPREPLY=( $(compgen -f -- $cur) )
		else
			COMPREPLY=( ${opts[*]} )
		fi
	fi

	return 0
}
complete -o nospace -F _wp_complete wp

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment