Skip to content

Commit dee78e6

Browse files
committed
Print necessary output in --quiet mode
1 parent 689652f commit dee78e6

File tree

2 files changed

+78
-43
lines changed

2 files changed

+78
-43
lines changed

src/Application.php

Lines changed: 78 additions & 11 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',
@@ -80,7 +83,6 @@ protected function getDefaultInputDefinition()
8083
new HiddenInputOption('--no-ansi', '', InputOption::VALUE_NONE, 'Disable ANSI output'),
8184
// TODO deprecate the following options?
8285
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'),
8486
]);
8587
}
8688

@@ -320,14 +322,6 @@ public function getHelp()
320322
*/
321323
protected function configureIO(InputInterface $input, OutputInterface $output)
322324
{
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-
331325
// Allow the NO_COLOR, CLICOLOR_FORCE, and TERM environment variables to
332326
// override whether colors are used in the output.
333327
// See: https://no-color.org
@@ -342,7 +336,80 @@ protected function configureIO(InputInterface $input, OutputInterface $output)
342336
$output->setDecorated(false);
343337
}
344338

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

348415
/**

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)