From d70320af518bf0c8784271dd9dd9c63b1ab45830 Mon Sep 17 00:00:00 2001 From: Alexander Obuhovich Date: Wed, 13 May 2015 00:07:07 +0300 Subject: [PATCH] Allow `_completion` command to auto-complete it's arguments or their values --- src/CompletionCommand.php | 52 +++++++++++++++++++++++++-------------- src/CompletionHandler.php | 10 ++++---- 2 files changed, 39 insertions(+), 23 deletions(-) diff --git a/src/CompletionCommand.php b/src/CompletionCommand.php index d3d81da..88108b5 100644 --- a/src/CompletionCommand.php +++ b/src/CompletionCommand.php @@ -3,6 +3,7 @@ namespace Stecman\Component\Symfony\Console\BashCompletion; use Symfony\Component\Console\Command\Command as SymfonyCommand; +use Symfony\Component\Console\Input\InputDefinition; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; @@ -19,6 +20,7 @@ protected function configure() { $this ->setName('_completion') + ->setDefinition($this->createDefinition()) ->setDescription('BASH completion hook.') ->setHelp(<<eval `[program] _completion -g -p [alias]`. END - ) - ->addOption( - 'generate-hook', - 'g', - InputOption::VALUE_NONE, - 'Generate BASH code that sets up completion for this application.' - ) - ->addOption( - 'program', - 'p', - InputOption::VALUE_REQUIRED, - "Program name that should trigger completion\n(defaults to the absolute application path)." - ) - ->addOption( - 'shell-type', - null, - InputOption::VALUE_OPTIONAL, - 'Set the shell type (zsh or bash). Otherwise this is determined automatically.' ); } + /** + * {@inheritdoc} + */ + public function getNativeDefinition() + { + return $this->createDefinition(); + } + protected function execute(InputInterface $input, OutputInterface $output) { $this->handler = new CompletionHandler($this->getApplication()); @@ -110,4 +102,28 @@ protected function getShellType() return basename(getenv('SHELL')); } + + protected function createDefinition() + { + return new InputDefinition(array( + new InputOption( + 'generate-hook', + 'g', + InputOption::VALUE_NONE, + 'Generate BASH code that sets up completion for this application.' + ), + new InputOption( + 'program', + 'p', + InputOption::VALUE_REQUIRED, + "Program name that should trigger completion\n(defaults to the absolute application path)." + ), + new InputOption( + 'shell-type', + null, + InputOption::VALUE_OPTIONAL, + 'Set the shell type (zsh or bash). Otherwise this is determined automatically.' + ), + )); + } } diff --git a/src/CompletionHandler.php b/src/CompletionHandler.php index 0925df0..11998b5 100644 --- a/src/CompletionHandler.php +++ b/src/CompletionHandler.php @@ -177,7 +177,7 @@ protected function completeForOptionShortcuts() $word = $this->context->getCurrentWord(); if (strpos($word, '-') === 0 && strlen($word) == 2) { - $definition = $this->command ? $this->command->getDefinition() : $this->application->getDefinition(); + $definition = $this->command ? $this->command->getNativeDefinition() : $this->application->getDefinition(); if ($definition->hasShortcut(substr($word, 1))) { return array($word); @@ -202,7 +202,7 @@ protected function completeForOptionShortcutValues() // Complete short options if ($left[0] == '-' && strlen($left) == 2) { $shortcut = substr($left, 1); - $def = $this->command->getDefinition(); + $def = $this->command->getNativeDefinition(); if (!$def->hasShortcut($shortcut)) { return false; @@ -232,7 +232,7 @@ protected function completeForOptionValues() if (strpos($left, '--') === 0) { $name = substr($left, 2); - $def = $this->command->getDefinition(); + $def = $this->command->getNativeDefinition(); if (!$def->hasOption($name)) { return false; @@ -279,7 +279,7 @@ protected function completeForCommandArguments() { if (strpos($this->context->getCurrentWord(), '-') !== 0) { if ($this->command) { - $argWords = $this->mapArgumentsToWords($this->command->getDefinition()->getArguments()); + $argWords = $this->mapArgumentsToWords($this->command->getNativeDefinition()->getArguments()); $wordIndex = $this->context->getWordIndex(); if (isset($argWords[$wordIndex])) { @@ -433,7 +433,7 @@ protected function getAllOptions() } return array_merge( - $this->command->getDefinition()->getOptions(), + $this->command->getNativeDefinition()->getOptions(), $this->application->getDefinition()->getOptions() ); }