Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit baacd57

Browse filesBrowse files
committed
[Console] Updated section 'Building a single Command Application' with the new 'SingleCommandApplication'
1 parent e4d8832 commit baacd57
Copy full SHA for baacd57

File tree

1 file changed

+23
-17
lines changed
Filter options

1 file changed

+23
-17
lines changed

‎components/console/single_command_tool.rst

Copy file name to clipboardExpand all lines: components/console/single_command_tool.rst
+23-17Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,34 +5,36 @@ Building a single Command Application
55
=====================================
66

77
When building a command line tool, you may not need to provide several commands.
8-
In such case, having to pass the command name each time is tedious. Fortunately,
9-
it is possible to remove this need by declaring a single command application::
8+
In such case, having to pass the command name each time is tedious.
9+
10+
.. versionadded:: 5.1
11+
12+
The class :class:`Symfony\\Component\\Console\\SingleCommandApplication` was
13+
introduced in Symfony 5.1.
14+
15+
Fortunately, it is possible to remove this need by declaring a single command
16+
application::
1017

1118
#!/usr/bin/env php
1219
<?php
1320
require __DIR__.'/vendor/autoload.php';
1421

15-
use Symfony\Component\Console\Application;
1622
use Symfony\Component\Console\Input\InputArgument;
1723
use Symfony\Component\Console\Input\InputInterface;
1824
use Symfony\Component\Console\Input\InputOption;
1925
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
26+
use Symfony\Component\Console\SingleCommandApplication;
27+
28+
(new SingleCommandApplication())
29+
->setName('My Super Command') // Optional
30+
->setVersion('1.0.0') // Optional
31+
->addArgument('foo', InputArgument::OPTIONAL, 'The directory')
32+
->addOption('bar', null, InputOption::VALUE_REQUIRED)
33+
->setCode(function(InputInterface $input, OutputInterface $output) {
34+
// output arguments and options
35+
})
3036
->run();
3137

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-
3638
You can still register a command as usual::
3739

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

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

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.