Skip to content

Commit 35659d2

Browse files
committed
Merge pull request #47 from aik099/46-unable-to-auto-complete-for-completion-command
Allow `_completion` command to auto-complete it's arguments or their values
2 parents 47bf604 + d70320a commit 35659d2

File tree

2 files changed

+39
-23
lines changed

2 files changed

+39
-23
lines changed

src/CompletionCommand.php

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Stecman\Component\Symfony\Console\BashCompletion;
44

55
use Symfony\Component\Console\Command\Command as SymfonyCommand;
6+
use Symfony\Component\Console\Input\InputDefinition;
67
use Symfony\Component\Console\Input\InputInterface;
78
use Symfony\Component\Console\Input\InputOption;
89
use Symfony\Component\Console\Output\OutputInterface;
@@ -19,6 +20,7 @@ protected function configure()
1920
{
2021
$this
2122
->setName('_completion')
23+
->setDefinition($this->createDefinition())
2224
->setDescription('BASH completion hook.')
2325
->setHelp(<<<END
2426
To enable BASH completion, run:
@@ -30,27 +32,17 @@ protected function configure()
3032
<comment>eval `[program] _completion -g -p [alias]`</comment>.
3133
3234
END
33-
)
34-
->addOption(
35-
'generate-hook',
36-
'g',
37-
InputOption::VALUE_NONE,
38-
'Generate BASH code that sets up completion for this application.'
39-
)
40-
->addOption(
41-
'program',
42-
'p',
43-
InputOption::VALUE_REQUIRED,
44-
"Program name that should trigger completion\n<comment>(defaults to the absolute application path)</comment>."
45-
)
46-
->addOption(
47-
'shell-type',
48-
null,
49-
InputOption::VALUE_OPTIONAL,
50-
'Set the shell type (zsh or bash). Otherwise this is determined automatically.'
5135
);
5236
}
5337

38+
/**
39+
* {@inheritdoc}
40+
*/
41+
public function getNativeDefinition()
42+
{
43+
return $this->createDefinition();
44+
}
45+
5446
protected function execute(InputInterface $input, OutputInterface $output)
5547
{
5648
$this->handler = new CompletionHandler($this->getApplication());
@@ -110,4 +102,28 @@ protected function getShellType()
110102

111103
return basename(getenv('SHELL'));
112104
}
105+
106+
protected function createDefinition()
107+
{
108+
return new InputDefinition(array(
109+
new InputOption(
110+
'generate-hook',
111+
'g',
112+
InputOption::VALUE_NONE,
113+
'Generate BASH code that sets up completion for this application.'
114+
),
115+
new InputOption(
116+
'program',
117+
'p',
118+
InputOption::VALUE_REQUIRED,
119+
"Program name that should trigger completion\n<comment>(defaults to the absolute application path)</comment>."
120+
),
121+
new InputOption(
122+
'shell-type',
123+
null,
124+
InputOption::VALUE_OPTIONAL,
125+
'Set the shell type (zsh or bash). Otherwise this is determined automatically.'
126+
),
127+
));
128+
}
113129
}

src/CompletionHandler.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ protected function completeForOptionShortcuts()
177177
$word = $this->context->getCurrentWord();
178178

179179
if (strpos($word, '-') === 0 && strlen($word) == 2) {
180-
$definition = $this->command ? $this->command->getDefinition() : $this->application->getDefinition();
180+
$definition = $this->command ? $this->command->getNativeDefinition() : $this->application->getDefinition();
181181

182182
if ($definition->hasShortcut(substr($word, 1))) {
183183
return array($word);
@@ -202,7 +202,7 @@ protected function completeForOptionShortcutValues()
202202
// Complete short options
203203
if ($left[0] == '-' && strlen($left) == 2) {
204204
$shortcut = substr($left, 1);
205-
$def = $this->command->getDefinition();
205+
$def = $this->command->getNativeDefinition();
206206

207207
if (!$def->hasShortcut($shortcut)) {
208208
return false;
@@ -232,7 +232,7 @@ protected function completeForOptionValues()
232232

233233
if (strpos($left, '--') === 0) {
234234
$name = substr($left, 2);
235-
$def = $this->command->getDefinition();
235+
$def = $this->command->getNativeDefinition();
236236

237237
if (!$def->hasOption($name)) {
238238
return false;
@@ -279,7 +279,7 @@ protected function completeForCommandArguments()
279279
{
280280
if (strpos($this->context->getCurrentWord(), '-') !== 0) {
281281
if ($this->command) {
282-
$argWords = $this->mapArgumentsToWords($this->command->getDefinition()->getArguments());
282+
$argWords = $this->mapArgumentsToWords($this->command->getNativeDefinition()->getArguments());
283283
$wordIndex = $this->context->getWordIndex();
284284

285285
if (isset($argWords[$wordIndex])) {
@@ -433,7 +433,7 @@ protected function getAllOptions()
433433
}
434434

435435
return array_merge(
436-
$this->command->getDefinition()->getOptions(),
436+
$this->command->getNativeDefinition()->getOptions(),
437437
$this->application->getDefinition()->getOptions()
438438
);
439439
}

0 commit comments

Comments
 (0)