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] Allow callable forAsCommand,Argument andOption attributes arguments#61677

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
MacDada wants to merge5 commits intosymfony:7.4
base:7.4
Choose a base branch
Loading
fromMacDada:74_command_description

Conversation

MacDada
Copy link
Contributor

QA
Branch?7.4
Bug fix?no
New feature?yes
Deprecations?no
LicenseMIT

Given:

privateconstSOME_CONST ='foo';publicstaticfunctiongenerateTheArgumentDescription(){returnsprintf('The description is being generated with %s being a const.',self::SOME_CONST,    );}

With "old style" Commands I'm able to generate description of an Argument:

protectedfunctionconfigure():void{$this->addArgument('the-argument',        InputArgument::REQUIRED,self::generateTheArgumentDescription(),    );}

It's currently not possible with "Symfony Style":

publicfunction __invoke(SymfonyStyle$io,    #[Argument(// "Fatal error: Constant expression contains invalid operations"        description:self::generateTheArgumentDescription(),    )]string$theArgument,)

It becomes possible with this PR:

publicfunction __invoke(SymfonyStyle$io,    #[Argument(        description: [self::class,'generateTheArgumentDescription'],    )]string$theArgument,)

GromNaN reacted with thumbs up emoji
@GromNaN
Copy link
Member

An interesting option that allows the definition to be made more dynamic.

Then we should probably do the same for#[AsCommand]:description,help andusages.

With PHP 8.5, the named closure syntax can be used:

publicfunction __invoke(SymfonyStyle$io,    #[Argument(description:self::generateTheArgumentDescription(...))]string$theArgument,)

@MacDada
Copy link
ContributorAuthor

MacDada commentedSep 7, 2025
edited
Loading

Then we should probably do the same for#[AsCommand]:description,help andusages.

Not required in my codebase, but should be simple enough -> I can volunteer a new PR or extend this one if this is the way to go :-)

publicstring$name ='',
array|callable$suggestedValues = [],
) {
$this->description =\is_callable($description) ?$description() :$description;
Copy link
Member

Choose a reason for hiding this comment

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

I would apply the same approach we used for suggestedValues, so that non-static callables are supported. This way, we can access the$this instance and its dependencies if needed, for example, to build a description dynamically based on the command's dependencies

gassan reacted with thumbs up emoji
Copy link
ContributorAuthor

Choose a reason for hiding this comment

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

already tried that and failed, not sure why, guessingarray|callable (working as getting "in" as just array) vsstring|callable (not working, as not getting in as array is not a string, and not an acceptable callable)

will try doing it again tomorrow

Copy link
ContributorAuthor

Choose a reason for hiding this comment

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

Alright, I've tried to do that, but I've noticed a problem: I don't think I can do that without breaking compatibility.

As for now,$description is apublic string field:https://github.com/symfony/symfony/blob/v7.3.4/src/Symfony/Component/Console/Attribute/Argument.php#L39

I'd have to change it topublic string|\Closure, so if someone's code relies upon$description being a string, it would break it.

@nicolas-grekas
Copy link
Member

What's the use case? I prefer simple static data. A static call can only return static data anyway.

@MacDada
Copy link
ContributorAuthor

MacDada commentedSep 8, 2025
edited
Loading

What's the use case? I prefer simple static data. A static call can only return static data anyway.

Simple, I want the command to be more "user-friendly" by listing valid values in the argument description.

Yeah, sure, if the user passes an invalid value an error will be shown, but I don't want to encourage user to intentionally type invalid value just to discover what valid values are.

In my particular case, the valid values are from ENUM, so it is statically accessible. It just cannot be passed todescription, as PHP doesn't allow me that.


Given enum:

Screenshot 2025-09-08 at 15 00 10

Right now, with "old style"configure – all is dandy:

Screenshot 2025-09-08 at 14 54 18

Right now if I use "SymfonyStyle" – I'm missing allowed values list in the description:

Screenshot 2025-09-08 at 15 02 30

Right now, if I try to use "SymfonyStyle" – with the allowed values list in the description – error:

Screenshot 2025-09-08 at 15 07 48

If I move it to a static function – still an error:

Screenshot 2025-09-08 at 15 11 09

With this PR, itcould work – and give me the desired description with allowed values listed:

Screenshot 2025-09-08 at 15 13 22

To sum up: this PR would allow me to switch to "SymfonyStyle", instead of using "old style"configure, in order to display valid values of an Argument.

nicolas-grekas and chalasr reacted with thumbs up emoji


publicstaticfunctiongenerateEducationDescription():string
{
returnsprintf('Mention your favourite lectures from %s!',self::UNIVERSITY);

Choose a reason for hiding this comment

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

let's inline this, indirections makes reviewing test cases more complex

chalasr reacted with thumbs up emoji
Copy link
ContributorAuthor

Choose a reason for hiding this comment

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

i guess u meant to inline the const:71a08ac

@chalasr
Copy link
Member

@MacDada any chance you can finish this one soon? The feature freeze period is starting for 7.4.

MacDada added a commit to MacDada/symfony that referenced this pull requestSep 29, 2025
MacDada added a commit to MacDada/symfony that referenced this pull requestSep 29, 2025
@MacDada
Copy link
ContributorAuthor

MacDada commentedSep 29, 2025
edited
Loading

any chance you can finish this one soon?

@chalasr As for my own particular needs – this PR is finished.

BUT:

  1. There is a conflict on CHANGELOG – should I rebase and fix it? -> EDIT: rebased
  2. Should I squash the PR into one commit instead of it having multiple (with fixes)?
  3. Should Iextend it uponAsCommand as well? -> EDIT:done as much as I believe is doable
  4. I guess I should skipmaking it more dynamic?

MacDada added a commit to MacDada/symfony that referenced this pull requestSep 29, 2025
@MacDada
Copy link
ContributorAuthor

MacDada commentedSep 29, 2025
edited
Loading

Then we should probably do the same for#[AsCommand]:description,help andusages.

@GromNaN I've done it fordescription andhelp:5fd1f25

I didn't do it forusages as I guess it could be a "conflict" between normal array of strings and an array representing a callable… similar to this one:#61677 (comment) – but I don't know if there is a way to "fix" it here…

MacDada added a commit to MacDada/symfony that referenced this pull requestSep 29, 2025
MacDada added a commit to MacDada/symfony that referenced this pull requestSep 29, 2025
MacDada added a commit to MacDada/symfony that referenced this pull requestSep 29, 2025
@MacDadaMacDada changed the title[Console] Allow callable for$description ofArgument's andOption'sAttributes[Console] Allow callable forAsCommand,Argument andOption attributesSep 29, 2025
@MacDadaMacDada changed the title[Console] Allow callable forAsCommand,Argument andOption attributes[Console] Allow callable forAsCommand,Argument andOption attributes argumentsSep 29, 2025
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

@GromNaNGromNaNGromNaN left review comments

@ycerutoycerutoyceruto 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.

5 participants
@MacDada@GromNaN@nicolas-grekas@chalasr@yceruto

[8]ページ先頭

©2009-2025 Movatter.jp