Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Console] Support backed enums for invokable command as argument and option #60433

Closed
@GromNaN

Description

@GromNaN

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".
                                       

Metadata

Metadata

Assignees

No one assigned

    Labels

    ConsoleRFCRFC = Request For Comments (proposals about features that you want to be discussed)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions