Skip to content

Commit

Permalink
Merge pull request #66 from CarsonF/feature/array-arg-multiple
Browse files Browse the repository at this point in the history
Support completing array arguments multiple times
  • Loading branch information
stecman committed Feb 24, 2016
2 parents e104743 + 11357ff commit 5461d43
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 16 deletions.
33 changes: 19 additions & 14 deletions src/CompletionHandler.php
Expand Up @@ -277,23 +277,28 @@ protected function completeForCommandName()
*/
protected function completeForCommandArguments()
{
if (strpos($this->context->getCurrentWord(), '-') !== 0) {
if ($this->command) {
$argWords = $this->mapArgumentsToWords($this->command->getNativeDefinition()->getArguments());
$wordIndex = $this->context->getWordIndex();
if (!$this->command || strpos($this->context->getCurrentWord(), '-') === 0) {
return false;
}

if (isset($argWords[$wordIndex])) {
$name = $argWords[$wordIndex];
$definition = $this->command->getNativeDefinition();
$argWords = $this->mapArgumentsToWords($definition->getArguments());
$wordIndex = $this->context->getWordIndex();

if ($helper = $this->getCompletionHelper($name, Completion::TYPE_ARGUMENT)) {
return $helper->run();
}
if (isset($argWords[$wordIndex])) {
$name = $argWords[$wordIndex];
} elseif (!empty($argWords) && $definition->getArgument(end($argWords))->isArray()) {
$name = end($argWords);
} else {
return false;
}

if ($this->command instanceof CompletionAwareInterface) {
return $this->command->completeArgumentValues($name, $this->context);
}
}
}
if ($helper = $this->getCompletionHelper($name, Completion::TYPE_ARGUMENT)) {
return $helper->run();
}

if ($this->command instanceof CompletionAwareInterface) {
return $this->command->completeArgumentValues($name, $this->context);
}

return false;
Expand Down
Expand Up @@ -124,6 +124,8 @@ public function completionAwareCommandDataProvider()
'argument suggestions' => array('app completion-aware any-arg ', array('one-arg', 'two-arg')),
'argument no suggestions' => array('app completion-aware ', array()),
'argument suggestions + context' => array('app completion-aware any-arg one', array('one-arg', 'one-arg-context')),
'array argument suggestions' => array('app completion-aware any-arg one-arg array-arg1 ', array('one-arg', 'two-arg')),
'array argument suggestions + context' => array('app completion-aware any-arg one-arg array-arg1 one', array('one-arg', 'one-arg-context')),
'option suggestions' => array('app completion-aware --option-with-suggestions ', array('one-opt', 'two-opt')),
'option no suggestions' => array('app completion-aware --option-without-suggestions ', array()),
'option suggestions + context' => array(
Expand Down
Expand Up @@ -4,6 +4,7 @@
use Stecman\Component\Symfony\Console\BashCompletion\Completion\CompletionAwareInterface;
use Stecman\Component\Symfony\Console\BashCompletion\CompletionContext;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;

class CompletionAwareCommand extends Command implements CompletionAwareInterface
Expand All @@ -14,7 +15,9 @@ protected function configure()
->addOption('option-with-suggestions', null, InputOption::VALUE_REQUIRED)
->addOption('option-without-suggestions', null, InputOption::VALUE_REQUIRED)
->addArgument('argument-without-suggestions')
->addArgument('argument-with-suggestions');
->addArgument('argument-with-suggestions')
->addArgument('array-argument-with-suggestions', InputArgument::IS_ARRAY)
;
}

/**
Expand Down Expand Up @@ -50,7 +53,7 @@ public function completeOptionValues($optionName, CompletionContext $context)
*/
public function completeArgumentValues($argumentName, CompletionContext $context)
{
if ($argumentName === 'argument-with-suggestions') {
if (in_array($argumentName, array('argument-with-suggestions', 'array-argument-with-suggestions'))) {
$suggestions = array('one-arg', 'two-arg');

if ('one' === $context->getCurrentWord()) {
Expand Down

0 comments on commit 5461d43

Please sign in to comment.