Skip to content

Commit b342502

Browse files
committed
[Command] Added better for custom default command
1 parent 45e26ca commit b342502

File tree

1 file changed

+26
-3
lines changed

1 file changed

+26
-3
lines changed

src/Symfony/Component/Console/Application.php

+26-3
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.
@@ -167,8 +168,7 @@ public function doRun(InputInterface $input, OutputInterface $output)
167168
$name = $this->getCommandName($input);
168169
if (true === $input->hasParameterOption(array('--help', '-h'), true)) {
169170
if (!$name) {
170-
$name = 'help';
171-
$input = new ArrayInput(array('command' => 'help'));
171+
$input = new ArrayInput(array('command_name' => $this->defaultCommand));
172172
} else {
173173
$this->wantHelps = true;
174174
}
@@ -226,6 +226,13 @@ public function setDefinition(InputDefinition $definition)
226226
*/
227227
public function getDefinition()
228228
{
229+
if ($this->singleCommand) {
230+
$inputDefinition = $this->definition;
231+
$inputDefinition->setArguments();
232+
233+
return $inputDefinition;
234+
}
235+
229236
return $this->definition;
230237
}
231238

@@ -832,7 +839,7 @@ protected function doRunCommand(Command $command, InputInterface $input, OutputI
832839
*/
833840
protected function getCommandName(InputInterface $input)
834841
{
835-
return $input->getFirstArgument();
842+
return $this->singleCommand ?: $input->getFirstArgument();
836843
}
837844

838845
/**
@@ -1017,6 +1024,22 @@ private function findAlternatives($name, $collection)
10171024
public function setDefaultCommand($commandName)
10181025
{
10191026
$this->defaultCommand = $commandName;
1027+
1028+
return $this;
1029+
}
1030+
1031+
public function setSingleCommand($commandName)
1032+
{
1033+
if (null !== $this->singleCommand) {
1034+
throw new \LogicException('A Single command is already defined.');
1035+
}
1036+
1037+
// Ensure the command exist
1038+
$this->find($commandName);
1039+
1040+
$this->singleCommand = $commandName;
1041+
1042+
return $this;
10201043
}
10211044

10221045
private function stringWidth($string)

0 commit comments

Comments
 (0)