Skip to content

Commit 5a92ed2

Browse files
santysisinicolas-grekas
authored andcommitted
[Console] Preserve --help option when a command is not found
1 parent 18c5fd1 commit 5a92ed2

File tree

2 files changed

+38
-7
lines changed

2 files changed

+38
-7
lines changed

Application.php

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

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

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 ";
738+
// remove hidden commands
739+
if ($alternatives = array_filter($alternatives, fn ($name) => !$this->get($name)->isHidden())) {
740+
$message .= \sprintf("\n\nDid you mean %s?\n %s", 1 === \count($alternatives) ? 'this' : 'one of these', implode("\n ", $alternatives));
742741
}
743-
$message .= implode("\n ", $alternatives);
742+
$this->wantHelps = $wantHelps;
744743
}
745744

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

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]);
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)