Skip to content

Building a single Command Application

Edit this page

When building a command line tool, you may not need to provide several commands. In such a case, having to pass the command name each time is tedious. Fortunately, it is possible to remove this need by declaring a single command application:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#!/usr/bin/env php
<?php
require __DIR__.'/vendor/autoload.php';

use Symfony\Component\Console\Attribute\Argument;
use Symfony\Component\Console\Attribute\Option;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\SingleCommandApplication;

(new SingleCommandApplication())
    ->setName('My Super Command') // Optional
    ->setVersion('1.0.0') // Optional
    ->setCode(function (OutputInterface $output, #[Argument] string $foo = 'The directory', #[Option] string $bar = ''): int {
        // output arguments and options

        return 0;
    })
    ->run();

You can still register a command as usual:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#!/usr/bin/env php
<?php
require __DIR__.'/vendor/autoload.php';

use Acme\Command\DefaultCommand;
use Symfony\Component\Console\Application;

$application = new Application('echo', '1.0.0');
$command = new DefaultCommand();

$application->add($command);

$application->setDefaultCommand($command->getName(), true);
$application->run();

The setDefaultCommand() method accepts a boolean as second parameter. If true, the command echo will then always be used, without having to pass its name.

This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.
TOC
    Version