Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
GromNaN opened this issue May 16, 2025 · 5 comments
Open
Labels
Console RFC RFC = Request For Comments (proposals about features that you want to be discussed)

Comments

@GromNaN
Copy link
Member

GromNaN commented May 16, 2025

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

@GromNaN GromNaN added the RFC RFC = Request For Comments (proposals about features that you want to be discussed) label May 16, 2025
@GromNaN
Copy link
Member Author

GromNaN commented May 16, 2025

We can modify the argument and option definition classes to add a $values property that would accept an array of scalars or a backed enum.

@stof
Copy link
Member

stof commented May 16, 2025

For backed enums, I don't think we should need to add anything in the attributes. This can be inferred from the parameter type.

@GromNaN
Copy link
Member Author

GromNaN commented May 16, 2025

For backed enums, I don't think we should need to add anything in the attributes. This can be inferred from the parameter type.

Sure, my comment was if we want to support backed enum in regular command definition. Which is an other topic.

@chalasr
Copy link
Member

chalasr commented May 16, 2025

Object support will come with value resolvers

@stof
Copy link
Member

stof commented May 16, 2025

@chalasr this won't provide automatic completion for backed enum values if we don't have a special handling for them.

But anyway, we have 5 months to figure out the solution for Symfony 7.4.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Console RFC RFC = Request For Comments (proposals about features that you want to be discussed)
Projects
None yet
Development

No branches or pull requests

4 participants