Closed
Description
Description
Enums represent a closed list of possible values. Supporting backed enums as option or argument type would enable:
- Auto-completion from enum cases values (ie:
suggestedValues: BackedEnum::cases()
values) - Validation of the input value with a nice error message
- No need to call
BackedEnum::tryFrom($input)
in the command__invoke
.
In Symfony itself, most of the --format
options could be enums.
Example
Given a backed enum like this:
enum EmbeddingType: string
{
case Image = 'image';
case Description = 'description';
}
The argument parameter should accept a backed enum type,
#[AsCommand(name: 'app:regenerate')]
class RegenerateCommand
{
public function __invoke(#[Argument] EmbeddingType $type): int
{
$this->generator->generate($type);
}
}
If the command is run with an invalid value, an error is returned.
$ app/console app:regenerate yolo
The "yolo" value is not accepted. Use one of "image", "description".