Skip to content

Commit f7573e5

Browse files
authored
Merge pull request #69 from hansfn/autocomplete
Bash completion support
2 parents efcc1d6 + b5c0b93 commit f7573e5

4 files changed

Lines changed: 81 additions & 0 deletions

File tree

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,18 @@ Use the following command to install the command line tool via Composer:
2525

2626
`composer global require mglaman/drupalorg-cli`
2727

28+
### Installing (Bash) completion
29+
30+
`drupalorg` comes with completion support for all commands, excluding options.
31+
32+
To activate it, either source the completion file or add it to the system-wide completion directory, normally `/etc/bash_completion.d/`.
33+
34+
In your `.bashrc` (or `.profile`) add
35+
36+
```
37+
source [...]/vendor/mglaman/drupalorg-cli/drupalorg-cli-completion.sh
38+
```
39+
2840
## Updating
2941

3042
Automatic updating is not yet supported. You will need to manually download new releases.

drupalorg-cli-completion.sh

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#!/bin/bash
2+
3+
_drupalorgcli_complete() {
4+
local cur prev words cword command
5+
_init_completion -n : || return
6+
7+
# This is a minimal completion - global options and options for each command is not completed.
8+
# Many edge cases are not covered, for example completion after command option is wrong.
9+
10+
COMPREPLY=()
11+
12+
# Stop completing if command is already present, except for "help".
13+
# (This check fails when options follows command.)
14+
if [[ -n $prev ]] && [[ $prev != "help" ]]; then
15+
command=$(compgen -W "$(drupalorg complete)" -- "$prev")
16+
if [[ $command == $prev ]]; then
17+
return 0
18+
fi
19+
fi
20+
21+
COMPREPLY=( $(compgen -W "$(drupalorg complete)" -- "$cur") )
22+
__ltrim_colon_completions "$cur"
23+
} &&
24+
complete -F _drupalorgcli_complete drupalorg

src/Cli/Application.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ public function getCommands()
4141
}
4242

4343
$commands[] = new Command\CacheClear();
44+
$commands[] = new Command\Completion();
4445
$commands[] = new Command\DrupalCi\ListResults();
4546
$commands[] = new Command\DrupalCi\Watch();
4647
$commands[] = new Command\Issue\Link();

src/Cli/Command/Completion.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
namespace mglaman\DrupalOrgCli\Command;
4+
5+
use mglaman\DrupalOrgCli\Cache;
6+
use Symfony\Component\Console\Input\ArrayInput;
7+
use Symfony\Component\Console\Input\InputInterface;
8+
use Symfony\Component\Console\Output\BufferedOutput;
9+
use Symfony\Component\Console\Output\OutputInterface;
10+
11+
class Completion extends Command
12+
{
13+
protected function configure()
14+
{
15+
$this
16+
->setName('complete')
17+
->setDescription('List commands for (Bash) completion');
18+
}
19+
20+
/**
21+
* {@inheritdoc}
22+
*
23+
*/
24+
protected function execute(InputInterface $input, OutputInterface $output)
25+
{
26+
$command = $this->getApplication()->find('list');
27+
28+
$arguments = [
29+
'--format' => 'json'
30+
];
31+
$input = new ArrayInput($arguments);
32+
$output = new BufferedOutput();
33+
$returnCode = $command->run($input, $output);
34+
$commandListArray = json_decode($output->fetch(), true);
35+
$commandsToComplete = [];
36+
foreach ($commandListArray['commands'] as $commandToAdd) {
37+
if ($commandToAdd['name'] == $this->getName()) {
38+
continue;
39+
}
40+
$commandsToComplete[] = $commandToAdd['name'];
41+
}
42+
$this->stdOut->writeln(implode(' ', $commandsToComplete));
43+
}
44+
}

0 commit comments

Comments
 (0)