Skip to content

Commit f273ae8

Browse files
committed
[Console] Updated section 'Building a single Command Application' with the new 'SingleCommandApplication'
1 parent e4d8832 commit f273ae8

File tree

1 file changed

+19
-15
lines changed

1 file changed

+19
-15
lines changed

components/console/single_command_tool.rst

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,31 +8,31 @@ When building a command line tool, you may not need to provide several commands.
88
In such case, having to pass the command name each time is tedious. Fortunately,
99
it is possible to remove this need by declaring a single command application::
1010

11+
.. versionadded:: 5.0
12+
13+
The class :class:`Symfony\\Component\\Console\\SingleCommandApplication` was
14+
introduced in Symfony 5.1.
15+
1116
#!/usr/bin/env php
1217
<?php
1318
require __DIR__.'/vendor/autoload.php';
1419

15-
use Symfony\Component\Console\Application;
1620
use Symfony\Component\Console\Input\InputArgument;
1721
use Symfony\Component\Console\Input\InputInterface;
1822
use Symfony\Component\Console\Input\InputOption;
1923
use Symfony\Component\Console\Output\OutputInterface;
20-
21-
(new Application('echo', '1.0.0'))
22-
->register('echo')
23-
->addArgument('foo', InputArgument::OPTIONAL, 'The directory')
24-
->addOption('bar', null, InputOption::VALUE_REQUIRED)
25-
->setCode(function(InputInterface $input, OutputInterface $output) {
26-
// output arguments and options
27-
})
28-
->getApplication()
29-
->setDefaultCommand('echo', true) // Single command application
24+
use Symfony\Component\Console\SingleCommandApplication;
25+
26+
(new SingleCommandApplication())
27+
->setName('My Super Command') // Optional
28+
->setVersion('1.0.0') // Optional
29+
->addArgument('foo', InputArgument::OPTIONAL, 'The directory')
30+
->addOption('bar', null, InputOption::VALUE_REQUIRED)
31+
->setCode(function(InputInterface $input, OutputInterface $output) {
32+
// output arguments and options
33+
})
3034
->run();
3135

32-
The method :method:`Symfony\\Component\\Console\\Application::setDefaultCommand`
33-
accepts a boolean as second parameter. If true, the command ``echo`` will then
34-
always be used, without having to pass its name.
35-
3636
You can still register a command as usual::
3737

3838
#!/usr/bin/env php
@@ -49,3 +49,7 @@ You can still register a command as usual::
4949

5050
$application->setDefaultCommand($command->getName(), true);
5151
$application->run();
52+
53+
The method :method:`Symfony\\Component\\Console\\Application::setDefaultCommand`
54+
accepts a boolean as second parameter. If true, the command ``echo`` will then
55+
always be used, without having to pass its name.

0 commit comments

Comments
 (0)