Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Console] Add silent verbosity suppressing all output, including errors #53632

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
Sep 21, 2024
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
7 changes: 7 additions & 0 deletions UPGRADE-7.2.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ Cache
* `igbinary_serialize()` is not used by default when the igbinary extension is installed
* Deprecate making `cache.app` adapter taggable, use the `cache.app.taggable` adapter instead

Console
-------

* [BC BREAK] Add ``--silent`` global option to enable the silent verbosity mode (suppressing all output, including errors)
If a custom command defines the `silent` option, it must be renamed before upgrading.
* Add `isSilent()` method to `OutputInterface`

DependencyInjection
-------------------

Expand Down
13 changes: 10 additions & 3 deletions src/Symfony/Component/Console/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -913,6 +913,9 @@ protected function configureIO(InputInterface $input, OutputInterface $output):
}

switch ($shellVerbosity = (int) getenv('SHELL_VERBOSITY')) {
case -2:
$output->setVerbosity(OutputInterface::VERBOSITY_SILENT);
break;
case -1:
$output->setVerbosity(OutputInterface::VERBOSITY_QUIET);
break;
Expand All @@ -930,7 +933,10 @@ protected function configureIO(InputInterface $input, OutputInterface $output):
break;
}

if (true === $input->hasParameterOption(['--quiet', '-q'], true)) {
if (true === $input->hasParameterOption(['--silent'], true)) {
$output->setVerbosity(OutputInterface::VERBOSITY_SILENT);
$shellVerbosity = -2;
} elseif (true === $input->hasParameterOption(['--quiet', '-q'], true)) {
$output->setVerbosity(OutputInterface::VERBOSITY_QUIET);
$shellVerbosity = -1;
} else {
Expand All @@ -946,7 +952,7 @@ protected function configureIO(InputInterface $input, OutputInterface $output):
}
}

if (-1 === $shellVerbosity) {
if (0 > $shellVerbosity) {
$input->setInteractive(false);
}

Expand Down Expand Up @@ -1082,7 +1088,8 @@ protected function getDefaultInputDefinition(): InputDefinition
return new InputDefinition([
new InputArgument('command', InputArgument::REQUIRED, 'The command to execute'),
new InputOption('--help', '-h', InputOption::VALUE_NONE, 'Display help for the given command. When no command is given display help for the <info>'.$this->defaultCommand.'</info> command'),
new InputOption('--quiet', '-q', InputOption::VALUE_NONE, 'Do not output any message'),
new InputOption('--silent', null, InputOption::VALUE_NONE, 'Do not output any message'),
new InputOption('--quiet', '-q', InputOption::VALUE_NONE, 'Only errors are displayed. All other output is suppressed'),
new InputOption('--verbose', '-v|vv|vvv', InputOption::VALUE_NONE, 'Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug'),
new InputOption('--version', '-V', InputOption::VALUE_NONE, 'Display this application version'),
new InputOption('--ansi', '', InputOption::VALUE_NEGATABLE, 'Force (or disable --no-ansi) ANSI output', null),
Expand Down
2 changes: 2 additions & 0 deletions src/Symfony/Component/Console/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ CHANGELOG

* Add support for `FORCE_COLOR` environment variable
* Add `verbosity` argument to `mustRun` process helper method
* [BC BREAK] Add silent verbosity (`--silent`/`SHELL_VERBOSITY=-2`) to suppress all output, including errors
* Add `OutputInterface::isSilent()`, `Output::isSilent()`, `OutputStyle::isSilent()` methods

7.1
---
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ private function getNonDefaultOptions(InputDefinition $definition): array
{
$globalOptions = [
'help',
'silent',
'quiet',
'verbose',
'version',
Expand Down
9 changes: 7 additions & 2 deletions src/Symfony/Component/Console/Output/NullOutput.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,19 @@ public function setVerbosity(int $level): void

public function getVerbosity(): int
{
return self::VERBOSITY_QUIET;
return self::VERBOSITY_SILENT;
}

public function isQuiet(): bool
public function isSilent(): bool
{
return true;
}

public function isQuiet(): bool
{
return false;
}

public function isVerbose(): bool
{
return false;
Expand Down
10 changes: 8 additions & 2 deletions src/Symfony/Component/Console/Output/Output.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,14 @@
/**
* Base class for output classes.
*
* There are five levels of verbosity:
* There are six levels of verbosity:
*
* * normal: no option passed (normal output)
* * verbose: -v (more output)
* * very verbose: -vv (highly extended output)
* * debug: -vvv (all debug output)
* * quiet: -q (no output)
* * quiet: -q (only output errors)
* * silent: --silent (no output)
*
* @author Fabien Potencier <[email protected]>
*/
Expand Down Expand Up @@ -74,6 +75,11 @@ public function getVerbosity(): int
return $this->verbosity;
}

public function isSilent(): bool
{
return self::VERBOSITY_SILENT === $this->verbosity;
}

public function isQuiet(): bool
{
return self::VERBOSITY_QUIET === $this->verbosity;
Expand Down
3 changes: 3 additions & 0 deletions src/Symfony/Component/Console/Output/OutputInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,12 @@
* OutputInterface is the interface implemented by all Output classes.
*
* @author Fabien Potencier <[email protected]>
*
* @method bool isSilent()
*/
interface OutputInterface
{
public const VERBOSITY_SILENT = 8;
public const VERBOSITY_QUIET = 16;
public const VERBOSITY_NORMAL = 32;
public const VERBOSITY_VERBOSE = 64;
Expand Down
6 changes: 6 additions & 0 deletions src/Symfony/Component/Console/Style/OutputStyle.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,12 @@ public function getFormatter(): OutputFormatterInterface
return $this->output->getFormatter();
}

public function isSilent(): bool
{
// @deprecated since Symfony 7.2, change to $this->output->isSilent() in 8.0
return method_exists($this->output, 'isSilent') ? $this->output->isSilent() : self::VERBOSITY_SILENT === $this->output->getVerbosity();
}

public function isQuiet(): bool
{
return $this->output->isQuiet();
Expand Down
5 changes: 4 additions & 1 deletion src/Symfony/Component/Console/Tests/ApplicationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -851,12 +851,15 @@ public function testRenderException()
putenv('COLUMNS=120');
$tester = new ApplicationTester($application);

$tester->run(['command' => 'foo'], ['decorated' => false, 'capture_stderr_separately' => true]);
$tester->run(['command' => 'foo'], ['decorated' => false, 'verbosity' => Output::VERBOSITY_QUIET, 'capture_stderr_separately' => true]);
$this->assertStringEqualsFile(self::$fixturesPath.'/application_renderexception1.txt', $tester->getErrorOutput(true), '->renderException() renders a pretty exception');

$tester->run(['command' => 'foo'], ['decorated' => false, 'verbosity' => Output::VERBOSITY_VERBOSE, 'capture_stderr_separately' => true]);
$this->assertStringContainsString('Exception trace', $tester->getErrorOutput(), '->renderException() renders a pretty exception with a stack trace when verbosity is verbose');

$tester->run(['command' => 'foo'], ['decorated' => false, 'verbosity' => Output::VERBOSITY_SILENT, 'capture_stderr_separately' => true]);
$this->assertSame('', $tester->getErrorOutput(true), '->renderException() renders nothing in SILENT verbosity');

$tester->run(['command' => 'list', '--foo' => true], ['decorated' => false, 'capture_stderr_separately' => true]);
$this->assertStringEqualsFile(self::$fixturesPath.'/application_renderexception2.txt', $tester->getErrorOutput(true), '->renderException() renders the command synopsis when an exception occurs in the context of a command');

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,9 @@ public function testCompleteCommandInputDefinition(array $input, array $suggesti

public static function provideCompleteCommandInputDefinitionInputs()
{
yield 'definition' => [['bin/console', 'hello', '-'], ['--help', '--quiet', '--verbose', '--version', '--ansi', '--no-ansi', '--no-interaction']];
yield 'definition' => [['bin/console', 'hello', '-'], ['--help', '--silent', '--quiet', '--verbose', '--version', '--ansi', '--no-ansi', '--no-interaction']];
yield 'custom' => [['bin/console', 'hello'], ['Fabien', 'Robin', 'Wouter']];
yield 'definition-aliased' => [['bin/console', 'ahoy', '-'], ['--help', '--quiet', '--verbose', '--version', '--ansi', '--no-ansi', '--no-interaction']];
yield 'definition-aliased' => [['bin/console', 'ahoy', '-'], ['--help', '--silent', '--quiet', '--verbose', '--version', '--ansi', '--no-ansi', '--no-interaction']];
yield 'custom-aliased' => [['bin/console', 'ahoy'], ['Fabien', 'Robin', 'Wouter']];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ public function testExecuteListsCommandsOrder()

Options:
-h, --help Display help for the given command. When no command is given display help for the list command
-q, --quiet Do not output any message
--silent Do not output any message
-q, --quiet Only errors are displayed. All other output is suppressed
-V, --version Display this application version
--ansi|--no-ansi Force (or disable --no-ansi) ANSI output
-n, --no-interaction Do not ask any interactive question
Expand Down
44 changes: 40 additions & 4 deletions src/Symfony/Component/Console/Tests/Fixtures/application_1.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,22 @@
"description": "Display help for the given command. When no command is given display help for the <info>list</info> command",
"default": false
},
"silent": {
"name": "--silent",
"shortcut": "",
"accept_value": false,
"is_value_required": false,
"is_multiple": false,
"description": "Do not output any message",
"default": false
},
"quiet": {
"name": "--quiet",
"shortcut": "-q",
"accept_value": false,
"is_value_required": false,
"is_multiple": false,
"description": "Do not output any message",
"description": "Only errors are displayed. All other output is suppressed",
"default": false
},
"verbose": {
Expand Down Expand Up @@ -150,13 +159,22 @@
"description": "Display help for the given command. When no command is given display help for the <info>list</info> command",
"default": false
},
"silent": {
"name": "--silent",
"shortcut": "",
"accept_value": false,
"is_value_required": false,
"is_multiple": false,
"description": "Do not output any message",
"default": false
},
"quiet": {
"name": "--quiet",
"shortcut": "-q",
"accept_value": false,
"is_value_required": false,
"is_multiple": false,
"description": "Do not output any message",
"description": "Only errors are displayed. All other output is suppressed",
"default": false
},
"verbose": {
Expand Down Expand Up @@ -262,13 +280,22 @@
"description": "Display help for the given command. When no command is given display help for the <info>list</info> command",
"default": false
},
"silent": {
"name": "--silent",
"shortcut": "",
"accept_value": false,
"is_value_required": false,
"is_multiple": false,
"description": "Do not output any message",
"default": false
},
"quiet": {
"name": "--quiet",
"shortcut": "-q",
"accept_value": false,
"is_value_required": false,
"is_multiple": false,
"description": "Do not output any message",
"description": "Only errors are displayed. All other output is suppressed",
"default": false
},
"verbose": {
Expand Down Expand Up @@ -365,13 +392,22 @@
"description": "Display help for the given command. When no command is given display help for the <info>list</info> command",
"default": false
},
"silent": {
"name": "--silent",
"shortcut": "",
"accept_value": false,
"is_value_required": false,
"is_multiple": false,
"description": "Do not output any message",
"default": false
},
"quiet": {
"name": "--quiet",
"shortcut": "-q",
"accept_value": false,
"is_value_required": false,
"is_multiple": false,
"description": "Do not output any message",
"description": "Only errors are displayed. All other output is suppressed",
"default": false
},
"verbose": {
Expand Down
36 changes: 33 additions & 3 deletions src/Symfony/Component/Console/Tests/Fixtures/application_1.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ Display help for the given command. When no command is given display help for th
* Is negatable: no
* Default: `false`

#### `--quiet|-q`
#### `--silent`

Do not output any message

Expand All @@ -58,6 +58,16 @@ Do not output any message
* Is negatable: no
* Default: `false`

#### `--quiet|-q`

Only errors are displayed. All other output is suppressed

* Accept value: no
* Is value required: no
* Is multiple: no
* Is negatable: no
* Default: `false`

#### `--verbose|-v|-vv|-vvv`

Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug
Expand Down Expand Up @@ -159,7 +169,7 @@ Display help for the given command. When no command is given display help for th
* Is negatable: no
* Default: `false`

#### `--quiet|-q`
#### `--silent`

Do not output any message

Expand All @@ -169,6 +179,16 @@ Do not output any message
* Is negatable: no
* Default: `false`

#### `--quiet|-q`

Only errors are displayed. All other output is suppressed

* Accept value: no
* Is value required: no
* Is multiple: no
* Is negatable: no
* Default: `false`

#### `--verbose|-v|-vv|-vvv`

Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug
Expand Down Expand Up @@ -286,7 +306,7 @@ Display help for the given command. When no command is given display help for th
* Is negatable: no
* Default: `false`

#### `--quiet|-q`
#### `--silent`

Do not output any message

Expand All @@ -296,6 +316,16 @@ Do not output any message
* Is negatable: no
* Default: `false`

#### `--quiet|-q`

Only errors are displayed. All other output is suppressed

* Accept value: no
* Is value required: no
* Is multiple: no
* Is negatable: no
* Default: `false`

#### `--verbose|-v|-vv|-vvv`

Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ Console Tool

<comment>Options:</comment>
<info>-h, --help</info> Display help for the given command. When no command is given display help for the <info>list</info> command
<info>-q, --quiet</info> Do not output any message
<info> --silent</info> Do not output any message
<info>-q, --quiet</info> Only errors are displayed. All other output is suppressed
<info>-V, --version</info> Display this application version
<info> --ansi|--no-ansi</info> Force (or disable --no-ansi) ANSI output
<info>-n, --no-interaction</info> Do not ask any interactive question
Expand Down
Loading