Skip to content

Commit 0b7d8f4

Browse files
committed
[#2292] Expanding section about the single command that shows and explains how to override the getDefinition method
1 parent 5ce533a commit 0b7d8f4

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

components/console/introduction.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,8 @@ You also need to create the file to run at the command line which creates
8585
an ``Application`` and adds commands to it::
8686

8787
#!/usr/bin/env php
88-
# app/console
8988
<?php
89+
// app/console
9090

9191
use Acme\DemoBundle\Command\GreetCommand;
9292
use Symfony\Component\Console\Application;

components/console/single_command_tool.rst

+23-3
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,32 @@ it is possible to remove this need by extending the application::
4343

4444
return $defaultCommands;
4545
}
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+
}
4659
}
4760

4861
When calling your console script, the command `MyCommand` will then always
4962
be used, without having to pass its name.
5063

51-
.. note::
64+
You can also simplify how you execute the application::
65+
66+
#!/usr/bin/env php
67+
<?php
68+
// command.php
69+
70+
use Acme\Tool\MyApplication;
71+
72+
$application = new MyApplication();
73+
$application->run();
5274

53-
If your command accepts one or more arguments, you may also want to override
54-
the :method:`Symfony\\Component\\Console\\getDefaultInputDefinition` method.

0 commit comments

Comments
 (0)