Skip to content

Commit 6db5f23

Browse files
committed
feature #3426 New Feature: Change the Default Command in the Console component (danielcsgomes)
This PR was merged into the master branch. Discussion ---------- New Feature: Change the Default Command in the Console component | Q | A | ------------- | --- | Doc fix? | no | New docs? | yes (symfony/symfony#9776) | Applies to | 2.5+ | Fixed tickets | I followed the suggestion from @wouterj in IRC on changing the `single_command_tool` and added the documentation for the new feature there. Commits ------- c1b2aad Applied suggestions from Ryan 5e97202 Applyed suggestions from @fabpot and @stof c23f34e Applied some suggestions 012456d Moved `versionadded` to the right section 730985f Updated references to the new document af9eac4 Changed the code to remove references to Symfony Framework since it's the standalone component 60e2b3e Added the delete document to avoid broken urls and added a notice that the document was moved to another location 11c7174 Added the version number where the setDefaultCommand was introduced b29ab89 Documented the Change the Default Command in the Console component
2 parents 0f90fed + c1b2aad commit 6db5f23

File tree

5 files changed

+71
-1
lines changed

5 files changed

+71
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
.. index::
2+
single: Console; Changing the Default Command
3+
4+
Changing the Default Command
5+
============================
6+
7+
.. versionadded:: 2.5,
8+
The :method:`Symfony\\Component\\Console\\Application::setDefaultCommand`
9+
method was introduced in version 2.5.
10+
11+
will always run the ``ListCommand`` when no command name is passed. In order to change
12+
the default command you just need to pass the command name you want to run by
13+
default to the ``setDefaultCommand`` method::
14+
15+
namespace Acme\Command;
16+
17+
use Symfony\Component\Console\Command\Command;
18+
use Symfony\Component\Console\Input\InputInterface;
19+
use Symfony\Component\Console\Output\OutputInterface;
20+
21+
class HelloWorldCommand extends Command
22+
{
23+
protected function configure()
24+
{
25+
$this->setName('hello:world')
26+
->setDescription('Outputs \'Hello World\'');
27+
}
28+
29+
protected function execute(InputInterface $input, OutputInterface $output)
30+
{
31+
$output->writeln('Hello World');
32+
}
33+
}
34+
35+
Executing the application and changing the default Command::
36+
37+
// application.php
38+
39+
use Acme\Command\HelloWorldCommand;
40+
use Symfony\Component\Console\Application;
41+
42+
$command = new HelloWorldCommand();
43+
$application = new Application();
44+
$application->add($command);
45+
$application->setDefaultCommand($command->getName());
46+
$application->run();
47+
48+
Test the new default console command by running the following:
49+
50+
.. code-block:: bash
51+
52+
$ php application.php
53+
54+
This will print the following to the command line:
55+
56+
.. code-block:: text
57+
58+
Hello Fabien
59+
60+
.. tip::
61+
62+
This feature has a limitation: you cannot use it with any Command arguments.
63+
64+
Learn More!
65+
-----------
66+
67+
* :doc:`/components/console/single_command_tool`

components/console/index.rst

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

77
introduction
88
usage
9+
changing_default_command
910
single_command_tool
1011
events
1112
helpers/index

components/console/introduction.rst

+1
Original file line numberDiff line numberDiff line change
@@ -526,6 +526,7 @@ Learn More!
526526

527527
* :doc:`/components/console/usage`
528528
* :doc:`/components/console/single_command_tool`
529+
* :doc:`/components/console/changing_default_command`
529530

530531
.. _Packagist: https://packagist.org/packages/symfony/console
531532
.. _ANSICON: https://github.com/adoxa/ansicon/downloads

components/console/single_command_tool.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
.. index::
2-
single: Console; Single command application
2+
single: Console; Single command application
33

44
Building a Single Command Application
55
=====================================

components/map.rst.inc

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
* :doc:`/components/console/introduction`
2121
* :doc:`/components/console/usage`
2222
* :doc:`/components/console/single_command_tool`
23+
* :doc:`/components/console/changing_default_command`
2324
* :doc:`/components/console/events`
2425
* :doc:`/components/console/helpers/index`
2526

0 commit comments

Comments
 (0)