Skip to content

Commit 5e97202

Browse files
committed
Applyed suggestions from @fabpot and @stof
1 parent c23f34e commit 5e97202

5 files changed

+140
-119
lines changed

components/console/changing_default_behavior.rst

-116
This file was deleted.
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+
By default the Application will always run the ``ListCommand``. 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+
The feature was a limitation since you cannot use the Command ``arguments``.
63+
64+
Learn More!
65+
-----------
66+
67+
* :doc:`/components/console/single_command_tool`

components/console/index.rst

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

77
introduction
88
usage
9-
changing_default_behavior
9+
changing_default_command
10+
single_command_tool
1011
events
1112
helpers/index

components/console/introduction.rst

+2-1
Original file line numberDiff line numberDiff line change
@@ -525,7 +525,8 @@ Learn More!
525525
-----------
526526

527527
* :doc:`/components/console/usage`
528-
* :doc:`/components/console/changing_default_behavior`
528+
* :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
+69-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,72 @@
1+
.. index::
2+
single: Console; Single command application
3+
14
Building a Single Command Application
25
=====================================
36

4-
This Document was moved to :doc:`/components/console/changing_default_behaviour`
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+
// This should return the name of your command.
28+
return 'my_command';
29+
}
30+
31+
/**
32+
* Gets the default commands that should always be available.
33+
*
34+
* @return array An array of default Command instances
35+
*/
36+
protected function getDefaultCommands()
37+
{
38+
// Keep the core default commands to have the HelpCommand
39+
// which is used when using the --help option
40+
$defaultCommands = parent::getDefaultCommands();
41+
42+
$defaultCommands[] = new MyCommand();
43+
44+
return $defaultCommands;
45+
}
46+
47+
/**
48+
* Overridden so that the application doesn't expect the command
49+
* name to be the first argument.
50+
*/
51+
public function getDefinition()
52+
{
53+
$inputDefinition = parent::getDefinition();
54+
// clear out the normal first argument, which is the command name
55+
$inputDefinition->setArguments();
56+
57+
return $inputDefinition;
58+
}
59+
}
60+
61+
When calling your console script, the command ``MyCommand`` will then always
62+
be used, without having to pass its name.
63+
64+
You can also simplify how you execute the application::
65+
66+
#!/usr/bin/env php
67+
<?php
68+
// command.php
69+
use Acme\Tool\MyApplication;
70+
71+
$application = new MyApplication();
72+
$application->run();

0 commit comments

Comments
 (0)