Skip to content

[Console] Support BackedEnum in invokable commands #60586

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
wants to merge 1 commit into
base: 7.4
Choose a base branch
from

Conversation

GromNaN
Copy link
Member

@GromNaN GromNaN commented May 29, 2025

Q A
Branch? 7.4
Bug fix? no
New feature? yes
Deprecations? no
Issues Fix #60433
License MIT
  • Convert automatically from the string input into the backed enum value using BackedEnum::from($value)
  • Display a nice error message when the input value is not compatible with the typed enum
  • Use BackedEnum::cases() to provide autocompletion. Also part of the error message.

Example

Given this 2 backed enums

enum StringEnum: string
{
    case Image = 'image';
    case Video = 'video';
}

enum IntEnum: int
{
    case First = 1;
    case Second = 2;
}

We declare this command:

#[AsCommand('enum')]
class EnumCommand extends Command
{
    public function __invoke(
        OutputInterface $output,
        #[Argument] StringEnum $string,
        #[Option] ?IntEnum $int = null,
    ) {
        $output->writeln($string->value ?? 'No value');
        $output->writeln($int->value ?? 'No value');

        return 0;
    }
}

Usage:
image

Error with invalid argument value:
image

Error with invalid option value:
image

@@ -27,6 +28,7 @@ class Argument
private array|\Closure $suggestedValues;
private ?int $mode = null;
private string $function = '';
private string $typeName = '';
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Option attribute already has this same property.

Copy link
Member

@nicolas-grekas nicolas-grekas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sweet :)

Copy link
Member Author

@GromNaN GromNaN left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the for the review, the code looks better like this.

Co-authored-by: Nicolas Grekas <nicolas.grekas@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Console] Support backed enums for invokable command as argument and option
4 participants
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy