Skip to content
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

Add function for getting the current numeric shell verbosity #48942

Open
wants to merge 6 commits into
base: 6.3
Choose a base branch
from

Conversation

Saggre
Copy link

@Saggre Saggre commented Jan 10, 2023

Q A
Branch? 6.3
Bug fix? no
New feature? yes
Deprecations? no
Tickets
License MIT
Doc PR

Add Application::getShellVerbosity for getting the current numeric shell verbosity.

This makes the code cleaner and allows us to do something like this to get the numeric verbosity level of an InputInterface:

$input = new ArrayInput(
    [
        'command',
        '--foo' => 'bar',
        '-vvv'  => 1
    ]
);

$verbosity = $this->application->getShellVerbosity($input);
// int(256)

In my case I needed to get the verbosity of a dynamically built command call and found it too troublesome to get with the current tools.


Test pass and the functionality should remain the same as it is now.

The appropriate tests are here:

$tester->run(['command' => 'list', '--quiet' => true]);
$this->assertSame('', $tester->getDisplay(), '->run() removes all output if --quiet is passed');
$this->assertFalse($tester->getInput()->isInteractive(), '->run() sets off the interactive mode if --quiet is passed');
$tester->run(['command' => 'list', '-q' => true]);
$this->assertSame('', $tester->getDisplay(), '->run() removes all output if -q is passed');
$this->assertFalse($tester->getInput()->isInteractive(), '->run() sets off the interactive mode if -q is passed');
$tester->run(['command' => 'list', '--verbose' => true]);
$this->assertSame(Output::VERBOSITY_VERBOSE, $tester->getOutput()->getVerbosity(), '->run() sets the output to verbose if --verbose is passed');
$tester->run(['command' => 'list', '--verbose' => 1]);
$this->assertSame(Output::VERBOSITY_VERBOSE, $tester->getOutput()->getVerbosity(), '->run() sets the output to verbose if --verbose=1 is passed');
$tester->run(['command' => 'list', '--verbose' => 2]);
$this->assertSame(Output::VERBOSITY_VERY_VERBOSE, $tester->getOutput()->getVerbosity(), '->run() sets the output to very verbose if --verbose=2 is passed');
$tester->run(['command' => 'list', '--verbose' => 3]);
$this->assertSame(Output::VERBOSITY_DEBUG, $tester->getOutput()->getVerbosity(), '->run() sets the output to debug if --verbose=3 is passed');
$tester->run(['command' => 'list', '--verbose' => 4]);
$this->assertSame(Output::VERBOSITY_VERBOSE, $tester->getOutput()->getVerbosity(), '->run() sets the output to verbose if unknown --verbose level is passed');
$tester->run(['command' => 'list', '-v' => true]);
$this->assertSame(Output::VERBOSITY_VERBOSE, $tester->getOutput()->getVerbosity(), '->run() sets the output to verbose if -v is passed');
$tester->run(['command' => 'list', '-vv' => true]);
$this->assertSame(Output::VERBOSITY_VERY_VERBOSE, $tester->getOutput()->getVerbosity(), '->run() sets the output to verbose if -v is passed');
$tester->run(['command' => 'list', '-vvv' => true]);
$this->assertSame(Output::VERBOSITY_DEBUG, $tester->getOutput()->getVerbosity(), '->run() sets the output to verbose if -v is passed');


The PR is using the same internal verbosity levels from -1 to 3 as before. The following mapping is performed with a bit shift:

-1 => 16 (VERBOSITY_QUIET)
0 => 32 (VERBOSITY_NORMAL)
1 => 64 (VERBOSITY_VERBOSE)
2 => 128 (VERBOSITY_VERY_VERBOSE)
3 => 256 (VERBOSITY_DEBUG)

Perhaps a switch statement would be more readable but the bit shift makes for a neat one-liner.


OutputInterface::setVerbosity is not called when verbosity is 0 because it wasn't called in the previous implementation either:

switch ($shellVerbosity = (int) getenv('SHELL_VERBOSITY')) {
case -1:
$output->setVerbosity(OutputInterface::VERBOSITY_QUIET);
break;
case 1:
$output->setVerbosity(OutputInterface::VERBOSITY_VERBOSE);
break;
case 2:
$output->setVerbosity(OutputInterface::VERBOSITY_VERY_VERBOSE);
break;
case 3:
$output->setVerbosity(OutputInterface::VERBOSITY_DEBUG);
break;
default:
$shellVerbosity = 0;
break;
}
if (true === $input->hasParameterOption(['--quiet', '-q'], true)) {
$output->setVerbosity(OutputInterface::VERBOSITY_QUIET);
$shellVerbosity = -1;
} else {
if ($input->hasParameterOption('-vvv', true) || $input->hasParameterOption('--verbose=3', true) || 3 === $input->getParameterOption('--verbose', false, true)) {
$output->setVerbosity(OutputInterface::VERBOSITY_DEBUG);
$shellVerbosity = 3;
} elseif ($input->hasParameterOption('-vv', true) || $input->hasParameterOption('--verbose=2', true) || 2 === $input->getParameterOption('--verbose', false, true)) {
$output->setVerbosity(OutputInterface::VERBOSITY_VERY_VERBOSE);
$shellVerbosity = 2;
} elseif ($input->hasParameterOption('-v', true) || $input->hasParameterOption('--verbose=1', true) || $input->hasParameterOption('--verbose', true) || $input->getParameterOption('--verbose', false, true)) {
$output->setVerbosity(OutputInterface::VERBOSITY_VERBOSE);
$shellVerbosity = 1;
}
}
if (-1 === $shellVerbosity) {
$input->setInteractive(false);
}
if (\function_exists('putenv')) {
@putenv('SHELL_VERBOSITY='.$shellVerbosity);
}
$_ENV['SHELL_VERBOSITY'] = $shellVerbosity;
$_SERVER['SHELL_VERBOSITY'] = $shellVerbosity;

@Saggre Saggre requested a review from chalasr as a code owner Jan 10, 2023
@carsonbot carsonbot added this to the 6.3 milestone Jan 10, 2023
@carsonbot
Copy link

Hey!

I see that this is your first PR. That is great! Welcome!

Symfony has a contribution guide which I suggest you to read.

In short:

  • Always add tests
  • Keep backward compatibility (see https://symfony.com/bc).
  • Bug fixes must be submitted against the lowest maintained branch where they apply (see https://symfony.com/releases)
  • Features and deprecations must be submitted against the 6.3 branch.

Review the GitHub status checks of your pull request and try to solve the reported issues. If some tests are failing, try to see if they are failing because of this change.

When two Symfony core team members approve this change, it will be merged and you will become an official Symfony contributor!
If this PR is merged in a lower version branch, it will be merged up to all maintained branches within a few days.

I am going to sit back now and wait for the reviews.

Cheers!

Carsonbot

@carsonbot
Copy link

Hey!

Thanks for your PR. You are targeting branch "6.3" but it seems your PR description refers to branch "6.3 for features / 5.4, 6.0, 6.1, or 6.2 for bug fixes".
Could you update the PR description or change target branch? This helps core maintainers a lot.

Cheers!

Carsonbot

@Saggre Saggre changed the title Feature/shell verbosity fn Add function for getting the current numeric shell verbosity Jan 10, 2023
@Saggre Saggre changed the title Add function for getting the current numeric shell verbosity [Console] Add function for getting the current numeric shell verbosity Jan 10, 2023
/**
* Get shell verbosity.
*
* @param InputInterface $input
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This @param and @return doc has no benefit. IMHO the whole phpdoc can be removed.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed phpdoc

src/Symfony/Component/Console/Application.php Show resolved Hide resolved
$shellVerbosity = 1;
}
if ($shellVerbosity) {
$output->setVerbosity(1 << $shellVerbosity + 5);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a bit of magic here. i guess at least a comment would be helpful and maybe as wenn some tests to cover this.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a comment. ApplicationTest::testRun asserts the verbosity of a command's output and will fail if this mapping is incorrect.

@carsonbot carsonbot changed the title [Console] Add function for getting the current numeric shell verbosity Add function for getting the current numeric shell verbosity Jan 11, 2023
@Saggre
Copy link
Author

Saggre commented Jan 12, 2023

@maxbeckers I made some changes based on your comments

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants