Skip to content

[Console] Show Application-level console options when showing synopsis or when error occurred #47436

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 11 commits into
base: 7.4
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions src/Symfony/Component/Console/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ CHANGELOG

* Improve truecolor terminal detection in some cases
* Add support for 256 color terminals (conversion from Ansi24 to Ansi8 if terminal is capable of it)
* Show Application-level console options when showing synopsis or when error occurred

6.1
---
Expand Down
10 changes: 7 additions & 3 deletions src/Symfony/Component/Console/Command/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -375,15 +375,19 @@ public function setCode(callable $code): static
*
* @internal
*/
public function mergeApplicationDefinition(bool $mergeArgs = true)
public function mergeApplicationDefinition(bool $mergeArgs = true, bool $mergeOptions = true)
{
if (null === $this->application) {
return;
}

$this->fullDefinition = new InputDefinition();
$this->fullDefinition->setOptions($this->definition->getOptions());
$this->fullDefinition->addOptions($this->application->getDefinition()->getOptions());
if ($mergeOptions) {
$this->fullDefinition->setOptions($this->definition->getOptions());
$this->fullDefinition->addOptions($this->application->getDefinition()->getOptions());
} else {
$this->fullDefinition->setOptions($this->definition->getOptions());
}

if ($mergeArgs) {
$this->fullDefinition->setArguments($this->application->getDefinition()->getArguments());
Expand Down
4 changes: 2 additions & 2 deletions src/Symfony/Component/Console/Command/LazyCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,9 @@ public function setCode(callable $code): static
/**
* @internal
*/
public function mergeApplicationDefinition(bool $mergeArgs = true): void
public function mergeApplicationDefinition(bool $mergeArgs = true, bool $mergeOptions = false): void
{
$this->getCommand()->mergeApplicationDefinition($mergeArgs);
$this->getCommand()->mergeApplicationDefinition($mergeArgs, $mergeOptions);
}

public function setDefinition(array|InputDefinition $definition): static
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/Console/Descriptor/Descriptor.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ abstract protected function describeInputOption(InputOption $option, array $opti
/**
* Describes an InputDefinition instance.
*/
abstract protected function describeInputDefinition(InputDefinition $definition, array $options = []);
abstract protected function describeInputDefinition(InputDefinition $definition, array $options = [], string $prefix = '');

/**
* Describes a Command instance.
Expand Down
38 changes: 26 additions & 12 deletions src/Symfony/Component/Console/Descriptor/JsonDescriptor.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ protected function describeInputOption(InputOption $option, array $options = [])
}
}

protected function describeInputDefinition(InputDefinition $definition, array $options = [])
protected function describeInputDefinition(InputDefinition $definition, array $options = [], string $prefix = '')
{
$this->writeData($this->getInputDefinitionData($definition), $options);
$this->writeData($this->getInputDefinitionData($definition, $prefix), $options);
}

protected function describeCommand(Command $command, array $options = [])
Expand Down Expand Up @@ -120,22 +120,26 @@ private function getInputOptionData(InputOption $option, bool $negated = false):
];
}

private function getInputDefinitionData(InputDefinition $definition): array
private function getInputDefinitionData(InputDefinition $definition, string $prefix = ''): array
{
$inputArguments = [];
foreach ($definition->getArguments() as $name => $argument) {
$inputArguments[$name] = $this->getInputArgumentData($argument);
if ($definition->getArguments()) {
foreach ($definition->getArguments() as $name => $argument) {
$inputArguments[$name] = $this->getInputArgumentData($argument);
}
}

$inputOptions = [];
foreach ($definition->getOptions() as $name => $option) {
$inputOptions[$name] = $this->getInputOptionData($option);
if ($option->isNegatable()) {
$inputOptions['no-'.$name] = $this->getInputOptionData($option, true);
if ($definition->getOptions()) {
foreach ($definition->getOptions() as $name => $option) {
$inputOptions[$name] = $this->getInputOptionData($option);
if ($option->isNegatable()) {
$inputOptions['no-'.$name] = $this->getInputOptionData($option, true);
}
}
}

return ['arguments' => $inputArguments, 'options' => $inputOptions];
return ["{$prefix}arguments" => $inputArguments, "{$prefix}options" => $inputOptions];
}

private function getCommandData(Command $command, bool $short = false): array
Expand All @@ -150,13 +154,23 @@ private function getCommandData(Command $command, bool $short = false): array
'usage' => $command->getAliases(),
];
} else {
$command->mergeApplicationDefinition(false);
$command->mergeApplicationDefinition(false, false);

$data += [
'usage' => array_merge([$command->getSynopsis()], $command->getUsages(), $command->getAliases()),
'help' => $command->getProcessedHelp(),
'definition' => $this->getInputDefinitionData($command->getDefinition()),
'definition' => [],
];

$definition = $command->getApplication()?->getDefinition();
if ($definition && ($definition->getOptions() || $definition->getArguments())) {
$data['definition'] = $this->getInputDefinitionData($command->getApplication()->getDefinition(), 'application-level ');
}

$definition = $command->getDefinition();
if ($definition->getOptions() || $definition->getArguments()) {
$data['definition'] += $this->getInputDefinitionData($command->getDefinition(), 'command-level ');
}
}

$data['hidden'] = $command->isHidden();
Expand Down
16 changes: 11 additions & 5 deletions src/Symfony/Component/Console/Descriptor/MarkdownDescriptor.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,10 @@ protected function describeInputOption(InputOption $option, array $options = [])
);
}

protected function describeInputDefinition(InputDefinition $definition, array $options = [])
protected function describeInputDefinition(InputDefinition $definition, array $options = [], string $prefix = '')
{
if ($showArguments = \count($definition->getArguments()) > 0) {
$this->write('### Arguments');
$this->write("### {$prefix}Arguments");
foreach ($definition->getArguments() as $argument) {
$this->write("\n\n");
if (null !== $describeInputArgument = $this->describeInputArgument($argument)) {
Expand All @@ -92,7 +92,7 @@ protected function describeInputDefinition(InputDefinition $definition, array $o
$this->write("\n\n");
}

$this->write('### Options');
$this->write("### {$prefix}Options");
foreach ($definition->getOptions() as $option) {
$this->write("\n\n");
if (null !== $describeInputOption = $this->describeInputOption($option)) {
Expand All @@ -118,7 +118,7 @@ protected function describeCommand(Command $command, array $options = [])
return;
}

$command->mergeApplicationDefinition(false);
$command->mergeApplicationDefinition(false, false);

$this->write(
'`'.$command->getName()."`\n"
Expand All @@ -135,10 +135,16 @@ protected function describeCommand(Command $command, array $options = [])
$this->write($help);
}

$definition = $command->getApplication()?->getDefinition();
if ($definition && ($definition->getOptions() || $definition->getArguments())) {
$this->write("\n\n");
$this->describeInputDefinition($definition, prefix: 'Application-level ');
}

$definition = $command->getDefinition();
if ($definition->getOptions() || $definition->getArguments()) {
$this->write("\n\n");
$this->describeInputDefinition($definition);
$this->describeInputDefinition($definition, prefix: 'Command-level ');
}
}

Expand Down
17 changes: 13 additions & 4 deletions src/Symfony/Component/Console/Descriptor/TextDescriptor.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,15 +83,15 @@ protected function describeInputOption(InputOption $option, array $options = [])
), $options);
}

protected function describeInputDefinition(InputDefinition $definition, array $options = [])
protected function describeInputDefinition(InputDefinition $definition, array $options = [], string $prefix = '')
{
$totalWidth = $this->calculateTotalWidthForOptions($definition->getOptions());
foreach ($definition->getArguments() as $argument) {
$totalWidth = max($totalWidth, Helper::width($argument->getName()));
}

if ($definition->getArguments()) {
$this->writeText('<comment>Arguments:</comment>', $options);
$this->writeText("<comment>{$prefix}Arguments:</comment>", $options);
$this->writeText("\n");
foreach ($definition->getArguments() as $argument) {
$this->describeInputArgument($argument, array_merge($options, ['total_width' => $totalWidth]));
Expand All @@ -106,7 +106,7 @@ protected function describeInputDefinition(InputDefinition $definition, array $o
if ($definition->getOptions()) {
$laterOptions = [];

$this->writeText('<comment>Options:</comment>', $options);
$this->writeText("<comment>{$prefix}Options:</comment>", $options);
foreach ($definition->getOptions() as $option) {
if (\strlen($option->getShortcut() ?? '') > 1) {
$laterOptions[] = $option;
Expand Down Expand Up @@ -140,10 +140,19 @@ protected function describeCommand(Command $command, array $options = [])
}
$this->writeText("\n");

$command->mergeApplicationDefinition(false, false);

$definition = $command->getApplication()?->getDefinition();
if ($definition && ($definition->getOptions() || $definition->getArguments())) {
$this->writeText("\n");
$this->describeInputDefinition($definition, $options, 'Application-level ');
$this->writeText("\n");
}

$definition = $command->getDefinition();
if ($definition->getOptions() || $definition->getArguments()) {
$this->writeText("\n");
$this->describeInputDefinition($definition, $options);
$this->describeInputDefinition($definition, $options, 'Command-level ');
$this->writeText("\n");
}

Expand Down
43 changes: 31 additions & 12 deletions src/Symfony/Component/Console/Descriptor/XmlDescriptor.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,29 @@
*/
class XmlDescriptor extends Descriptor
{
public function getInputDefinitionDocument(InputDefinition $definition): \DOMDocument
public function getInputDefinitionDocument(InputDefinition $definition, string $type = ''): \DOMDocument
{
$dom = new \DOMDocument('1.0', 'UTF-8');
$dom->appendChild($definitionXML = $dom->createElement('definition'));

$definitionXML->appendChild($argumentsXML = $dom->createElement('arguments'));
foreach ($definition->getArguments() as $argument) {
$this->appendDocument($argumentsXML, $this->getInputArgumentDocument($argument));
if ($definition->getArguments()) {
$definitionXML->appendChild($argumentsXML = $dom->createElement('arguments'));
if ($type) {
$argumentsXML->setAttribute('type', $type);
}
foreach ($definition->getArguments() as $argument) {
$this->appendDocument($argumentsXML, $this->getInputArgumentDocument($argument));
}
}

$definitionXML->appendChild($optionsXML = $dom->createElement('options'));
foreach ($definition->getOptions() as $option) {
$this->appendDocument($optionsXML, $this->getInputOptionDocument($option));
if ($definition->getOptions()) {
$definitionXML->appendChild($optionsXML = $dom->createElement('options'));
if ($type) {
$optionsXML->setAttribute('type', $type);
}
foreach ($definition->getOptions() as $option) {
$this->appendDocument($optionsXML, $this->getInputOptionDocument($option));
}
}

return $dom;
Expand All @@ -63,7 +73,7 @@ public function getCommandDocument(Command $command, bool $short = false): \DOMD
$usagesXML->appendChild($dom->createElement('usage', $usage));
}
} else {
$command->mergeApplicationDefinition(false);
$command->mergeApplicationDefinition(false, false);

foreach (array_merge([$command->getSynopsis()], $command->getAliases(), $command->getUsages()) as $usage) {
$usagesXML->appendChild($dom->createElement('usage', $usage));
Expand All @@ -72,8 +82,17 @@ public function getCommandDocument(Command $command, bool $short = false): \DOMD
$commandXML->appendChild($helpXML = $dom->createElement('help'));
$helpXML->appendChild($dom->createTextNode(str_replace("\n", "\n ", $command->getProcessedHelp())));

$definitionXML = $this->getInputDefinitionDocument($command->getDefinition());
$this->appendDocument($commandXML, $definitionXML->getElementsByTagName('definition')->item(0));
$definition = $command->getApplication()?->getDefinition();
if ($definition && ($definition->getOptions() || $definition->getArguments())) {
$definitionXML = $this->getInputDefinitionDocument($definition, 'application-level');
$this->appendDocument($commandXML, $definitionXML->getElementsByTagName('definition')->item(0));
}

$definition = $command->getDefinition();
if ($definition->getOptions() || $definition->getArguments()) {
$definitionXML = $this->getInputDefinitionDocument($definition, 'command-level');
$this->appendDocument($commandXML, $definitionXML->getElementsByTagName('definition')->item(0));
}
}

return $dom;
Expand Down Expand Up @@ -130,9 +149,9 @@ protected function describeInputOption(InputOption $option, array $options = [])
$this->writeDocument($this->getInputOptionDocument($option));
}

protected function describeInputDefinition(InputDefinition $definition, array $options = [])
protected function describeInputDefinition(InputDefinition $definition, array $options = [], string $prefix = '')
{
$this->writeDocument($this->getInputDefinitionDocument($definition));
$this->writeDocument($this->getInputDefinitionDocument($definition, $prefix));
}

protected function describeCommand(Command $command, array $options = [])
Expand Down
Loading