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

Added a cookbook entry about building single command applications #1810

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

Merged
merged 2 commits into from
Oct 23, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions 1 components/console/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ Console

introduction
usage
single_command_tool
49 changes: 49 additions & 0 deletions 49 components/console/single_command_tool.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
.. index::
single: Console; Single command application

How to build an Application with a single Command
=================================================

When building a command line tool, you may not need to provide several commands.
In such case, having to pass the command name each time is tedious. Fortunately,
it is possible to remove this need by extending the application::

namespace Acme\Tool;

use Symfony\Component\Console\Application;
use Symfony\Component\Console\Input\InputInterface;

class MyApplication extends Application
{
/**
* Gets the name of the command based on input.
*
* @param InputInterface $input The input interface
*
* @return string The command name
*/
protected function getCommandName(InputInterface $input)
{
// This should return the name of your command.
return 'my_command';
}

/**
* Gets the default commands that should always be available.
*
* @return array An array of default Command instances
*/
protected function getDefaultCommands()
{
// Keep the core default commands to have the HelpCommand
// which is used when using the --help option
$defaultCommands = parent::getDefaultCommands()

$defaultCommands[] = new MyCommand();

return $defaultCommands;
}
}

When calling your console script, the command `MyCommand` will then always
be used, without having to pass its name.
1 change: 1 addition & 0 deletions 1 components/map.rst.inc
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

* :doc:`/components/console/introduction`
* :doc:`/components/console/usage`
* :doc:`/components/console/single_command_tool`

* **CSS Selector**

Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.