Skip to content

Add ReStructuredText descriptor for console component. #45718

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

Closed
wants to merge 12 commits into from
Closed
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

* Add method `__toString()` to `InputInterface`
* Deprecate `Command::$defaultName` and `Command::$defaultDescription`, use the `AsCommand` attribute instead
* Add `ReStructuredTextDescriptor`

6.0
---
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,210 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Console\Descriptor;

use Symfony\Component\Console\Application;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\Helper;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputDefinition;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

/**
* ReStructuredTextDescriptor descriptor.
Copy link
Contributor

Choose a reason for hiding this comment

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

Can be removed

*/
class ReStructuredTextDescriptor extends Descriptor
{
private $applicationTitleUnderlineChar = '#';
Copy link
Member

Choose a reason for hiding this comment

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

Some context in case anyone needs it: RST format doesn't define which characters to use for <h1>, <h2>, <h3>, etc. RST defines a series of characters that you can use (###, ===, ^^^, ~~~, etc.) and the first one found is considered <h1>, the second set of characters found is considered <h2>, etc.


I'm not saying that we need to do the same here, but, for your consideration, the standard notation that we follow in Symfony Docs is:

h1 =====
h2 -----
h3 ~~~~~
h4 .....

Here we would be using:

h1 #####
h2 *****
h3 =====
h4 -----

private $commandTitleUnderlineChar = '*';
private $argumentsTitleUnderlineChar = '=';
private $argumentTitleUnderlineChar = '-';

/**
* {@inheritdoc}
*/
public function describe(OutputInterface $output, object $object, array $options = [])
{
$decorated = $output->isDecorated();
$output->setDecorated(false);

parent::describe($output, $object, $options);

$output->setDecorated($decorated);
}

/**
* {@inheritdoc}
*/
protected function write(string $content, bool $decorated = true)
{
parent::write($content, $decorated);
Copy link
Member

Choose a reason for hiding this comment

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

why overriding the method to do nothing else than calling the parent ?

}

/**
* {@inheritdoc}
*/
protected function describeInputArgument(InputArgument $argument, array $options = [])
{
$this->write(
'``'.($argument->getName() ?: '<none>')."``\n".str_repeat($this->argumentTitleUnderlineChar, Helper::width($argument->getName()) + 4)."\n\n"
Copy link
Member

Choose a reason for hiding this comment

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

indentation looks wrong

.($argument->getDescription() ? preg_replace('/\s*[\r\n]\s*/', "\n", $argument->getDescription())."\n\n" : '')
.'- Is required: '.($argument->isRequired() ? 'yes' : 'no')."\n"
.'- Is array: '.($argument->isArray() ? 'yes' : 'no')."\n"
.'- Default: ``'.str_replace("\n", '', var_export($argument->getDefault(), true)).'``'
);
}

/**
* {@inheritdoc}
*/
protected function describeInputOption(InputOption $option, array $options = [])
{
$name = '--'.$option->getName();
if ($option->isNegatable()) {
$name .= '|--no-'.$option->getName();
}
if ($option->getShortcut()) {
$name .= '|-'.str_replace('|', '|-', $option->getShortcut()).'';
}

$this->write(
'``'.$name.'``'."\n".str_repeat($this->argumentTitleUnderlineChar, Helper::width($name) + 4)."\n\n"
Copy link
Member

Choose a reason for hiding this comment

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

here as well

.($option->getDescription() ? preg_replace('/\s*[\r\n]\s*/', "\n", $option->getDescription())."\n\n" : '')
.'- Accept value: '.($option->acceptValue() ? 'yes' : 'no')."\n"
.'- Is value required: '.($option->isValueRequired() ? 'yes' : 'no')."\n"
.'- Is multiple: '.($option->isArray() ? 'yes' : 'no')."\n"
.'- Is negatable: '.($option->isNegatable() ? 'yes' : 'no')."\n"
.'- Default: ``'.str_replace("\n", '', var_export($option->getDefault(), true)).'``'
);
}

/**
* {@inheritdoc}
*/
protected function describeInputDefinition(InputDefinition $definition, array $options = [])
{
if ($showArguments = \count($definition->getArguments()) > 0) {
$this->write("Arguments\n".str_repeat($this->argumentsTitleUnderlineChar, 9))."\n\n";
foreach ($definition->getArguments() as $argument) {
$this->write("\n\n");
if (null !== $describeInputArgument = $this->describeInputArgument($argument)) {
$this->write($describeInputArgument);
}
}
}

if (\count($definition->getOptions()) > 0) {
if ($showArguments) {
$this->write("\n\n");
}

$this->write("Options\n".str_repeat($this->argumentsTitleUnderlineChar, 7))."\n\n";
foreach ($definition->getOptions() as $option) {
$this->write("\n\n");
if (null !== $describeInputOption = $this->describeInputOption($option)) {
$this->write($describeInputOption);
}
}
}
}

/**
* {@inheritdoc}
*/
protected function describeCommand(Command $command, array $options = [])
{
if ($options['short'] ?? false) {
$this->write(
'``'.$command->getName()."``\n"
.str_repeat($this->commandTitleUnderlineChar, Helper::width($command->getName()) + 4)."\n\n"
.($command->getDescription() ? $command->getDescription()."\n\n" : '')
."Usage\n".str_repeat($this->argumentsTitleUnderlineChar, 5)."\n\n"
.array_reduce($command->getAliases(), function ($carry, $usage) {
return $carry.'- ``'.$usage.'``'."\n";
})
);

return;
}

$command->mergeApplicationDefinition(false);

foreach ($command->getAliases() as $alias) {
$this->write('.. _'.$alias.":\n\n");
}
$this->write(
'``'.$command->getName()."``\n"
.str_repeat($this->commandTitleUnderlineChar, Helper::width($command->getName()) + 4)."\n\n"
.($command->getDescription() ? $command->getDescription()."\n\n" : '')
."Usage\n".str_repeat($this->argumentsTitleUnderlineChar, 5)."\n\n"
.array_reduce(array_merge([$command->getSynopsis()], $command->getAliases(), $command->getUsages()), function ($carry, $usage) {
return $carry.'- ``'.$usage.'``'."\n";
})
);

if ($help = $command->getProcessedHelp()) {
$this->write("\n");
$this->write($help);
}

$definition = $command->getDefinition();
if ($definition->getOptions() || $definition->getArguments()) {
$this->write("\n\n");
$this->describeInputDefinition($definition);
}
}

/**
* {@inheritdoc}
*/
protected function describeApplication(Application $application, array $options = [])
{
$describedNamespace = $options['namespace'] ?? null;
$description = new ApplicationDescription($application, $describedNamespace);
$title = $this->getApplicationTitle($application);

$this->write($title."\n".str_repeat($this->applicationTitleUnderlineChar, Helper::width($title)));

foreach ($description->getNamespaces() as $namespace) {
if (ApplicationDescription::GLOBAL_NAMESPACE !== $namespace['id']) {
$this->write("\n\n");
$this->write('**'.$namespace['id'].':**');
}

$this->write("\n\n");
$this->write(implode("\n", array_map(function ($commandName) {
return sprintf('- `%s`_', $commandName);
}, $namespace['commands'])));
}

foreach ($description->getCommands() as $command) {
$this->write("\n\n");
if (null !== $describeCommand = $this->describeCommand($command, $options)) {
$this->write($describeCommand);
}
}
}

private function getApplicationTitle(Application $application): string
{
if ('UNKNOWN' !== $application->getName()) {
if ('UNKNOWN' !== $application->getVersion()) {
return sprintf('%s %s', $application->getName(), $application->getVersion());
}

return $application->getName();
}

return 'Console Tool';
}
}
2 changes: 2 additions & 0 deletions src/Symfony/Component/Console/Helper/DescriptorHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Symfony\Component\Console\Descriptor\DescriptorInterface;
use Symfony\Component\Console\Descriptor\JsonDescriptor;
use Symfony\Component\Console\Descriptor\MarkdownDescriptor;
use Symfony\Component\Console\Descriptor\ReStructuredTextDescriptor;
use Symfony\Component\Console\Descriptor\TextDescriptor;
use Symfony\Component\Console\Descriptor\XmlDescriptor;
use Symfony\Component\Console\Exception\InvalidArgumentException;
Expand All @@ -38,6 +39,7 @@ public function __construct()
->register('xml', new XmlDescriptor())
->register('json', new JsonDescriptor())
->register('md', new MarkdownDescriptor())
->register('rst', new ReStructuredTextDescriptor())
;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public function provideCompletionSuggestions()
{
yield 'option --format' => [
['--format', ''],
['txt', 'xml', 'json', 'md'],
['txt', 'xml', 'json', 'md', 'rst'],
];

yield 'nothing' => [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ public function provideCompletionSuggestions()
{
yield 'option --format' => [
['--format', ''],
['txt', 'xml', 'json', 'md'],
['txt', 'xml', 'json', 'md', 'rst'],
];

yield 'namespace' => [
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Console\Tests\Descriptor;

use Symfony\Component\Console\Descriptor\ReStructuredTextDescriptor;
use Symfony\Component\Console\Tests\Fixtures\DescriptorApplicationMbString;
use Symfony\Component\Console\Tests\Fixtures\DescriptorCommandMbString;

class ReStructuredTextDescriptorTest extends AbstractDescriptorTest
{
public function getDescribeCommandTestData()
{
return $this->getDescriptionTestData(array_merge(
ObjectsProvider::getCommands(),
['command_mbstring' => new DescriptorCommandMbString()]
));
}

public function getDescribeApplicationTestData()
{
return $this->getDescriptionTestData(array_merge(
ObjectsProvider::getApplications(),
['application_mbstring' => new DescriptorApplicationMbString()]
));
}

protected function getDescriptor()
{
return new ReStructuredTextDescriptor();
}

protected function getFormat()
{
return 'rst';
}
}
Loading