diff --git a/README.md b/README.md index 00b3448..dd5f070 100644 --- a/README.md +++ b/README.md @@ -25,6 +25,18 @@ Use the following command to install the command line tool via Composer: `composer global require mglaman/drupalorg-cli` +### Installing (Bash) completion + +`drupalorg` comes with completion support for all commands, excluding options. + +To activate it, either source the completion file or add it to the system-wide completion directory, normally `/etc/bash_completion.d/`. + +In your `.bashrc` (or `.profile`) add + +``` +source [...]/vendor/mglaman/drupalorg-cli/drupalorg-cli-completion.sh +``` + ## Updating Automatic updating is not yet supported. You will need to manually download new releases. diff --git a/drupalorg-cli-completion.sh b/drupalorg-cli-completion.sh new file mode 100644 index 0000000..0778848 --- /dev/null +++ b/drupalorg-cli-completion.sh @@ -0,0 +1,24 @@ +#!/bin/bash + +_drupalorgcli_complete() { + local cur prev words cword command + _init_completion -n : || return + + # This is a minimal completion - global options and options for each command is not completed. + # Many edge cases are not covered, for example completion after command option is wrong. + + COMPREPLY=() + + # Stop completing if command is already present, except for "help". + # (This check fails when options follows command.) + if [[ -n $prev ]] && [[ $prev != "help" ]]; then + command=$(compgen -W "$(drupalorg complete)" -- "$prev") + if [[ $command == $prev ]]; then + return 0 + fi + fi + + COMPREPLY=( $(compgen -W "$(drupalorg complete)" -- "$cur") ) + __ltrim_colon_completions "$cur" +} && +complete -F _drupalorgcli_complete drupalorg diff --git a/src/Cli/Application.php b/src/Cli/Application.php index a456a05..a59325c 100644 --- a/src/Cli/Application.php +++ b/src/Cli/Application.php @@ -41,6 +41,7 @@ public function getCommands() } $commands[] = new Command\CacheClear(); + $commands[] = new Command\Completion(); $commands[] = new Command\DrupalCi\ListResults(); $commands[] = new Command\DrupalCi\Watch(); $commands[] = new Command\Issue\Link(); diff --git a/src/Cli/Command/Completion.php b/src/Cli/Command/Completion.php new file mode 100644 index 0000000..1b16721 --- /dev/null +++ b/src/Cli/Command/Completion.php @@ -0,0 +1,44 @@ +setName('complete') + ->setDescription('List commands for (Bash) completion'); + } + + /** + * {@inheritdoc} + * + */ + protected function execute(InputInterface $input, OutputInterface $output) + { + $command = $this->getApplication()->find('list'); + + $arguments = [ + '--format' => 'json' + ]; + $input = new ArrayInput($arguments); + $output = new BufferedOutput(); + $returnCode = $command->run($input, $output); + $commandListArray = json_decode($output->fetch(), true); + $commandsToComplete = []; + foreach ($commandListArray['commands'] as $commandToAdd) { + if ($commandToAdd['name'] == $this->getName()) { + continue; + } + $commandsToComplete[] = $commandToAdd['name']; + } + $this->stdOut->writeln(implode(' ', $commandsToComplete)); + } +}