codebase.watch what is new in your dependencies

Sub-commands: foo bar runs foo:bar

At a glance

Type
New feature
Sub-commands added
8.2

A command name is made of levels separated by colons: foo:bar:baz has three levels, foo, bar and baz.

Before 8.2, the command line had to give the whole name with its colons: bin/console foo bar failed with Command "foo" is not defined.

Since symfony/console 8.2, the levels can also be separated by spaces, like docker compose up or git remote add.

A level is a registered command, which can declare options, or only a namespace, like bar.

The words of the command line are read level by level: the options after a level belong to the command of this level.

Only the command of the last level runs: the commands of the levels before it only parse their options.

The command of the last level reads the options of the other levels with CommandChain.

bin/console foo --name=qux bar baz
            │   │          │   └─ baz: the command foo:bar:baz, which runs
            │   │          └─ bar: a namespace, without a command
            │   └─ --name=qux: an option of the command foo
            └─ foo: a registered command, without code

Spaced sub-commands

Namespace as a level

A namespace is a level of the tree, even without a command of its own.

use Symfony\Component\Console\Application;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\ArgvInput;

$application = new Application();

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

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

Parent without code

Parent run alone

A command without code of its own lists its sub-commands after the help of the application, on the error output, like a namespace.

use Symfony\Component\Console\Application;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\ArgvInput;

$application = new Application();

// Command "foo", without code
$application->register('foo');

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

// Available commands for the "foo" namespace:
//   foo:bar:baz
// Exit code: 1
$application->run(new ArgvInput(['bin/console', 'foo']));

Help of a sub-command

--help of the last level

use Symfony\Component\Console\Application;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\ArgvInput;

$application = new Application();

$application->register('foo');

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

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

Sub-command before an argument

Argument named like a sub-command

When a command has an argument and sub-commands, a sub-command always takes precedence over the value of the argument.

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

$application = new Application();

// Command "foo", with an argument "target"
$application
    ->register('foo')
    ->addArgument('target', InputArgument::REQUIRED)
    ->setCode(function (InputInterface $input, OutputInterface $output): int {
        $output->writeln('Value of the argument target: ' . $input->getArgument('target'));

        return Command::SUCCESS;
    });

// Sub-command "foo:bar": "bar" is also the value given to the argument "target" of "foo"
$application
    ->register('foo:bar')
    ->setCode(function (): int {
        throw new RuntimeException('foo:bar has been executed');
    });

// 5.1 – 8.1: Value of the argument target: bar
// >= 8.2: RuntimeException: foo:bar has been executed
$application->run(new ArgvInput(['bin/console', 'foo', 'bar']));
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\ArgvInput;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

$application = new Application();

// Command "foo", with an argument "target"
$application
    ->register('foo')
    ->addArgument('target', InputArgument::REQUIRED)
    ->setCode(function (InputInterface $input, OutputInterface $output): int {
        $output->writeln('Value of the argument target: ' . $input->getArgument('target'));

        return Command::SUCCESS;
    });

// Sub-command "foo:bar": "bar" is also the value given to the argument "target" of "foo"
$application
    ->register('foo:bar')
    ->setCode(fn (): int => throw new RuntimeException('foo:bar has been executed'));

// 5.1 – 8.1: Value of the argument target: bar
// >= 8.2: RuntimeException: foo:bar has been executed
$application->run(new ArgvInput(['bin/console', 'foo', 'bar']));

Value after --

A value after -- is given to the argument, even when it is the name of a sub-command.

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

$application = new Application();

// Command "foo", with an argument "target": the command run
$application
    ->register('foo')
    ->addArgument('target', InputArgument::REQUIRED)
    ->setCode(function (InputInterface $input, OutputInterface $output): int {
        $output->writeln('Value of the argument target: ' . $input->getArgument('target'));

        return Command::SUCCESS;
    });

// Sub-command "foo:bar", never run: "bar" is also the value given to the argument "target" of "foo"
$application
    ->register('foo:bar')
    ->setCode(fn (): int => throw new RuntimeException('foo:bar has been executed'));

// Value of the argument target: bar
$application->run(new ArgvInput(['bin/console', 'foo', '--', 'bar']));

Tests with ArrayInput

Positional values

The values of an ArrayInput without a key fill the levels and the arguments in their order.

use Symfony\Component\Console\Application;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\OutputInterface;

$application = new Application();

$application
    ->register('foo:bar')
    ->setCode(function (OutputInterface $output): int {
        $output->writeln('Command has been executed.');

        return Command::SUCCESS;
    });

// Command has been executed.
$application->run(new ArrayInput(['foo', 'bar']));

The sources