Skip to content

Commit 5461d43

Browse files
committed
Merge pull request #66 from CarsonF/feature/array-arg-multiple
Support completing array arguments multiple times
2 parents e104743 + 11357ff commit 5461d43

File tree

3 files changed

+26
-16
lines changed

3 files changed

+26
-16
lines changed

src/CompletionHandler.php

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -277,23 +277,28 @@ protected function completeForCommandName()
277277
*/
278278
protected function completeForCommandArguments()
279279
{
280-
if (strpos($this->context->getCurrentWord(), '-') !== 0) {
281-
if ($this->command) {
282-
$argWords = $this->mapArgumentsToWords($this->command->getNativeDefinition()->getArguments());
283-
$wordIndex = $this->context->getWordIndex();
280+
if (!$this->command || strpos($this->context->getCurrentWord(), '-') === 0) {
281+
return false;
282+
}
284283

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

288-
if ($helper = $this->getCompletionHelper($name, Completion::TYPE_ARGUMENT)) {
289-
return $helper->run();
290-
}
288+
if (isset($argWords[$wordIndex])) {
289+
$name = $argWords[$wordIndex];
290+
} elseif (!empty($argWords) && $definition->getArgument(end($argWords))->isArray()) {
291+
$name = end($argWords);
292+
} else {
293+
return false;
294+
}
291295

292-
if ($this->command instanceof CompletionAwareInterface) {
293-
return $this->command->completeArgumentValues($name, $this->context);
294-
}
295-
}
296-
}
296+
if ($helper = $this->getCompletionHelper($name, Completion::TYPE_ARGUMENT)) {
297+
return $helper->run();
298+
}
299+
300+
if ($this->command instanceof CompletionAwareInterface) {
301+
return $this->command->completeArgumentValues($name, $this->context);
297302
}
298303

299304
return false;

tests/Stecman/Component/Symfony/Console/BashCompletion/CompletionHandlerTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,8 @@ public function completionAwareCommandDataProvider()
124124
'argument suggestions' => array('app completion-aware any-arg ', array('one-arg', 'two-arg')),
125125
'argument no suggestions' => array('app completion-aware ', array()),
126126
'argument suggestions + context' => array('app completion-aware any-arg one', array('one-arg', 'one-arg-context')),
127+
'array argument suggestions' => array('app completion-aware any-arg one-arg array-arg1 ', array('one-arg', 'two-arg')),
128+
'array argument suggestions + context' => array('app completion-aware any-arg one-arg array-arg1 one', array('one-arg', 'one-arg-context')),
127129
'option suggestions' => array('app completion-aware --option-with-suggestions ', array('one-opt', 'two-opt')),
128130
'option no suggestions' => array('app completion-aware --option-without-suggestions ', array()),
129131
'option suggestions + context' => array(

tests/Stecman/Component/Symfony/Console/BashCompletion/Fixtures/CompletionAwareCommand.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
use Stecman\Component\Symfony\Console\BashCompletion\Completion\CompletionAwareInterface;
55
use Stecman\Component\Symfony\Console\BashCompletion\CompletionContext;
66
use Symfony\Component\Console\Command\Command;
7+
use Symfony\Component\Console\Input\InputArgument;
78
use Symfony\Component\Console\Input\InputOption;
89

910
class CompletionAwareCommand extends Command implements CompletionAwareInterface
@@ -14,7 +15,9 @@ protected function configure()
1415
->addOption('option-with-suggestions', null, InputOption::VALUE_REQUIRED)
1516
->addOption('option-without-suggestions', null, InputOption::VALUE_REQUIRED)
1617
->addArgument('argument-without-suggestions')
17-
->addArgument('argument-with-suggestions');
18+
->addArgument('argument-with-suggestions')
19+
->addArgument('array-argument-with-suggestions', InputArgument::IS_ARRAY)
20+
;
1821
}
1922

2023
/**
@@ -50,7 +53,7 @@ public function completeOptionValues($optionName, CompletionContext $context)
5053
*/
5154
public function completeArgumentValues($argumentName, CompletionContext $context)
5255
{
53-
if ($argumentName === 'argument-with-suggestions') {
56+
if (in_array($argumentName, array('argument-with-suggestions', 'array-argument-with-suggestions'))) {
5457
$suggestions = array('one-arg', 'two-arg');
5558

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

0 commit comments

Comments
 (0)