codebase.watch what is new in your dependencies

CommandChain

At a glance

Type
New feature
CommandChain added
8.2

Since symfony/console 8.2, a command run as a sub-command reads the commands resolved from the command line and the input of each level with CommandChain.

The command receives it as a parameter of its code, or gets it with Application::getCommandChain().

Input of a parent level

A sub-command does not inherit the options of its parents: it reads the input of each level from CommandChain.

Option of the parent level

use Symfony\Component\Console\Application;
use Symfony\Component\Console\CommandChain;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\ArgvInput;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

$application = new Application();

// The foo command is registered only to declare the --name option of its level, without code
$application
    ->register('foo')
    ->addOption('name', mode: InputOption::VALUE_REQUIRED);

$application
    ->register('foo:bar:baz')
    ->setCode(function (CommandChain $chain, OutputInterface $output): int {
        $output->writeln($chain->getInput('foo')?->getOption('name'));

        return Command::SUCCESS;
    });

// qux
$application->run(
    new ArgvInput(
        // The option given after foo is parsed by this command, then foo:bar:baz runs and reads it
        ['bin/console', 'foo', '--name=qux', 'bar', 'baz']
    )
);

Name with colons

With the name separated by colons, the parent is not part of the chain.

use Symfony\Component\Console\Application;
use Symfony\Component\Console\CommandChain;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\ArgvInput;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

$application = new Application();

$application
    ->register('foo')
    ->addOption('name', mode: InputOption::VALUE_REQUIRED);

$application
    ->register('foo:bar:baz')
    ->setCode(function (CommandChain $chain, OutputInterface $output): int {
        $output->writeln($chain->getInput('foo')?->getOption('name') ?? 'Parent not found');

        return Command::SUCCESS;
    });

// Parent not found
$application->run(new ArgvInput(['bin/console', 'foo:bar:baz']));

Resolved commands

Commands of the command line

The chain lists the registered commands of the levels, from the root to the running command: bar, a level without a command, is not part of it.

use Symfony\Component\Console\Application;
use Symfony\Component\Console\CommandChain;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\ArgvInput;
use Symfony\Component\Console\Output\OutputInterface;

$application = new Application();

$application->register('foo');

$application
    ->register('foo:bar:baz')
    ->setCode(function (CommandChain $chain, OutputInterface $output): int {
        $output->writeln(array_map(
            fn (Command $command): string => $command->getName(),
            $chain->getCommands()
        ));

        return Command::SUCCESS;
    });

// foo
// foo:bar:baz
$application->run(new ArgvInput(['bin/console', 'foo', 'bar', 'baz']));

Outside the command

Chain read by a listener

While a command runs, Application::getCommandChain() returns the same chain, e.g. to a listener of ConsoleEvents::COMMAND.

use Symfony\Component\Console\Application;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\ConsoleEvents;
use Symfony\Component\Console\Event\ConsoleCommandEvent;
use Symfony\Component\Console\Input\ArgvInput;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\EventDispatcher\EventDispatcher;

$application = new Application();

$dispatcher = new EventDispatcher();
$dispatcher->addListener(
    ConsoleEvents::COMMAND,
    function (ConsoleCommandEvent $event) use ($application): void {
        $event->getOutput()->writeln(
            $application->getCommandChain()?->getInput('foo')?->getOption('name')
        );
    }
);
$application->setDispatcher($dispatcher);

$application
    ->register('foo')
    ->addOption('name', mode: InputOption::VALUE_REQUIRED);

$application
    ->register('foo:bar:baz')
    ->setCode(fn (): int => Command::SUCCESS);

// qux
$application->run(
    new ArgvInput(
        ['bin/console', 'foo', '--name=qux', 'bar', 'baz']
    )
);

The sources