Skip to content

Commit 7dcead4

Browse files
committed
[Console] Preserve --help option when a command is not found
1 parent baf4a16 commit 7dcead4

File tree

2 files changed

+42
-5
lines changed

2 files changed

+42
-5
lines changed

src/Symfony/Component/Console/Application.php

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -732,15 +732,20 @@ public function find(string $name)
732732
$message = \sprintf('Command "%s" is not defined.', $name);
733733

734734
if ($alternatives = $this->findAlternatives($name, $allCommands)) {
735+
$wantHelps = $this->wantHelps;
736+
$this->wantHelps = false;
735737
// remove hidden commands
736738
$alternatives = array_filter($alternatives, fn ($name) => !$this->get($name)->isHidden());
739+
$this->wantHelps = $wantHelps;
737740

738-
if (1 == \count($alternatives)) {
739-
$message .= "\n\nDid you mean this?\n ";
740-
} else {
741-
$message .= "\n\nDid you mean one of these?\n ";
741+
if ($alternatives) {
742+
if (1 == \count($alternatives)) {
743+
$message .= "\n\nDid you mean this?\n ";
744+
} else {
745+
$message .= "\n\nDid you mean one of these?\n ";
746+
}
747+
$message .= implode("\n ", $alternatives);
742748
}
743-
$message .= implode("\n ", $alternatives);
744749
}
745750

746751
throw new CommandNotFoundException($message, array_values($alternatives));

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2439,6 +2439,38 @@ public function testFindAmbiguousHiddenCommands()
24392439
$application->find('t:f');
24402440
}
24412441

2442+
public function testDoesNotFindHiddenCommandAsAlternativeIfHelpOptionIsPresent()
2443+
{
2444+
$application = new Application();
2445+
$application->setAutoExit(false);
2446+
$application->add(new \FooHiddenCommand());
2447+
2448+
$tester = new ApplicationTester($application);
2449+
$tester->setInputs(['yes']);
2450+
$tester->run(['command' => 'foohidden', '--help' => true], ['interactive' => true]);
2451+
2452+
$this->assertStringContainsString('Command "foohidden" is not defined.', $tester->getDisplay(true));
2453+
$this->assertStringNotContainsString('Did you mean', $tester->getDisplay(true));
2454+
$this->assertStringNotContainsString('Do you want to run', $tester->getDisplay(true));
2455+
$this->assertSame(Command::FAILURE, $tester->getStatusCode());
2456+
}
2457+
2458+
public function testsPreservedHelpOptionWhenItsAnAlternative()
2459+
{
2460+
$application = new Application();
2461+
$application->setAutoExit(false);
2462+
$application->add(new \FoobarCommand());
2463+
2464+
$tester = new ApplicationTester($application);
2465+
$tester->setInputs(['yes']);
2466+
$tester->run(['command' => 'foobarfoo', '--help' => true]);
2467+
2468+
$this->assertStringContainsString('Command "foobarfoo" is not defined.', $tester->getDisplay(true));
2469+
$this->assertStringContainsString('Do you want to run "foobar:foo" instead?', $tester->getDisplay(true));
2470+
$this->assertStringContainsString('The foobar:foo command', $tester->getDisplay(true));
2471+
$this->assertSame(Command::SUCCESS, $tester->getStatusCode());
2472+
}
2473+
24422474
/**
24432475
* Reads the private "signalHandlers" property of the SignalRegistry for assertions.
24442476
*/

0 commit comments

Comments
 (0)