Skip to content

[Console] Show available commands in namespace when running namespace as command #47750

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 9, 2022
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
51 changes: 33 additions & 18 deletions src/Symfony/Component/Console/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
use Symfony\Component\Console\Exception\RuntimeException;
use Symfony\Component\Console\Formatter\OutputFormatter;
use Symfony\Component\Console\Helper\DebugFormatterHelper;
use Symfony\Component\Console\Helper\DescriptorHelper;
use Symfony\Component\Console\Helper\FormatterHelper;
use Symfony\Component\Console\Helper\Helper;
use Symfony\Component\Console\Helper\HelperSet;
Expand Down Expand Up @@ -259,7 +260,24 @@ public function doRun(InputInterface $input, OutputInterface $output)
// the command name MUST be the first element of the input
$command = $this->find($name);
} catch (\Throwable $e) {
if (!($e instanceof CommandNotFoundException && !$e instanceof NamespaceNotFoundException) || 1 !== \count($alternatives = $e->getAlternatives()) || !$input->isInteractive()) {
if (($e instanceof CommandNotFoundException && !$e instanceof NamespaceNotFoundException) && 1 === \count($alternatives = $e->getAlternatives()) && $input->isInteractive()) {
$alternative = $alternatives[0];

$style = new SymfonyStyle($input, $output);
$style->block(sprintf("\nCommand \"%s\" is not defined.\n", $name), null, 'error');
if (!$style->confirm(sprintf('Do you want to run "%s" instead? ', $alternative), false)) {
if (null !== $this->dispatcher) {
$event = new ConsoleErrorEvent($input, $output, $e);
$this->dispatcher->dispatch($event, ConsoleEvents::ERROR);

return $event->getExitCode();
}

return 1;
}

$command = $this->find($alternative);
} else {
if (null !== $this->dispatcher) {
$event = new ConsoleErrorEvent($input, $output, $e);
$this->dispatcher->dispatch($event, ConsoleEvents::ERROR);
Expand All @@ -271,25 +289,22 @@ public function doRun(InputInterface $input, OutputInterface $output)
$e = $event->getError();
}

throw $e;
}

$alternative = $alternatives[0];

$style = new SymfonyStyle($input, $output);
$style->block(sprintf("\nCommand \"%s\" is not defined.\n", $name), null, 'error');
if (!$style->confirm(sprintf('Do you want to run "%s" instead? ', $alternative), false)) {
if (null !== $this->dispatcher) {
$event = new ConsoleErrorEvent($input, $output, $e);
$this->dispatcher->dispatch($event, ConsoleEvents::ERROR);

return $event->getExitCode();
try {
if ($e instanceof CommandNotFoundException && $namespace = $this->findNamespace($name)) {
$helper = new DescriptorHelper();
$helper->describe($output instanceof ConsoleOutputInterface ? $output->getErrorOutput() : $output, $this, [
'format' => 'txt',
'raw_text' => false,
'namespace' => $namespace,
'short' => false,
]);

return isset($event) ? $event->getExitCode() : 1;
}
} catch (NamespaceNotFoundException) {
throw $e;
}

return 1;
}

$command = $this->find($alternative);
}

if ($command instanceof LazyCommand) {
Expand Down
16 changes: 16 additions & 0 deletions src/Symfony/Component/Console/Tests/ApplicationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -555,6 +555,22 @@ public function provideInvalidCommandNamesSingle()
];
}

public function testRunNamespace()
{
putenv('COLUMNS=120');
$application = new Application();
$application->setAutoExit(false);
$application->add(new \FooCommand());
$application->add(new \Foo1Command());
$application->add(new \Foo2Command());
$tester = new ApplicationTester($application);
$tester->run(['command' => 'foo'], ['decorated' => false]);
$display = trim($tester->getDisplay(true));
$this->assertStringContainsString('Available commands for the "foo" namespace:', $display);
$this->assertStringContainsString('The foo:bar command', $display);
$this->assertStringContainsString('The foo:bar1 command', $display);
}

public function testFindAlternativeExceptionMessageMultiple()
{
putenv('COLUMNS=120');
Expand Down