Skip to content

[Console] Add broader support for command "help" definition #59473

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

Merged
merged 1 commit into from
Jan 20, 2025

Conversation

yceruto
Copy link
Member

@yceruto yceruto commented Jan 10, 2025

Q A
Branch? 7.3
Bug fix? no
New feature? yes
Deprecations? no
Issues -
License MIT

Follow up #59340

Invokable and regular commands can now define the command help content via the #[AsCommand] attribute.

This is particularly useful for invokable commands, as it avoids the need to extend the Command class.

#[AsCommand(
    name: 'user:create',
    description: 'Create a new user',
    help: <<<TXT
The <info>%command.name%</info> command generates a new user class for security
and updates your security.yaml file for it. It will also generate a user provider
class if your situation needs a custom class.

<info>php %command.full_name% email</info>

If the argument is missing, the command will ask for the class name interactively.
TXT
)]
class CreateUserCommand
{
    public function __invoke(SymfonyStyle $io, #[Argument] string $email): int
    {
        // ...
    }
}

Cheers!

@yceruto yceruto requested a review from kbond January 10, 2025 21:20
Copy link
Member

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

I'm worried that these texts will be unnecessarily duplicated when the container is compiled.

@yceruto yceruto force-pushed the invokable_command_help branch 4 times, most recently from bb48d97 to 92d279b Compare January 10, 2025 22:33
@yceruto
Copy link
Member Author

yceruto commented Jan 10, 2025

I'm worried that these texts will be unnecessarily duplicated when the container is compiled.

It's already the same case for name and description when configured via tag; I don't see a problem here.

Copy link
Member

@chalasr chalasr left a comment

Choose a reason for hiding this comment

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

That was on my list, thanks!

@yceruto
Copy link
Member Author

yceruto commented Jan 11, 2025

Just rebased and conflicts solved.

@kbond
Copy link
Member

kbond commented Jan 11, 2025

This wasn't on my radar but good call!

Copy link
Contributor

@OskarStark OskarStark left a comment

Choose a reason for hiding this comment

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

Like it 🖖

Copy link
Member

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

I'm worried that these texts will be unnecessarily duplicated when the container is compiled.

It's already the same case for name and description when configured via tag; I don't see a problem here.

The help text can be large, the duplication in the dumped container is only for commands using the attribute to declare it (twice in the XML and once in the PHP).

@@ -100,6 +100,10 @@ public function __construct(?string $name = null)
$this->setDescription(static::getDefaultDescription() ?? '');
}

if ('' === $this->help && $attributes = (new \ReflectionClass(static::class))->getAttributes(AsCommand::class)) {
$this->setHelp($attributes[0]->newInstance()->help ?? '');
}
Copy link
Member

Choose a reason for hiding this comment

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

we should either add addDefaultHelp or deprecate getDefaultName/Description to be consistent

Copy link
Member Author

@yceruto yceruto Jan 13, 2025

Choose a reason for hiding this comment

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

The name and description static methods remain useful for lazy commands, but this approach is not necessary for help. Additionally, introducing a new getDefaultHelp() method seems redundant since it's already possible to override getHelp() when customization is needed.

Copy link
Member Author

Choose a reason for hiding this comment

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

would you prefer adding a private static method for getDefaultHelp() purely for the sake of consistency?

Copy link
Member

Choose a reason for hiding this comment

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

The name and description static methods remain useful for lazy commands

Are they really? I miss a case where it cannot be found in the tag and #[AsCommand]

Copy link
Member Author

Choose a reason for hiding this comment

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

Hmm, I think I missed the fact that the AsCommand attribute is always auto-configured, so yes! they can now be deprecated. I'll make the changes in another PR, as it's not directly related to the feature proposed here.

@yceruto yceruto force-pushed the invokable_command_help branch from ccf8c15 to 4a51788 Compare January 17, 2025 13:27
@yceruto yceruto force-pushed the invokable_command_help branch from 4a51788 to bbd806b Compare January 20, 2025 13:13
@yceruto
Copy link
Member Author

yceruto commented Jan 20, 2025

rebased and conflicts solved.

@chalasr
Copy link
Member

chalasr commented Jan 20, 2025

PR welcome to solve #59473 (comment) (deprecations) 🙏

@chalasr chalasr force-pushed the invokable_command_help branch from bbd806b to e9a6b0a Compare January 20, 2025 13:23
@chalasr
Copy link
Member

chalasr commented Jan 20, 2025

Thank you @yceruto.

@chalasr chalasr merged commit e348b70 into symfony:7.3 Jan 20, 2025
8 of 10 checks passed
@yceruto yceruto deleted the invokable_command_help branch January 20, 2025 13:31
fabpot added a commit that referenced this pull request Jan 25, 2025
…faultDescription methods (yceruto)

This PR was merged into the 7.3 branch.

Discussion
----------

[Console] Deprecating Command getDefaultName and getDefaultDescription methods

| Q             | A
| ------------- | ---
| Branch?       | 7.3
| Bug fix?      | no
| New feature?  | no
| Deprecations? | yes
| Issues        | -
| License       | MIT

related discussion #59473 (comment)

Commits
-------

b529726 Deprecating command getDefault* methods
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.

9 participants