Skip to content

Commit 6089869

Browse files
committed
Added a cookbook entry about building single command applications
This documents the way to achieve the use case requested in symfony/symfony#3857
1 parent 6e69f44 commit 6089869

File tree

3 files changed

+50
-0
lines changed

3 files changed

+50
-0
lines changed

cookbook/console/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ Console
77
console_command
88
usage
99
generating_urls
10+
single_command_tool
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
.. index::
2+
single: Console; Single command application
3+
4+
How to build an Application with a single Command
5+
=================================================
6+
7+
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 extending the application::
10+
11+
namespace Acme\Tool;
12+
13+
use Symfony\Component\Console\Application;
14+
use Symfony\Component\Console\Input\InputInterface;
15+
16+
class MyApplication extends Application
17+
{
18+
/**
19+
* Gets the name of the command based on input.
20+
*
21+
* @param InputInterface $input The input interface
22+
*
23+
* @return string The command name
24+
*/
25+
protected function getCommandName(InputInterface $input)
26+
{
27+
return 'my_command';
28+
}
29+
30+
/**
31+
* Gets the default commands that should always be available.
32+
*
33+
* @return array An array of default Command instances
34+
*/
35+
protected function getDefaultCommands()
36+
{
37+
// Keep the core default commands to have the HelpCommand
38+
// which is used when using the --help option
39+
$defaultCommands = parent::getDefaultCommands()
40+
41+
$defaultCommands[] = new MyCommand();
42+
43+
return $defaultCommands;
44+
}
45+
}
46+
47+
When calling your console script, the command `MyCommand` will then always
48+
be used, without having to pass its name.

cookbook/map.rst.inc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
* :doc:`/cookbook/console/console_command`
3030
* :doc:`/cookbook/console/usage`
3131
* :doc:`/cookbook/console/generating_urls`
32+
* :doc:`/cookbook/console/single_command_tool`
3233

3334
* :doc:`/cookbook/controller/index`
3435

0 commit comments

Comments
 (0)