From fdb4b1fd75169839c218762854d5a931ebf53c6a Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Thu, 9 May 2013 10:40:45 +0200 Subject: [PATCH 1/2] [Console] moved --help support to allow proper behavior with other passed options --- src/Symfony/Component/Console/Application.php | 21 +++++++++---------- .../Console/Tests/ApplicationTest.php | 12 +++++++++++ 2 files changed, 22 insertions(+), 11 deletions(-) diff --git a/src/Symfony/Component/Console/Application.php b/src/Symfony/Component/Console/Application.php index e8761d8837..c713bc2340 100644 --- a/src/Symfony/Component/Console/Application.php +++ b/src/Symfony/Component/Console/Application.php @@ -154,23 +154,12 @@ class Application */ public function doRun(InputInterface $input, OutputInterface $output) { - $name = $this->getCommandName($input); - if (true === $input->hasParameterOption(array('--ansi'))) { $output->setDecorated(true); } elseif (true === $input->hasParameterOption(array('--no-ansi'))) { $output->setDecorated(false); } - if (true === $input->hasParameterOption(array('--help', '-h'))) { - if (!$name) { - $name = 'help'; - $input = new ArrayInput(array('command' => 'help')); - } else { - $this->wantHelps = true; - } - } - if (true === $input->hasParameterOption(array('--no-interaction', '-n'))) { $input->setInteractive(false); } @@ -200,6 +189,16 @@ class Application return 0; } + $name = $this->getCommandName($input); + if (true === $input->hasParameterOption(array('--help', '-h'))) { + if (!$name) { + $name = 'help'; + $input = new ArrayInput(array('command' => 'help')); + } else { + $this->wantHelps = true; + } + } + if (!$name) { $name = 'list'; $input = new ArrayInput(array('command' => 'list')); diff --git a/src/Symfony/Component/Console/Tests/ApplicationTest.php b/src/Symfony/Component/Console/Tests/ApplicationTest.php index 912de51c10..f912338e04 100644 --- a/src/Symfony/Component/Console/Tests/ApplicationTest.php +++ b/src/Symfony/Component/Console/Tests/ApplicationTest.php @@ -146,6 +146,18 @@ class ApplicationTest extends \PHPUnit_Framework_TestCase $this->assertInstanceOf('Symfony\Component\Console\Command\HelpCommand', $command, '->get() returns the help command if --help is provided as the input'); } + public function testSilentHelp() + { + $application = new Application(); + $application->setAutoExit(false); + $application->setCatchExceptions(false); + + $tester = new ApplicationTester($application); + $tester->run(array('-h' => true, '-q' => true), array('decorated' => false)); + + $this->assertEmpty($tester->getDisplay(true)); + } + /** * @expectedException \InvalidArgumentException * @expectedExceptionMessage The command "foofoo" does not exist. From bd0c48cc0052a594d9416471bcce6b365294b952 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Thu, 9 May 2013 10:57:19 +0200 Subject: [PATCH 2/2] [Console] moved the IO configuration to its own method The IO configuration was also moved earlier in the process so that options are taken into account as early as possible. That's useful for instance in Symfony FrameworkBundle, where commands are registered in the doRun() method. If an exception occurs during registration, the -q, -v, ... options would not have any effect. --- src/Symfony/Component/Console/Application.php | 69 +++++++++++-------- 1 file changed, 40 insertions(+), 29 deletions(-) diff --git a/src/Symfony/Component/Console/Application.php b/src/Symfony/Component/Console/Application.php index c713bc2340..e2ea78cb7c 100644 --- a/src/Symfony/Component/Console/Application.php +++ b/src/Symfony/Component/Console/Application.php @@ -115,6 +115,8 @@ class Application $output = new ConsoleOutput(); } + $this->configureIO($input, $output); + try { $exitCode = $this->doRun($input, $output); } catch (\Exception $e) { @@ -154,35 +156,6 @@ class Application */ public function doRun(InputInterface $input, OutputInterface $output) { - if (true === $input->hasParameterOption(array('--ansi'))) { - $output->setDecorated(true); - } elseif (true === $input->hasParameterOption(array('--no-ansi'))) { - $output->setDecorated(false); - } - - if (true === $input->hasParameterOption(array('--no-interaction', '-n'))) { - $input->setInteractive(false); - } - - if (function_exists('posix_isatty') && $this->getHelperSet()->has('dialog')) { - $inputStream = $this->getHelperSet()->get('dialog')->getInputStream(); - if (!posix_isatty($inputStream)) { - $input->setInteractive(false); - } - } - - if (true === $input->hasParameterOption(array('--quiet', '-q'))) { - $output->setVerbosity(OutputInterface::VERBOSITY_QUIET); - } else { - if ($input->hasParameterOption('-vvv') || $input->hasParameterOption('--verbose=3') || $input->getParameterOption('--verbose') === 3) { - $output->setVerbosity(OutputInterface::VERBOSITY_DEBUG); - } elseif ($input->hasParameterOption('-vv') || $input->hasParameterOption('--verbose=2') || $input->getParameterOption('--verbose') === 2) { - $output->setVerbosity(OutputInterface::VERBOSITY_VERY_VERBOSE); - } elseif ($input->hasParameterOption('-v') || $input->hasParameterOption('--verbose=1') || $input->hasParameterOption('--verbose') || $input->getParameterOption('--verbose')) { - $output->setVerbosity(OutputInterface::VERBOSITY_VERBOSE); - } - } - if (true === $input->hasParameterOption(array('--version', '-V'))) { $output->writeln($this->getLongVersion()); @@ -863,6 +836,44 @@ class Application return array(null, null); } + /** + * Configures the input and output instances based on the user arguments and options. + * + * @param InputInterface $input An InputInterface instance + * @param OutputInterface $output An OutputInterface instance + */ + protected function configureIO(InputInterface $input, OutputInterface $output) + { + if (true === $input->hasParameterOption(array('--ansi'))) { + $output->setDecorated(true); + } elseif (true === $input->hasParameterOption(array('--no-ansi'))) { + $output->setDecorated(false); + } + + if (true === $input->hasParameterOption(array('--no-interaction', '-n'))) { + $input->setInteractive(false); + } + + if (function_exists('posix_isatty') && $this->getHelperSet()->has('dialog')) { + $inputStream = $this->getHelperSet()->get('dialog')->getInputStream(); + if (!posix_isatty($inputStream)) { + $input->setInteractive(false); + } + } + + if (true === $input->hasParameterOption(array('--quiet', '-q'))) { + $output->setVerbosity(OutputInterface::VERBOSITY_QUIET); + } else { + if ($input->hasParameterOption('-vvv') || $input->hasParameterOption('--verbose=3') || $input->getParameterOption('--verbose') === 3) { + $output->setVerbosity(OutputInterface::VERBOSITY_DEBUG); + } elseif ($input->hasParameterOption('-vv') || $input->hasParameterOption('--verbose=2') || $input->getParameterOption('--verbose') === 2) { + $output->setVerbosity(OutputInterface::VERBOSITY_VERY_VERBOSE); + } elseif ($input->hasParameterOption('-v') || $input->hasParameterOption('--verbose=1') || $input->hasParameterOption('--verbose') || $input->getParameterOption('--verbose')) { + $output->setVerbosity(OutputInterface::VERBOSITY_VERBOSE); + } + } + } + /** * Runs the current command. *