Skip to content

Commit 59d3235

Browse files
committed
Add Suggestion class for more advanced completion suggestion
1 parent c67fced commit 59d3235

File tree

4 files changed

+51
-10
lines changed

4 files changed

+51
-10
lines changed

Completion/CompletionSuggestions.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,33 +18,37 @@
1818
*
1919
* @author Wouter de Jong <[email protected]>
2020
*/
21-
class CompletionSuggestions
21+
final class CompletionSuggestions
2222
{
2323
private $valueSuggestions = [];
2424
private $optionSuggestions = [];
2525

2626
/**
2727
* Add a suggested value for an input option or argument.
2828
*
29+
* @param string|Suggestion $value
30+
*
2931
* @return $this
3032
*/
31-
public function suggestValue(string $value): self
33+
public function suggestValue($value): self
3234
{
33-
$this->valueSuggestions[] = $value;
35+
$this->valueSuggestions[] = !$value instanceof Suggestion ? new Suggestion($value) : $value;
3436

3537
return $this;
3638
}
3739

3840
/**
3941
* Add multiple suggested values at once for an input option or argument.
4042
*
41-
* @param string[] $values
43+
* @param list<string|Suggestion> $values
4244
*
4345
* @return $this
4446
*/
4547
public function suggestValues(array $values): self
4648
{
47-
$this->valueSuggestions = array_merge($this->valueSuggestions, $values);
49+
foreach ($values as $value) {
50+
$this->suggestValue($value);
51+
}
4852

4953
return $this;
5054
}
@@ -86,7 +90,7 @@ public function getOptionSuggestions(): array
8690
}
8791

8892
/**
89-
* @return string[]
93+
* @return Suggestion[]
9094
*/
9195
public function getValueSuggestions(): array
9296
{

Completion/Output/BashCompletionOutput.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ class BashCompletionOutput implements CompletionOutputInterface
2121
{
2222
public function write(CompletionSuggestions $suggestions, OutputInterface $output): void
2323
{
24-
$options = $suggestions->getValueSuggestions();
24+
$values = $suggestions->getValueSuggestions();
2525
foreach ($suggestions->getOptionSuggestions() as $option) {
26-
$options[] = '--'.$option->getName();
26+
$values[] = '--'.$option->getName();
2727
}
28-
$output->writeln(implode("\n", $options));
28+
$output->writeln(implode("\n", $values));
2929
}
3030
}

Completion/Suggestion.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Console\Completion;
13+
14+
/**
15+
* Represents a single suggested value.
16+
*
17+
* @author Wouter de Jong <[email protected]>
18+
*/
19+
class Suggestion
20+
{
21+
private $value;
22+
23+
public function __construct(string $value)
24+
{
25+
$this->value = $value;
26+
}
27+
28+
public function getValue(): string
29+
{
30+
return $this->value;
31+
}
32+
33+
public function __toString(): string
34+
{
35+
return $this->getValue();
36+
}
37+
}

Tester/CommandCompletionTester.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,6 @@ public function complete(array $input): array
5151
$options[] = '--'.$option->getName();
5252
}
5353

54-
return array_merge($options, $suggestions->getValueSuggestions());
54+
return array_map('strval', array_merge($options, $suggestions->getValueSuggestions()));
5555
}
5656
}

0 commit comments

Comments
 (0)