Skip to content

Commit ae3f404

Browse files
committed
feature #43639 [Uid] Allow use autocompletion (StaffNowa)
This PR was squashed before being merged into the 5.4 branch. Discussion ---------- [Uid] Allow use autocompletion | Q | A | ------------- | --- | Branch? | 5.4 | Bug fix? | no | New feature? | yes | Deprecations? | no | Tickets | #43594 | License | MIT | Doc PR | - Adding Bash completion for following commands: ulid:generate uuid-genereate Commits ------- 1e400fb [Uid] Allow use autocompletion
2 parents 568947a + 1e400fb commit ae3f404

File tree

4 files changed

+94
-14
lines changed

4 files changed

+94
-14
lines changed

src/Symfony/Component/Uid/Command/GenerateUlidCommand.php

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
namespace Symfony\Component\Uid\Command;
1313

1414
use Symfony\Component\Console\Command\Command;
15+
use Symfony\Component\Console\Completion\CompletionInput;
16+
use Symfony\Component\Console\Completion\CompletionSuggestions;
1517
use Symfony\Component\Console\Input\InputInterface;
1618
use Symfony\Component\Console\Input\InputOption;
1719
use Symfony\Component\Console\Output\ConsoleOutputInterface;
@@ -83,14 +85,14 @@ protected function execute(InputInterface $input, OutputInterface $output)
8385
}
8486
}
8587

86-
switch ($input->getOption('format')) {
87-
case 'base32': $format = 'toBase32'; break;
88-
case 'base58': $format = 'toBase58'; break;
89-
case 'rfc4122': $format = 'toRfc4122'; break;
90-
default:
91-
$io->error(sprintf('Invalid format "%s", did you mean "base32", "base58" or "rfc4122"?', $input->getOption('format')));
88+
$formatOption = $input->getOption('format');
9289

93-
return 1;
90+
if (\in_array($formatOption, $this->getAvailableFormatOptions())) {
91+
$format = 'to'.ucfirst($formatOption);
92+
} else {
93+
$io->error(sprintf('Invalid format "%s", did you mean "base32", "base58" or "rfc4122"?', $input->getOption('format')));
94+
95+
return 1;
9496
}
9597

9698
$count = (int) $input->getOption('count');
@@ -106,4 +108,20 @@ protected function execute(InputInterface $input, OutputInterface $output)
106108

107109
return 0;
108110
}
111+
112+
public function complete(CompletionInput $input, CompletionSuggestions $suggestions): void
113+
{
114+
if ($input->mustSuggestOptionValuesFor('format')) {
115+
$suggestions->suggestValues($this->getAvailableFormatOptions());
116+
}
117+
}
118+
119+
private function getAvailableFormatOptions(): array
120+
{
121+
return [
122+
'base32',
123+
'base58',
124+
'rfc4122',
125+
];
126+
}
109127
}

src/Symfony/Component/Uid/Command/GenerateUuidCommand.php

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
namespace Symfony\Component\Uid\Command;
1313

1414
use Symfony\Component\Console\Command\Command;
15+
use Symfony\Component\Console\Completion\CompletionInput;
16+
use Symfony\Component\Console\Completion\CompletionSuggestions;
1517
use Symfony\Component\Console\Input\InputInterface;
1618
use Symfony\Component\Console\Input\InputOption;
1719
use Symfony\Component\Console\Output\ConsoleOutputInterface;
@@ -174,14 +176,14 @@ protected function execute(InputInterface $input, OutputInterface $output)
174176
break;
175177
}
176178

177-
switch ($input->getOption('format')) {
178-
case 'base32': $format = 'toBase32'; break;
179-
case 'base58': $format = 'toBase58'; break;
180-
case 'rfc4122': $format = 'toRfc4122'; break;
181-
default:
182-
$io->error(sprintf('Invalid format "%s", did you mean "base32", "base58" or "rfc4122"?', $input->getOption('format')));
179+
$formatOption = $input->getOption('format');
180+
181+
if (\in_array($formatOption, $this->getAvailableFormatOptions())) {
182+
$format = 'to'.ucfirst($formatOption);
183+
} else {
184+
$io->error(sprintf('Invalid format "%s", did you mean "base32", "base58" or "rfc4122"?', $formatOption));
183185

184-
return 1;
186+
return 1;
185187
}
186188

187189
$count = (int) $input->getOption('count');
@@ -197,4 +199,20 @@ protected function execute(InputInterface $input, OutputInterface $output)
197199

198200
return 0;
199201
}
202+
203+
public function complete(CompletionInput $input, CompletionSuggestions $suggestions): void
204+
{
205+
if ($input->mustSuggestOptionValuesFor('format')) {
206+
$suggestions->suggestValues($this->getAvailableFormatOptions());
207+
}
208+
}
209+
210+
private function getAvailableFormatOptions(): array
211+
{
212+
return [
213+
'base32',
214+
'base58',
215+
'rfc4122',
216+
];
217+
}
200218
}

src/Symfony/Component/Uid/Tests/Command/GenerateUlidCommandTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
namespace Symfony\Component\Uid\Tests\Command;
1313

1414
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Console\Application;
16+
use Symfony\Component\Console\Tester\CommandCompletionTester;
1517
use Symfony\Component\Console\Tester\CommandTester;
1618
use Symfony\Component\Uid\Command\GenerateUlidCommand;
1719
use Symfony\Component\Uid\Ulid;
@@ -100,4 +102,24 @@ public function testUlidsAreDifferentWhenGeneratingSeveralNow()
100102

101103
$this->assertNotSame($ulids[0], $ulids[1]);
102104
}
105+
106+
/**
107+
* @dataProvider provideCompletionSuggestions
108+
*/
109+
public function testComplete(array $input, array $expectedSuggestions)
110+
{
111+
$application = new Application();
112+
$application->add(new GenerateUlidCommand());
113+
$tester = new CommandCompletionTester($application->get('ulid:generate'));
114+
$suggestions = $tester->complete($input, 2);
115+
$this->assertSame($expectedSuggestions, $suggestions);
116+
}
117+
118+
public function provideCompletionSuggestions(): iterable
119+
{
120+
yield 'option --format' => [
121+
['--format', ''],
122+
['base32', 'base58', 'rfc4122'],
123+
];
124+
}
103125
}

src/Symfony/Component/Uid/Tests/Command/GenerateUuidCommandTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
namespace Symfony\Component\Uid\Tests\Command;
1313

1414
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Console\Application;
16+
use Symfony\Component\Console\Tester\CommandCompletionTester;
1517
use Symfony\Component\Console\Tester\CommandTester;
1618
use Symfony\Component\Uid\Command\GenerateUuidCommand;
1719
use Symfony\Component\Uid\Factory\UuidFactory;
@@ -229,4 +231,24 @@ public function testNamespacePredefinedKeyword()
229231

230232
$this->assertSame('9c7d0eda-982d-5708-b4bd-79b3b179725d', (string) Uuid::fromRfc4122(trim($commandTester->getDisplay())));
231233
}
234+
235+
/**
236+
* @dataProvider provideCompletionSuggestions
237+
*/
238+
public function testComplete(array $input, array $expectedSuggestions)
239+
{
240+
$application = new Application();
241+
$application->add(new GenerateUuidCommand());
242+
$tester = new CommandCompletionTester($application->get('uuid:generate'));
243+
$suggestions = $tester->complete($input, 2);
244+
$this->assertSame($expectedSuggestions, $suggestions);
245+
}
246+
247+
public function provideCompletionSuggestions(): iterable
248+
{
249+
yield 'option --format' => [
250+
['--format', ''],
251+
['base32', 'base58', 'rfc4122'],
252+
];
253+
}
232254
}

0 commit comments

Comments
 (0)