Skip to content

[Console] Add support for invokable commands and input attributes #20553

Open
@chalasr

Description

@chalasr
Q A
Feature PR symfony/symfony#59340
PR author(s) @yceruto
Merged in 7.3

We created this issue to not forget to document this new feature. We would really appreciate if you can help us with this task. If you are not sure how to do it, please ask us and we will help you.

To fix this issue, please create a PR against the 7.3 branch in the symfony-docs repository.

Thank you! 😃

Activity

chalasr

chalasr commented on Jan 11, 2025

@chalasr
MemberAuthor

needs to be taken into account when tackling this:

added this to the 7.3 milestone on Jan 11, 2025
yceruto

yceruto commented on Jan 25, 2025

@yceruto
Member
tacman

tacman commented on Feb 13, 2025

@tacman
Contributor

Until the documentation is written, is there an example of a trivial command using input attributes? Thx.

chalasr

chalasr commented on Feb 13, 2025

@chalasr
MemberAuthor

@tacman You can find one in the code PR: symfony/symfony#59340

tacman

tacman commented on Feb 13, 2025

@tacman
Contributor

Here's a working example:

symfony new --version=next --webapp test-console7.3 && cd test-console7.3 
bin/console make:command lucky:number  

cat > src/Command/LuckyNumberCommand.php <<'END'
<?php

namespace App\Command;

use Symfony\Component\Console\Attribute\Argument;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Attribute\Option;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Style\SymfonyStyle;

#[AsCommand(name: 'lucky:number')]
class LuckyNumberCommand extends Command
{
    public function __construct(
    )
    {
        parent::__construct();
    }

    public function __invoke(SymfonyStyle $io,
                             #[Argument] string $name,
                             #[Option] bool $formal=true): void
    {
        $io->title(sprintf('%s %s!', $formal ? 'Hello' : 'Hey', ucfirst($name)));
        $io->success(sprintf('Today\'s Lucky Number: %d', rand(0, 400)));
    }
}
END
bin/console lucky:number bob
yceruto

yceruto commented on Mar 4, 2025

@yceruto
Member

I've started looking into this and have a question: should we replace the old signature everywhere or just add a new section for the new one?

OskarStark

OskarStark commented on Mar 4, 2025

@OskarStark
Contributor

I would just add a new one

alamirault

alamirault commented on Mar 18, 2025

@alamirault
Contributor

I think the new attribute approach will be the new standard right ?
(readonly and final classes, no inheritance, same DX than for controllers)

Newcomers to symfony shouldn't see it first ? (to see how powerfull is it)

yceruto

yceruto commented on Mar 19, 2025

@yceruto
Member

I’d go with @alamirault’s view on this, but let’s wait a bit to hear other opinions.

OskarStark

OskarStark commented on Mar 19, 2025

@OskarStark
Contributor

It is not feature complete now compared to the old way, so I would still propose the old version first.

@chalasr what do you think?

16 remaining items

Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    ConsolehasPRA Pull Request has already been submitted for this issue.

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

      Participants

      @javiereguiluz@garak@tacman@TomasVotruba@OskarStark

      Issue actions

        [Console] Add support for invokable commands and input attributes · Issue #20553 · symfony/symfony-docs