Skip to content

Commit b29ab89

Browse files
committed
Documented the Change the Default Command in the Console component
1 parent b1e0886 commit b29ab89

File tree

2 files changed

+44
-3
lines changed

2 files changed

+44
-3
lines changed

components/console/single_command_tool.rst renamed to components/console/changing_default_behavior.rst

+43-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,49 @@
11
.. index::
2-
single: Console; Single command application
2+
single: Console; Changing the Default Behavior
3+
4+
Changing the Default Behavior
5+
=============================
6+
7+
When building a command line tool, you may need to customize it to fit your needs.
8+
Probably you want to change the Default Command that the Application runs or
9+
maybe you just want to run a Single Command instead of have to pass the command
10+
name each time. Fortunately it is possible to do both.
11+
12+
Changing the Default Command
13+
----------------------------
14+
15+
By default the Application will always run the ListCommand. In order to change
16+
the default command you just need to pass the command name you want to run by
17+
default to the :method:`Symfony\\Component\\Console\\Application::setDefaultCommand`
18+
method::
19+
20+
#!/usr/bin/env php
21+
<?php
22+
// app/console
23+
24+
use Acme\DemoBundle\Command\GreetCommand;
25+
use Symfony\Component\Console\Application;
26+
27+
$command = new GreetCommand();
28+
$application = new Application();
29+
$application->add($command);
30+
$application->setDefaultCommand($command->getName());
31+
$application->run()
32+
33+
Test the new console command by running the following
34+
35+
.. code-block:: bash
36+
37+
$ app/console Fabien
38+
39+
This will print the following to the command line:
40+
41+
.. code-block:: text
42+
43+
Hello Fabien
344
445
Building a Single Command Application
5-
=====================================
46+
-------------------------------------
647

748
When building a command line tool, you may not need to provide several commands.
849
In such case, having to pass the command name each time is tedious. Fortunately,

components/console/index.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@ Console
66

77
introduction
88
usage
9-
single_command_tool
9+
changing_default_behavior
1010
events
1111
helpers/index

0 commit comments

Comments
 (0)