|
1 | 1 | .. 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 |
3 | 44 |
|
4 | 45 | Building a Single Command Application
|
5 |
| -===================================== |
| 46 | +------------------------------------- |
6 | 47 |
|
7 | 48 | When building a command line tool, you may not need to provide several commands.
|
8 | 49 | In such case, having to pass the command name each time is tedious. Fortunately,
|
|
0 commit comments