diff --git a/components/console/changing_default_command.rst b/components/console/changing_default_command.rst new file mode 100644 index 00000000000..a2072c795ba --- /dev/null +++ b/components/console/changing_default_command.rst @@ -0,0 +1,67 @@ +.. index:: + single: Console; Changing the Default Command + +Changing the Default Command +============================ + +.. versionadded:: 2.5, + The :method:`Symfony\\Component\\Console\\Application::setDefaultCommand` + method was introduced in version 2.5. + +will always run the ``ListCommand`` when no command name is passed. In order to change +the default command you just need to pass the command name you want to run by +default to the ``setDefaultCommand`` method:: + + namespace Acme\Command; + + use Symfony\Component\Console\Command\Command; + use Symfony\Component\Console\Input\InputInterface; + use Symfony\Component\Console\Output\OutputInterface; + + class HelloWorldCommand extends Command + { + protected function configure() + { + $this->setName('hello:world') + ->setDescription('Outputs \'Hello World\''); + } + + protected function execute(InputInterface $input, OutputInterface $output) + { + $output->writeln('Hello World'); + } + } + +Executing the application and changing the default Command:: + + // application.php + + use Acme\Command\HelloWorldCommand; + use Symfony\Component\Console\Application; + + $command = new HelloWorldCommand(); + $application = new Application(); + $application->add($command); + $application->setDefaultCommand($command->getName()); + $application->run(); + +Test the new default console command by running the following: + +.. code-block:: bash + + $ php application.php + +This will print the following to the command line: + +.. code-block:: text + + Hello Fabien + +.. tip:: + + This feature has a limitation: you cannot use it with any Command arguments. + +Learn More! +----------- + +* :doc:`/components/console/single_command_tool` diff --git a/components/console/index.rst b/components/console/index.rst index c814942d018..f974291a8e0 100644 --- a/components/console/index.rst +++ b/components/console/index.rst @@ -6,6 +6,7 @@ Console introduction usage + changing_default_command single_command_tool events helpers/index diff --git a/components/console/introduction.rst b/components/console/introduction.rst index 4ca2bcfe8c1..c742c333f89 100644 --- a/components/console/introduction.rst +++ b/components/console/introduction.rst @@ -526,6 +526,7 @@ Learn More! * :doc:`/components/console/usage` * :doc:`/components/console/single_command_tool` +* :doc:`/components/console/changing_default_command` .. _Packagist: https://packagist.org/packages/symfony/console .. _ANSICON: https://github.com/adoxa/ansicon/downloads diff --git a/components/console/single_command_tool.rst b/components/console/single_command_tool.rst index 27942df40f2..02fc24a565f 100644 --- a/components/console/single_command_tool.rst +++ b/components/console/single_command_tool.rst @@ -1,5 +1,5 @@ .. index:: - single: Console; Single command application + single: Console; Single command application Building a Single Command Application ===================================== diff --git a/components/map.rst.inc b/components/map.rst.inc index a5f2d0f8db7..7a5c74eeff6 100644 --- a/components/map.rst.inc +++ b/components/map.rst.inc @@ -20,6 +20,7 @@ * :doc:`/components/console/introduction` * :doc:`/components/console/usage` * :doc:`/components/console/single_command_tool` + * :doc:`/components/console/changing_default_command` * :doc:`/components/console/events` * :doc:`/components/console/helpers/index`