Skip to content

Commit

Permalink
Harmonize command formats and ensure autocompletion is same
Browse files Browse the repository at this point in the history
  • Loading branch information
alamirault authored and fabpot committed Apr 8, 2023
1 parent 393bf72 commit 01b0f20
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 12 deletions.
23 changes: 13 additions & 10 deletions Command/GenerateUlidCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,6 @@
#[AsCommand(name: 'ulid:generate', description: 'Generate a ULID')]
class GenerateUlidCommand extends Command
{
private const FORMAT_OPTIONS = [
'base32',
'base58',
'rfc4122',
];

private UlidFactory $factory;

public function __construct(UlidFactory $factory = null)
Expand All @@ -46,7 +40,7 @@ protected function configure(): void
->setDefinition([
new InputOption('time', null, InputOption::VALUE_REQUIRED, 'The ULID timestamp: a parsable date/time string'),
new InputOption('count', 'c', InputOption::VALUE_REQUIRED, 'The number of ULID to generate', 1),
new InputOption('format', 'f', InputOption::VALUE_REQUIRED, 'The ULID output format: base32, base58 or rfc4122', 'base32'),
new InputOption('format', 'f', InputOption::VALUE_REQUIRED, sprintf('The ULID output format ("%s")', implode('", "', $this->getAvailableFormatOptions())), 'base32'),
])
->setHelp(<<<'EOF'
The <info>%command.name%</info> command generates a ULID.
Expand Down Expand Up @@ -85,10 +79,10 @@ protected function execute(InputInterface $input, OutputInterface $output): int

$formatOption = $input->getOption('format');

if (\in_array($formatOption, self::FORMAT_OPTIONS)) {
if (\in_array($formatOption, $this->getAvailableFormatOptions())) {
$format = 'to'.ucfirst($formatOption);
} else {
$io->error(sprintf('Invalid format "%s", did you mean "base32", "base58" or "rfc4122"?', $input->getOption('format')));
$io->error(sprintf('Invalid format "%s", supported formats are "%s".', $formatOption, implode('", "', $this->getAvailableFormatOptions())));

return 1;
}
Expand All @@ -110,7 +104,16 @@ protected function execute(InputInterface $input, OutputInterface $output): int
public function complete(CompletionInput $input, CompletionSuggestions $suggestions): void
{
if ($input->mustSuggestOptionValuesFor('format')) {
$suggestions->suggestValues(self::FORMAT_OPTIONS);
$suggestions->suggestValues($this->getAvailableFormatOptions());
}
}

private function getAvailableFormatOptions(): array
{
return [
'base32',
'base58',
'rfc4122',
];
}
}
4 changes: 2 additions & 2 deletions Command/GenerateUuidCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ protected function configure(): void
new InputOption('namespace', null, InputOption::VALUE_REQUIRED, 'The UUID to use at the namespace for named-based UUIDs, predefined namespaces keywords "dns", "url", "oid" and "x500" are accepted'),
new InputOption('random-based', null, InputOption::VALUE_NONE, 'To generate a random-based UUID'),
new InputOption('count', 'c', InputOption::VALUE_REQUIRED, 'The number of UUID to generate', 1),
new InputOption('format', 'f', InputOption::VALUE_REQUIRED, 'The UUID output format: rfc4122, base58 or base32', 'rfc4122'),
new InputOption('format', 'f', InputOption::VALUE_REQUIRED, sprintf('The UUID output format ("%s")', implode('", "', $this->getAvailableFormatOptions())), 'rfc4122'),
])
->setHelp(<<<'EOF'
The <info>%command.name%</info> generates a UUID.
Expand Down Expand Up @@ -171,7 +171,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
if (\in_array($formatOption, $this->getAvailableFormatOptions())) {
$format = 'to'.ucfirst($formatOption);
} else {
$io->error(sprintf('Invalid format "%s", did you mean "base32", "base58" or "rfc4122"?', $formatOption));
$io->error(sprintf('Invalid format "%s", supported formats are "%s".', $formatOption, implode('", "', $this->getAvailableFormatOptions())));

return 1;
}
Expand Down

0 comments on commit 01b0f20

Please sign in to comment.