Skip to content

Commit a7d0fa2

Browse files
committed
[Console] Better support for one command app
1 parent 1e263c0 commit a7d0fa2

File tree

3 files changed

+55
-21
lines changed

3 files changed

+55
-21
lines changed

src/Symfony/Component/Console/Application.php

+22-4
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ class Application
6666
private $dispatcher;
6767
private $terminalDimensions;
6868
private $defaultCommand;
69+
private $singleCommand;
6970

7071
/**
7172
* Constructor.
@@ -168,7 +169,7 @@ public function doRun(InputInterface $input, OutputInterface $output)
168169
if (true === $input->hasParameterOption(array('--help', '-h'), true)) {
169170
if (!$name) {
170171
$name = 'help';
171-
$input = new ArrayInput(array('command' => 'help'));
172+
$input = new ArrayInput(array('command_name' => $this->defaultCommand));
172173
} else {
173174
$this->wantHelps = true;
174175
}
@@ -226,6 +227,13 @@ public function setDefinition(InputDefinition $definition)
226227
*/
227228
public function getDefinition()
228229
{
230+
if ($this->singleCommand) {
231+
$inputDefinition = $this->definition;
232+
$inputDefinition->setArguments();
233+
234+
return $inputDefinition;
235+
}
236+
229237
return $this->definition;
230238
}
231239

@@ -859,7 +867,7 @@ protected function doRunCommand(Command $command, InputInterface $input, OutputI
859867
*/
860868
protected function getCommandName(InputInterface $input)
861869
{
862-
return $input->getFirstArgument();
870+
return $this->singleCommand ? $this->defaultCommand : $input->getFirstArgument();
863871
}
864872

865873
/**
@@ -1039,11 +1047,21 @@ private function findAlternatives($name, $collection)
10391047
/**
10401048
* Sets the default Command name.
10411049
*
1042-
* @param string $commandName The Command name
1050+
* @param string $commandName The Command name
1051+
* @param bool $isSingleCommand Set to true if there is only one command in this application
10431052
*/
1044-
public function setDefaultCommand($commandName)
1053+
public function setDefaultCommand($commandName, $isSingleCommand = false)
10451054
{
10461055
$this->defaultCommand = $commandName;
1056+
1057+
if ($isSingleCommand) {
1058+
// Ensure the command exist
1059+
$this->find($commandName);
1060+
1061+
$this->singleCommand = true;
1062+
}
1063+
1064+
return $this;
10471065
}
10481066

10491067
private function stringWidth($string)

src/Symfony/Component/Console/Tests/ApplicationTest.php

+18
Original file line numberDiff line numberDiff line change
@@ -1086,6 +1086,24 @@ public function testSetRunCustomDefaultCommand()
10861086
$this->assertEquals('interact called'.PHP_EOL.'called'.PHP_EOL, $tester->getDisplay(), 'Application runs the default set command if different from \'list\' command');
10871087
}
10881088

1089+
public function testSetRunCustomSingleCommand()
1090+
{
1091+
$command = new \FooCommand();
1092+
1093+
$application = new Application();
1094+
$application->setAutoExit(false);
1095+
$application->add($command);
1096+
$application->setDefaultCommand($command->getName(), true);
1097+
1098+
$tester = new ApplicationTester($application);
1099+
1100+
$tester->run(array());
1101+
$this->assertEquals('interact called'.PHP_EOL.'called'.PHP_EOL, $tester->getDisplay());
1102+
1103+
$tester->run(array('--help' => true));
1104+
$this->assertContains('Usage:', $tester->getDisplay());
1105+
}
1106+
10891107
/**
10901108
* @requires function posix_isatty
10911109
*/
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,26 @@
11
Usage:
2-
help [options] [--] [<command_name>]
2+
list [options] [--] [<namespace>]
33

44
Arguments:
5-
command The command to execute
6-
command_name The command name [default: "help"]
5+
namespace The namespace name
76

87
Options:
9-
--format=FORMAT The output format (txt, xml, json, or md) [default: "txt"]
10-
--raw To output raw command help
11-
-h, --help Display this help message
12-
-q, --quiet Do not output any message
13-
-V, --version Display this application version
14-
--ansi Force ANSI output
15-
--no-ansi Disable ANSI output
16-
-n, --no-interaction Do not ask any interactive question
17-
-v|vv|vvv, --verbose Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug
8+
--raw To output raw command list
9+
--format=FORMAT The output format (txt, xml, json, or md) [default: "txt"]
1810

1911
Help:
20-
The help command displays help for a given command:
12+
The list command lists all commands:
2113

22-
php app/console help list
14+
php app/console list
2315

24-
You can also output the help in other formats by using the --format option:
16+
You can also display the commands for a specific namespace:
2517

26-
php app/console help --format=xml list
18+
php app/console list test
2719

28-
To display the list of available commands, please use the list command.
20+
You can also output the information in other formats by using the --format option:
21+
22+
php app/console list --format=xml
23+
24+
It's also possible to get raw list of commands (useful for embedding command runner):
25+
26+
php app/console list --raw

0 commit comments

Comments
 (0)