How to enable commandline autocomplete support for the `platform` CLI tool

The platform CLI tool contains a large library of commands, and trying to list them each time you can’t quite remember the names or options can fill up your screen really fast. Tab-completion will be able to help us out here.

Goal

We want autocomplete support (press tab to get command and option suggestions) for the platform CLI tool.

Assumptions

As it’s a symfony component, these commands can be programmatically extracted in a standard way.

The symfony-console-autocomplete tool does this for us.

Instructions here for OSX, the setup instructions are similar for other systems and shells

Prerequisites

If you don’t have bash-completion installed by default, get that first.

Using brew on OSX. *nix sessions probably already have it.

brew install bash-completion

Add the startup line to your ~/.bash_profile like it advises.

Scripts found in /usr/local/etc/bash_completion.d/ can support individual commands now.

Steps

Install symfony-console-autocomplete

    composer global require bamarni/symfony-console-autocomplete

Now we can generate a dump of all symfony-console-based CLI commands, and use that dump to generate the autocomplete prompts.

Auto-generate autocomplete hints for platform

    symfony-autocomplete platform --script-options=list > /usr/local/etc/bash_completion.d/platform

Restart your shell session (or at least source ~/.bash_profile) and autocomplete should be available for platform cli now.

The location /usr/local/etc/bash_completion.d/ will differ depending on your OS, version, and preferred shell. Alternate instructions for fish, zsh etc are in the README. The only difference from the examples there is that we have to add --script-options=list to extract the commands for platform.

Test

Pressing [TAB] part-way through entering platform commands will now provide either autocompletion or a list of suggestions, for both commands and options to those commands.

Examples:

$ platform pr[TAB]
platform project:    

$ platform project:[TAB]
clear-build-cache  curl               get                list               variable:delete    variable:set       
create             delete             info               set-remote         variable:get

$ platform project:l[TAB]
platform project:list

$ platform project:list --[TAB]
--columns    --help       --my         --no-header  --quiet      --reverse    --title      --version    
--format     --host       --no         --pipe       --refresh    --sort       --verbose    --yes 
1 Like

After some discussion earlier - the CLI has had built-in autocompletion support for a long time, but it’s not documented and doesn’t activate everywhere, specifically we found a problem with Bash 3.x (an old version present by default on Macs).

All that you should now need is:

  1. Have Bash or ZSH as your shell
  2. Have bash-completion installed, or the equivalent (ZSH usually has completion built in) - on Mac OS X with Homebrew this is brew install bash-completion
  3. Install the CLI via the installer or, if already installed, run platform up to get version 3.42.1 and platform self:install to install
  4. Open a new shell (to make sure everything is sourced that needs to be)

… that should be it.

In other words on many systems the CLI installation process should enable all this by default.