Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 761975b

Browse filesBrowse files
committed
Merge pull request #1810 from stof/single_command_tool
Added a cookbook entry about building single command applications
2 parents 05f2a2e + 2cfcdcb commit 761975b
Copy full SHA for 761975b

File tree

3 files changed

+51
-0
lines changed
Filter options

3 files changed

+51
-0
lines changed

‎components/console/index.rst

Copy file name to clipboardExpand all lines: components/console/index.rst
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ Console
66

77
introduction
88
usage
9+
single_command_tool
+49Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
.. index::
2+
single: Console; Single command application
3+
4+
How to build an Application with a single Command
5+
=================================================
6+
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+
When calling your console script, the command `MyCommand` will then always
49+
be used, without having to pass its name.

‎components/map.rst.inc

Copy file name to clipboardExpand all lines: components/map.rst.inc
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
* :doc:`/components/console/introduction`
1515
* :doc:`/components/console/usage`
16+
* :doc:`/components/console/single_command_tool`
1617

1718
* **CSS Selector**
1819

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.