Skip to content

Commit fa07fcf

Browse files
authored
Print necessary output in --quiet mode (#1483)
Previously, the --quiet (-q) flag hid ALL output, on stderr and stdout. It now only hides message/error output (stderr), and continues to print necessary output (stdout).
1 parent 689652f commit fa07fcf

File tree

2 files changed

+78
-44
lines changed

2 files changed

+78
-44
lines changed

src/Application.php

Lines changed: 78 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
use Symfony\Component\Console\Input\InputDefinition;
1616
use Symfony\Component\Console\Input\InputInterface;
1717
use Symfony\Component\Console\Input\InputOption;
18+
use Symfony\Component\Console\Input\StreamableInputInterface;
19+
use Symfony\Component\Console\Output\ConsoleOutputInterface;
1820
use Symfony\Component\Console\Output\OutputInterface;
1921
use Symfony\Component\Console\Terminal;
2022
use Symfony\Component\EventDispatcher\EventDispatcher;
@@ -66,8 +68,9 @@ protected function getDefaultInputDefinition()
6668
return new InputDefinition([
6769
new InputArgument('command', InputArgument::REQUIRED, 'The command to execute'),
6870
new InputOption('--help', '-h', InputOption::VALUE_NONE, 'Display this help message'),
69-
new InputOption('--verbose', '-v|vv|vvv', InputOption::VALUE_NONE, 'Increase the verbosity of messages'),
7071
new InputOption('--version', '-V', InputOption::VALUE_NONE, 'Display this application version'),
72+
new InputOption('--verbose', '-v|vv|vvv', InputOption::VALUE_NONE, 'Increase the verbosity of messages'),
73+
new InputOption('--quiet', '-q', InputOption::VALUE_NONE, 'Only print necessary output; suppress other messages and errors. This implies --no-interaction. It is ignored in verbose mode.'),
7174
new InputOption('--yes', '-y', InputOption::VALUE_NONE, 'Answer "yes" to confirmation questions; accept the default value for other questions; disable interaction'),
7275
new InputOption(
7376
'--no-interaction',
@@ -78,9 +81,7 @@ protected function getDefaultInputDefinition()
7881
),
7982
new HiddenInputOption('--ansi', '', InputOption::VALUE_NONE, 'Force ANSI output'),
8083
new HiddenInputOption('--no-ansi', '', InputOption::VALUE_NONE, 'Disable ANSI output'),
81-
// TODO deprecate the following options?
8284
new HiddenInputOption('--no', '-n', InputOption::VALUE_NONE, 'Answer "no" to confirmation questions; accept the default value for other questions; disable interaction'),
83-
new HiddenInputOption('--quiet', '-q', InputOption::VALUE_NONE, 'Do not output any message'),
8485
]);
8586
}
8687

@@ -320,14 +321,6 @@ public function getHelp()
320321
*/
321322
protected function configureIO(InputInterface $input, OutputInterface $output)
322323
{
323-
// Set the input to non-interactive if the yes or no options are used,
324-
// or if the PLATFORMSH_CLI_NO_INTERACTION variable is not empty.
325-
// The --no-interaction option is handled in the parent method.
326-
if ($input->hasParameterOption(['--yes', '-y', '--no', '-n'])
327-
|| getenv($this->envPrefix . 'NO_INTERACTION')) {
328-
$input->setInteractive(false);
329-
}
330-
331324
// Allow the NO_COLOR, CLICOLOR_FORCE, and TERM environment variables to
332325
// override whether colors are used in the output.
333326
// See: https://no-color.org
@@ -342,7 +335,80 @@ protected function configureIO(InputInterface $input, OutputInterface $output)
342335
$output->setDecorated(false);
343336
}
344337

345-
parent::configureIO($input, $output);
338+
if ($input->hasParameterOption('--ansi', true)) {
339+
$output->setDecorated(true);
340+
} elseif ($input->hasParameterOption('--no-ansi', true)) {
341+
$output->setDecorated(false);
342+
}
343+
344+
$stdErr = $output instanceof ConsoleOutputInterface ? $output->getErrorOutput() : $output;
345+
346+
if ($input->hasParameterOption(['--yes', '-y', '--no-interaction', '-n', '--no'], true)
347+
|| getenv($this->envPrefix . 'NO_INTERACTION')) {
348+
$input->setInteractive(false);
349+
350+
// Deprecate the -n flag as a shortcut for --no.
351+
// It is confusing as it's a shortcut for --no-interaction in other Symfony Console commands.
352+
if ($input->hasParameterOption('-n', true)) {
353+
$stdErr->writeln('<options=reverse>DEPRECATED</> The -n flag (as a shortcut for --no) is deprecated. It will be removed or changed in a future version.');
354+
}
355+
} elseif (\function_exists('posix_isatty')) {
356+
$inputStream = null;
357+
358+
if ($input instanceof StreamableInputInterface) {
359+
$inputStream = $input->getStream();
360+
}
361+
362+
if (!@posix_isatty($inputStream) && false === getenv('SHELL_INTERACTIVE')) {
363+
$input->setInteractive(false);
364+
}
365+
}
366+
367+
switch ($shellVerbosity = (int) getenv('SHELL_VERBOSITY')) {
368+
case -1: $stdErr->setVerbosity(OutputInterface::VERBOSITY_QUIET); break;
369+
case 1: $output->setVerbosity(OutputInterface::VERBOSITY_VERBOSE); break;
370+
case 2: $output->setVerbosity(OutputInterface::VERBOSITY_VERY_VERBOSE); break;
371+
case 3: $output->setVerbosity(OutputInterface::VERBOSITY_DEBUG); break;
372+
default: $shellVerbosity = 0; break;
373+
}
374+
375+
if ($input->hasParameterOption('-vvv', true)
376+
|| getenv('CLI_DEBUG') || getenv($this->envPrefix . 'DEBUG')) {
377+
$output->setVerbosity(OutputInterface::VERBOSITY_DEBUG);
378+
$shellVerbosity = 3;
379+
} elseif ($input->hasParameterOption('-vv', true)) {
380+
$output->setVerbosity(OutputInterface::VERBOSITY_VERY_VERBOSE);
381+
$shellVerbosity = 2;
382+
} elseif ($input->hasParameterOption(['-v', '--verbose'], true)) {
383+
$output->setVerbosity(OutputInterface::VERBOSITY_VERBOSE);
384+
$shellVerbosity = 1;
385+
} elseif ($input->hasParameterOption(['--quiet', '-q'], true)) {
386+
$stdErr->setVerbosity(OutputInterface::VERBOSITY_QUIET);
387+
$input->setInteractive(false);
388+
$shellVerbosity = -1;
389+
}
390+
391+
putenv('SHELL_VERBOSITY='.$shellVerbosity);
392+
$_ENV['SHELL_VERBOSITY'] = $shellVerbosity;
393+
$_SERVER['SHELL_VERBOSITY'] = $shellVerbosity;
394+
395+
// Turn off error reporting in quiet mode.
396+
if ($shellVerbosity === -1) {
397+
error_reporting(false);
398+
ini_set('display_errors', '0');
399+
} else {
400+
// Display errors by default. In verbose mode, display all PHP
401+
// error levels except deprecations. Deprecations will only be
402+
// displayed while in debug mode and only if this is enabled via
403+
// the CLI_REPORT_DEPRECATIONS environment variable.
404+
$error_level = ($shellVerbosity >= 1 ? E_ALL : E_PARSE | E_ERROR) & ~E_DEPRECATED;
405+
$report_deprecations = getenv('CLI_REPORT_DEPRECATIONS') || getenv($this->envPrefix . 'REPORT_DEPRECATIONS');
406+
if ($report_deprecations && $shellVerbosity >= 3) {
407+
$error_level |= E_DEPRECATED;
408+
}
409+
error_reporting($error_level);
410+
ini_set('display_errors', 'stderr');
411+
}
346412
}
347413

348414
/**

src/Command/CommandBase.php

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -190,29 +190,6 @@ protected function initialize(InputInterface $input, OutputInterface $output)
190190
$this->environment = null;
191191
$this->remoteContainer = null;
192192

193-
$envPrefix = $this->config()->get('application.env_prefix');
194-
if (getenv('CLI_DEBUG') || getenv($envPrefix . 'DEBUG')) {
195-
$output->setVerbosity(OutputInterface::VERBOSITY_DEBUG);
196-
}
197-
198-
// Tune error reporting.
199-
if ($output->isQuiet()) {
200-
error_reporting(false);
201-
ini_set('display_errors', '0');
202-
} else {
203-
// Display errors by default. In verbose mode, display all PHP
204-
// error levels except deprecations. Deprecations will only be
205-
// displayed while in debug mode and only if this is enabled via
206-
// the CLI_REPORT_DEPRECATIONS environment variable.
207-
$error_level = ($output->isVerbose() ? E_ALL : E_PARSE | E_ERROR) & ~E_DEPRECATED;
208-
$report_deprecations = getenv('CLI_REPORT_DEPRECATIONS') || getenv($envPrefix . 'REPORT_DEPRECATIONS');
209-
if ($report_deprecations && $output->isDebug()) {
210-
$error_level |= E_DEPRECATED;
211-
}
212-
error_reporting($error_level);
213-
ini_set('display_errors', 'stderr');
214-
}
215-
216193
$this->promptLegacyMigrate();
217194

218195
if (!self::$printedApiTokenWarning && $this->onContainer() && (getenv($this->config()->get('application.env_prefix') . 'TOKEN') || $this->api()->hasApiToken(false))) {
@@ -222,15 +199,6 @@ protected function initialize(InputInterface $input, OutputInterface $output)
222199
$this->stdErr->writeln('');
223200
self::$printedApiTokenWarning = true;
224201
}
225-
226-
// Deprecate the -n flag as a shortcut for --no.
227-
// It is confusing as it's a shortcut for --no-interaction in other Symfony Console commands.
228-
if ($this->input->hasOption('no') && $this->input->getOption('no') && !$this->input->hasParameterOption('--no')) {
229-
$this->labeledMessage(
230-
'DEPRECATED',
231-
'The -n flag (as a shortcut for --no) is deprecated. It will be removed or changed in a future version.'
232-
);
233-
}
234202
}
235203

236204
/**

0 commit comments

Comments
 (0)