Skip to content

Commit 45c1cc6

Browse files
committed
[Console] Add the command name to input arguments if it's missing
1 parent f12a4c1 commit 45c1cc6

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

src/Symfony/Component/Console/Command/Command.php

+6
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,12 @@ public function run(InputInterface $input, OutputInterface $output)
241241
$this->interact($input, $output);
242242
}
243243

244+
// The command name argument is often omitted when a command is executed directly with its run() method.
245+
// It would fail the validation if we didn't make sure it's present, since it's required by the application.
246+
if ($input->hasArgument('command') && null === $input->getArgument('command')) {
247+
$input->setArgument('command', $this->getName());
248+
}
249+
244250
$input->validate();
245251

246252
if ($this->code) {

src/Symfony/Component/Console/Tests/Command/CommandTest.php

+9
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,15 @@ public function testRunReturnsIntegerExitCode()
267267
$this->assertSame(2, $exitCode, '->run() returns integer exit code (casts numeric to int)');
268268
}
269269

270+
public function testRunWithApplication()
271+
{
272+
$command = new \TestCommand();
273+
$command->setApplication(new Application());
274+
$exitCode = $command->run(new StringInput(''), new NullOutput());
275+
276+
$this->assertSame(0, $exitCode, '->run() returns an integer exit code');
277+
}
278+
270279
public function testRunReturnsAlwaysInteger()
271280
{
272281
$command = new \TestCommand();

0 commit comments

Comments
 (0)