-
-
Notifications
You must be signed in to change notification settings - Fork 5.2k
New Feature: Change the Default Command in the Console component #3426
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 8 commits
b29ab89
11c7174
60e2b3e
af9eac4
730985f
012456d
c23f34e
5e97202
c1b2aad
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. | ||
|
||
By default the Application will always run the ``ListCommand``. 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
.. code-block:: bash | ||
|
||
$ php application.php | ||
|
||
This will print the following to the command line: | ||
|
||
.. code-block:: text | ||
|
||
Hello Fabien | ||
|
||
.. tip:: | ||
|
||
The feature was a limitation since you cannot use the Command ``arguments``. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. feature has a limitation? Perhaps:
|
||
|
||
Learn More! | ||
----------- | ||
|
||
* :doc:`/components/console/single_command_tool` |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ Console | |
|
||
introduction | ||
usage | ||
changing_default_command | ||
single_command_tool | ||
events | ||
helpers/index |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
.. index:: | ||
single: Console; Single command application | ||
single: Console; Single command application | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Was this change intentional? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. no, and didn't notice that will revert! Thanks |
||
|
||
Building a Single Command Application | ||
===================================== | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,7 +19,7 @@ | |
|
||
* :doc:`/components/console/introduction` | ||
* :doc:`/components/console/usage` | ||
* :doc:`/components/console/single_command_tool` | ||
* :doc:`/components/console/changing_default_behavior` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Careful, I we need both of these now :) |
||
* :doc:`/components/console/events` | ||
* :doc:`/components/console/helpers/index` | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
By default,
Perhaps: