Skip to content

[Console] Add of hidden and deprecation option flags #54439

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

Open
wants to merge 15 commits into
base: 7.4
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
[Console] Change color of deprecated option to gray
  • Loading branch information
flkasper committed Sep 7, 2024
commit 69b4bf8c5a6e1a28288d5bbfaffa5a35e96d9a99
6 changes: 4 additions & 2 deletions src/Symfony/Component/Console/Command/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Symfony\Component\Console\Exception\ExceptionInterface;
use Symfony\Component\Console\Exception\InvalidArgumentException;
use Symfony\Component\Console\Exception\LogicException;
use Symfony\Component\Console\Helper\FormatterHelper;
use Symfony\Component\Console\Helper\HelperInterface;
use Symfony\Component\Console\Helper\HelperSet;
use Symfony\Component\Console\Input\InputArgument;
Expand Down Expand Up @@ -275,7 +276,7 @@ public function run(InputInterface $input, OutputInterface $output): int
$input->validate();

if (isset($inputDefinition)) {
Copy link
Member

Choose a reason for hiding this comment

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

why the isset?

Suggested change
if (isset($inputDefinition)) {
if ($inputDefinition) {

Copy link
Member

Choose a reason for hiding this comment

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

$inputDefinition is defined in a try..catch, but should be outside.

Copy link
Author

@flkasper flkasper Aug 26, 2024

Choose a reason for hiding this comment

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

As @GromNaN has already explained, $inputDefinition is defined within a try...catch:

try {
    $inputDefinition = $this->getDefinition();
    $input->bind($inputDefinition);
} catch (ExceptionInterface $e) {
    if (!$this->ignoreValidationErrors) {
        throw $e;
    }
}

Since getDefinition can also throw an exception (if the parent constructor call is missing), moving it outside the try...catch makes little sense.

Therefore, isset must be used to check whether $inputDefinition is defined.

$this->printDeprecationMessages($inputDefinition, $input, $output);
$this->writeDeprecationMessages($inputDefinition, $input, $output);
}

if ($this->code) {
Expand Down Expand Up @@ -653,7 +654,7 @@ public function getHelper(string $name): HelperInterface
return $this->helperSet->get($name);
}

private function printDeprecationMessages(InputDefinition $inputDefinition, InputInterface $input, OutputInterface $output): void
private function writeDeprecationMessages(InputDefinition $inputDefinition, InputInterface $input, OutputInterface $output): void
{
$deprecationMessages = [];
foreach ($inputDefinition->getOptions() as $inputOption) {
Expand All @@ -670,6 +671,7 @@ private function printDeprecationMessages(InputDefinition $inputDefinition, Inpu
}
}
if (!empty($deprecationMessages)) {
/** @var FormatterHelper $formatter */
$formatter = $this->getHelper('formatter');
$output->writeln($formatter->formatBlock($deprecationMessages, 'fg=black;bg=yellow', true));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ protected function describeInputOption(InputOption $option, array $options = [])
$spacingWidth = $totalWidth - Helper::width($synopsis);

$this->writeText(\sprintf(' %s %s%s%s%s',
\sprintf('<%1$s>%2$s</%1$s>', $option->isDeprecated() ? 'text_error' : 'info', $synopsis),
\sprintf('<%1$s>%2$s</%1$s>', $option->isDeprecated() ? 'fg=gray;' : 'info', $synopsis),
str_repeat(' ', $spacingWidth),
// + 4 = 2 spaces before <info>, 2 spaces after </info>
preg_replace('/\s*[\r\n]\s*/', "\n".str_repeat(' ', $totalWidth + 4), $option->getDescription()),
Expand Down