Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 6 additions & 7 deletions src/Symfony/Component/Console/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -732,15 +732,14 @@ public function find(string $name)
$message = \sprintf('Command "%s" is not defined.', $name);

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

if (1 == \count($alternatives)) {
$message .= "\n\nDid you mean this?\n ";
} else {
$message .= "\n\nDid you mean one of these?\n ";
// remove hidden commands
if ($alternatives = array_filter($alternatives, fn ($name) => !$this->get($name)->isHidden())) {
$message .= \sprintf("\n\nDid you mean %s?\n %s", 1 === \count($alternatives) ? 'this' : 'one of these', implode("\n ", $alternatives));
}
$message .= implode("\n ", $alternatives);
$this->wantHelps = $wantHelps;
}

throw new CommandNotFoundException($message, array_values($alternatives));
Expand Down
32 changes: 32 additions & 0 deletions src/Symfony/Component/Console/Tests/ApplicationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2439,6 +2439,38 @@ public function testFindAmbiguousHiddenCommands()
$application->find('t:f');
}

public function testDoesNotFindHiddenCommandAsAlternativeIfHelpOptionIsPresent()
{
$application = new Application();
$application->setAutoExit(false);
$application->add(new \FooHiddenCommand());

$tester = new ApplicationTester($application);
$tester->setInputs(['yes']);
$tester->run(['command' => 'foohidden', '--help' => true]);

$this->assertStringContainsString('Command "foohidden" is not defined.', $tester->getDisplay(true));
$this->assertStringNotContainsString('Did you mean', $tester->getDisplay(true));
$this->assertStringNotContainsString('Do you want to run', $tester->getDisplay(true));
$this->assertSame(Command::FAILURE, $tester->getStatusCode());
}

public function testsPreservedHelpOptionWhenItsAnAlternative()
{
$application = new Application();
$application->setAutoExit(false);
$application->add(new \FoobarCommand());

$tester = new ApplicationTester($application);
$tester->setInputs(['yes']);
$tester->run(['command' => 'foobarfoo', '--help' => true]);

$this->assertStringContainsString('Command "foobarfoo" is not defined.', $tester->getDisplay(true));
$this->assertStringContainsString('Do you want to run "foobar:foo" instead?', $tester->getDisplay(true));
$this->assertStringContainsString('The foobar:foo command', $tester->getDisplay(true));
$this->assertSame(Command::SUCCESS, $tester->getStatusCode());
}

/**
* Reads the private "signalHandlers" property of the SignalRegistry for assertions.
*/
Expand Down