8000 [Console] Add option to automatically run suggested command if there is only 1 alternative by pierredup · Pull Request #25732 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Console] Add option to automatically run suggested command if there is only 1 alternative #25732

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

Closed
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
8000
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
Prev Previous commit
Next Next commit
Use SymfonyStyle
  • Loading branch information
pierredup committed Feb 11, 2018
commit f8a816d8ca96928a49cd8ca971551595d5a20d49
6 changes: 4 additions & 2 deletions src/Symfony/Component/Console/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
use Symfony\Component\Console\Exception\CommandNotFoundException;
use Symfony\Component\Console\Exception\LogicException;
use Symfony\Component\Console\Question\ConfirmationQuestion;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\Debug\ErrorHandler;
use Symfony\Component\Debug\Exception\FatalThrowableError;
use Symfony\Component\E 8000 ventDispatcher\EventDispatcherInterface;
Expand Down Expand Up @@ -241,9 +242,10 @@ public function doRun(InputInterface $input, OutputInterface $output)
}

$alternative = $alternatives[0];
$question = new ConfirmationQuestion(sprintf("<error>Command \"%s\" is not defined.</error>\n\nDo you want to run \"%s\" instead? [y/n] ", $name, $alternative), false);

if (!(new QuestionHelper())->ask($input, $output, $question)) {
$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(ConsoleEvents::ERROR, $event);
Expand Down
25 changes: 23 additions & 2 deletions src/Symfony/Component/Console/Tests/ApplicationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -485,7 +485,18 @@ public function testCanRunAlternativeCommandName()
$tester = new ApplicationTester($application);
$tester->setInputs(array('y'));
$tester->run(array('command' => 'foos'), array('decorated' => false));
$this->assertSame("Command \"foos\" is not defined.\n\nDo you want to run \"foo\" instead? [y/n] called\n", $tester->getDisplay(true));
$this->assertSame(<<<OUTPUT
< 8000 /td>

Command "foos" is not defined.


Do you want to run "foo" instead? (yes/no) [no]:
>
called

OUTPUT
, $tester->getDisplay(true));
}

public function testDontRunAlternativeCommandName()
Expand All @@ -497,7 +508,17 @@ public function testDontRunAlternativeCommandName()
$tester->setInputs(array('n'));
$exitCode = $tester->run(array('command' => 'foos'), array('decorated' => false));
$this->assertSame(1, $exitCode);
$this->assertSame("Command \"foos\" is not defined.\n\nDo you want to run \"foo\" instead? [y/n] ", $tester->getDisplay(true));
$this->assertSame(<<<OUTPUT


Command "foos" is not defined.


Do you want to run "foo" instead? (yes/no) [no]:
>

OUTPUT
, $tester->getDisplay(true));
}

public function provideInvalidCommandNamesSingle()
Expand Down
0