Completion Tool

Working with the console gives the developer a lot of possibilities but havingto completely know and write those commands can be tedious. Especially whendeveloping new shells where the commands differ per minute iteration. TheCompletion Shells aids in this matter by providing an API to write completionscripts for shells like bash, zsh, fish etc.

Sub Commands

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.

Commands

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:

  1. bin/cake Completion commands

Returns:

  1. 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:

  1. bin/cake Completion subcommands bake

Returns:

  1. controller db_config fixture model plugin project test view

options

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:

  1. bin/cake Completion options bake

Returns:

  1. --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.

How to enable Bash autocompletion for the CakePHP Console

First, make sure the bash-completion library is installed. If not, you do itwith the following command:

  1. apt-get install bash-completion

Create a file named cake in /etc/bash_completion.d/ and put theBash Completion file content inside it.

Save the file, then restart your console.

Note

If you are using MacOS X, you can install the bash-completion libraryusing homebrew with the command brew install bash-completion.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:

  1. #
  2. # Bash completion file for CakePHP console
  3. #
  4.  
  5. _cake()
  6. {
  7. local cur prev opts cake
  8. COMPREPLY=()
  9. cake="${COMP_WORDS[0]}"
  10. cur="${COMP_WORDS[COMP_CWORD]}"
  11. prev="${COMP_WORDS[COMP_CWORD-1]}"
  12.  
  13. if [[ "$cur" == -* ]] ; then
  14. if [[ ${COMP_CWORD} = 1 ]] ; then
  15. opts=$(${cake} Completion options)
  16. elif [[ ${COMP_CWORD} = 2 ]] ; then
  17. opts=$(${cake} Completion options "${COMP_WORDS[1]}")
  18. else
  19. opts=$(${cake} Completion options "${COMP_WORDS[1]}" "${COMP_WORDS[2]}")
  20. fi
  21.  
  22. COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
  23. return 0
  24. fi
  25.  
  26. if [[ ${COMP_CWORD} = 1 ]] ; then
  27. opts=$(${cake} Completion commands)
  28. COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
  29. return 0
  30. fi
  31.  
  32. if [[ ${COMP_CWORD} = 2 ]] ; then
  33. opts=$(${cake} Completion subcommands $prev)
  34. COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
  35. if [[ $COMPREPLY = "" ]] ; then
  36. _filedir
  37. return 0
  38. fi
  39. return 0
  40. fi
  41.  
  42. opts=$(${cake} Completion fuzzy "${COMP_WORDS[@]:1}")
  43. COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
  44. if [[ $COMPREPLY = "" ]] ; then
  45. _filedir
  46. return 0
  47. fi
  48. return 0;
  49. }
  50.  
  51. complete -F _cake cake bin/cake

Using autocompletion

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.

Commands

Sample output for commands autocompletion:

  1. $ bin/cake <tab>
  2. bake i18n schema_cache routes
  3. console migrations plugin server

Subcommands

Sample output for subcommands autocompletion:

  1. $ bin/cake bake <tab>
  2. behavior helper shell
  3. cell mailer shell_helper
  4. component migration template
  5. controller migration_snapshot test
  6. fixture model
  7. form plugin

Options

Sample output for subcommands options autocompletion:

  1. $ bin/cake bake -<tab>
  2. -c --everything --force --help --plugin -q -t -v
  3. --connection -f -h -p --prefix --quiet --theme --verbose