Completion Shell
The Completion Shell consists of a number of sub commands to assist thedeveloper creating its completion script. Each for a different step in theautocompletion process.
For the first step commands outputs the available Shell Commands, includingplugin name when applicable. (All returned possibilities, for this and the othersub commands, are separated by a space.) For example:
Returns:
- acl api bake command_list completion console i18n schema server test testsuite upgrade
Your completion script can select the relevant commands from that list tocontinue with. (For this and the following sub commands.)
subCommands
Once the preferred command has been chosen subCommands comes in as the secondstep and outputs the possible sub command for the given shell command. Forexample:
- bin/cake Completion subcommands bake
As the third and final options outputs options for the given (sub) command asset in getOptionParser. (Including the default options inherited from Shell.)For example:
- bin/cake Completion options bake
Returns:
- --help -h --verbose -v --quiet -q --everything --connection -c --force -f --plugin -p --prefix --theme -t
You can also pass an additional argument being the shell sub-command : it willoutput the specific options of this sub-command.
First, make sure the bash-completion library is installed. If not, you do itwith the following command:
Create a file named cake in /etc/bash_completion.d/ and put the inside it.
Note
If you are using MacOS X, you can install the bash-completion libraryusing homebrew with the command .The target directory for the cake file will be/usr/local/etc/bash_completion.d/.
Bash Completion file content
This is the code you need to put inside the cake file in the correct locationin order to get autocompletion when using the CakePHP console:
- #
- # Bash completion file for CakePHP console
- #
- _cake()
- {
- local cur prev opts cake
- COMPREPLY=()
- cake="${COMP_WORDS[0]}"
- prev="${COMP_WORDS[COMP_CWORD-1]}"
- if [[ "$cur" == -* ]] ; then
- if [[ ${COMP_CWORD} = 1 ]] ; then
- opts=$(${cake} Completion options)
- elif [[ ${COMP_CWORD} = 2 ]] ; then
- opts=$(${cake} Completion options "${COMP_WORDS[1]}")
- else
- opts=$(${cake} Completion options "${COMP_WORDS[1]}" "${COMP_WORDS[2]}")
- fi
- COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
- return 0
- fi
- if [[ ${COMP_CWORD} = 1 ]] ; then
- opts=$(${cake} Completion commands)
- COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
- return 0
- fi
- if [[ ${COMP_CWORD} = 2 ]] ; then
- opts=$(${cake} Completion subcommands $prev)
- if [[ $COMPREPLY = "" ]] ; then
- _filedir
- return 0
- fi
- return 0
- fi
- opts=$(${cake} Completion fuzzy "${COMP_WORDS[@]:1}")
- COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
- if [[ $COMPREPLY = "" ]] ; then
- _filedir
- return 0
- fi
- return 0;
- }
- complete -F _cake cake bin/cake
Once enabled, the autocompletion can be used the same way than for otherbuilt-in commands, using the TAB key.Three type of autocompletion are provided. The following output are from a fresh CakePHP install.
Sample output for commands autocompletion:
- $ bin/cake <tab>
- bake i18n schema_cache routes
- console migrations plugin server
Subcommands
Sample output for subcommands options autocompletion:
- $ bin/cake bake -<tab>
- -c --everything --force --help --plugin -q -t -v
- --connection -f -h -p --prefix --quiet --theme --verbose