Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[Console] SupportBackedEnum 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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
GromNaN wants to merge1 commit intosymfony:7.4
base:7.4
Choose a base branch
Loading
fromGromNaN:console-enum

Conversation

GromNaN
Copy link
Member

QA
Branch?7.4
Bug fix?no
New feature?yes
Deprecations?no
IssuesFix#60433
LicenseMIT
  • Convert automatically from the string input into the backed enum value usingBackedEnum::from($value)
  • Display a nice error message when the input value is not compatible with the typed enum
  • UseBackedEnum::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 EnumCommandextends Command{publicfunction__invoke(OutputInterface$output,        #[Argument]StringEnum$string,        #[Option] ?IntEnum$int =null,    ) {$output->writeln($string->value ??'No value');$output->writeln($int->value ??'No value');return0;    }}

Usage:
image

Error with invalidargument value:
image

Error with invalidoption value:
image

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

Choose a reason for hiding this comment

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

TheOption attribute already has this same property.

Copy link
Member

@nicolas-grekasnicolas-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
MemberAuthor

@GromNaNGromNaN 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 freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@nicolas-grekasnicolas-grekasnicolas-grekas left review comments

@OskarStarkOskarStarkOskarStark left review comments

@chalasrchalasrAwaiting requested review from chalasrchalasr is a code owner

Assignees
No one assigned
Projects
None yet
Milestone
7.4
Development

Successfully merging this pull request may close these issues.

[Console] Support backed enums for invokable command as argument and option
4 participants
@GromNaN@nicolas-grekas@OskarStark@carsonbot

[8]ページ先頭

©2009-2025 Movatter.jp