Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 42a9111

Browse files
committed
[Command] Added better for custom default command
1 parent d46ac31 commit 42a9111

File tree

1 file changed

+26
-3
lines changed

1 file changed

+26
-3
lines changed

src/Symfony/Component/Console/Application.php

Lines changed: 26 additions & 3 deletions
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

@@ -831,7 +838,7 @@ protected function doRunCommand(Command $command, InputInterface $input, OutputI
831838
*/
832839
protected function getCommandName(InputInterface $input)
833840
{
834-
return $input->getFirstArgument();
841+
return $this->singleCommand ?: $input->getFirstArgument();
835842
}
836843

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

10211044
private function stringWidth($string)

0 commit comments

Comments
 (0)