Skip to content

Commit f39e9b9

Browse files
bug #62627 [Console] Preserve --help option when a command is not found (santysisi)
This PR was merged into the 6.4 branch. Discussion ---------- [Console] Preserve `--help` option when a command is not found | Q | A | ------------- | --- | Branch? | 6.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Issues | Fix #62618 | License | MIT This PR resolves 3 issues: 1. The first one is the commented in the issue. The `--help` is not preserved when a command is not found 2. The second issue this pull request resolves is that hidden commands are currently being displayed if the `--help` option is present and the original command is not found. 3. The final issue this pull request resolves is that the "Did you mean?" prompt is not displayed if no alternative command is present. Commits ------- ac79f5b [Console] Preserve `--help` option when a command is not found
2 parents 8b4162f + ac79f5b commit f39e9b9

File tree

2 files changed

+38
-7
lines changed

2 files changed

+38
-7
lines changed

src/Symfony/Component/Console/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));

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