Skip to content

[Console] add option --short to the list command #39904

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
Jan 20, 2021
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
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class DebugAutowiringCommand extends ContainerDebugCommand
{
protected static $defaultName = 'debug:autowiring';
protected static $defaultDescription = 'Lists classes/interfaces you can use for autowiring';

private $supportsHref;
private $fileLinkFormatter;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
class XliffLintCommand extends BaseLintCommand
{
protected static $defaultName = 'lint:xliff';
protected static $defaultDescription = 'Lints a XLIFF file and outputs encountered errors';

public function __construct()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
class YamlLintCommand extends BaseLintCommand
{
protected static $defaultName = 'lint:yaml';
protected static $defaultDescription = 'Lints a file and outputs encountered errors';

public function __construct()
{
Expand Down
3 changes: 3 additions & 0 deletions src/Symfony/Bundle/TwigBundle/Command/LintCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
*/
final class LintCommand extends BaseLintCommand
{
protected static $defaultName = 'lint:twig';
protected static $defaultDescription = 'Lints a template and outputs encountered errors';

/**
* {@inheritdoc}
*/
Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Component/Console/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ CHANGELOG
* Add `InputOption::VALUE_NEGATABLE` flag to handle `--foo`/`--no-foo` options
* Add the `Command::$defaultDescription` static property and the `description` attribute
on the `console.command` tag to allow the `list` command to instantiate commands lazily
* Add option `--short` to the `list` command

5.2.0
-----
Expand Down
2 changes: 2 additions & 0 deletions src/Symfony/Component/Console/Command/ListCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ protected function configure()
new InputArgument('namespace', InputArgument::OPTIONAL, 'The namespace name'),
new InputOption('raw', null, InputOption::VALUE_NONE, 'To output raw command list'),
new InputOption('format', null, InputOption::VALUE_REQUIRED, 'The output format (txt, xml, json, or md)', 'txt'),
new InputOption('short', null, InputOption::VALUE_NONE, 'To skip describing commands\' arguments'),
])
->setDescription('Lists commands')
->setHelp(<<<'EOF'
Expand Down Expand Up @@ -68,6 +69,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
'format' => $input->getOption('format'),
'raw_text' => $input->getOption('raw'),
'namespace' => $input->getArgument('namespace'),
'short' => $input->getOption('short'),
]);

return 0;
Expand Down
32 changes: 22 additions & 10 deletions src/Symfony/Component/Console/Descriptor/JsonDescriptor.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ protected function describeInputDefinition(InputDefinition $definition, array $o
*/
protected function describeCommand(Command $command, array $options = [])
{
$this->writeData($this->getCommandData($command), $options);
$this->writeData($this->getCommandData($command, $options['short'] ?? false), $options);
}

/**
Expand All @@ -71,7 +71,7 @@ protected function describeApplication(Application $application, array $options
$commands = [];

foreach ($description->getCommands() as $command) {
$commands[] = $this->getCommandData($command);
$commands[] = $this->getCommandData($command, $options['short'] ?? false);
}

$data = [];
Expand Down Expand Up @@ -153,17 +153,29 @@ private function getInputDefinitionData(InputDefinition $definition): array
return ['arguments' => $inputArguments, 'options' => $inputOptions];
}

private function getCommandData(Command $command): array
private function getCommandData(Command $command, bool $short = false): array
{
$command->mergeApplicationDefinition(false);

return [
$data = [
'name' => $command->getName(),
'usage' => array_merge([$command->getSynopsis()], $command->getUsages(), $command->getAliases()),
'description' => $command->getDescription(),
'help' => $command->getProcessedHelp(),
'definition' => $this->getInputDefinitionData($command->getDefinition()),
'hidden' => $command->isHidden(),
];

if ($short) {
$data += [
'usage' => $command->getAliases(),
];
} else {
$command->mergeApplicationDefinition(false);

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

$data['hidden'] = $command->isHidden();

return $data;
}
}
16 changes: 15 additions & 1 deletion src/Symfony/Component/Console/Descriptor/MarkdownDescriptor.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,20 @@ protected function describeInputDefinition(InputDefinition $definition, array $o
*/
protected function describeCommand(Command $command, array $options = [])
{
if ($options['short'] ?? false) {
$this->write(
'`'.$command->getName()."`\n"
.str_repeat('-', Helper::strlen($command->getName()) + 2)."\n\n"
.($command->getDescription() ? $command->getDescription()."\n\n" : '')
.'### Usage'."\n\n"
.array_reduce($command->getAliases(), function ($carry, $usage) {
return $carry.'* `'.$usage.'`'."\n";
})
);

return;
}

$command->mergeApplicationDefinition(false);

$this->write(
Expand Down Expand Up @@ -171,7 +185,7 @@ protected function describeApplication(Application $application, array $options

foreach ($description->getCommands() as $command) {
$this->write("\n\n");
if (null !== $describeCommand = $this->describeCommand($command)) {
if (null !== $describeCommand = $this->describeCommand($command, $options)) {
$this->write($describeCommand);
}
}
Expand Down
36 changes: 21 additions & 15 deletions src/Symfony/Component/Console/Descriptor/XmlDescriptor.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,36 +44,42 @@ public function getInputDefinitionDocument(InputDefinition $definition): \DOMDoc
return $dom;
}

public function getCommandDocument(Command $command): \DOMDocument
public function getCommandDocument(Command $command, bool $short = false): \DOMDocument
{
$dom = new \DOMDocument('1.0', 'UTF-8');
$dom->appendChild($commandXML = $dom->createElement('command'));

$command->mergeApplicationDefinition(false);

$commandXML->setAttribute('id', $command->getName());
$commandXML->setAttribute('name', $command->getName());
$commandXML->setAttribute('hidden', $command->isHidden() ? 1 : 0);

$commandXML->appendChild($usagesXML = $dom->createElement('usages'));

foreach (array_merge([$command->getSynopsis()], $command->getAliases(), $command->getUsages()) as $usage) {
$usagesXML->appendChild($dom->createElement('usage', $usage));
}

$commandXML->appendChild($descriptionXML = $dom->createElement('description'));
$descriptionXML->appendChild($dom->createTextNode(str_replace("\n", "\n ", $command->getDescription())));

$commandXML->appendChild($helpXML = $dom->createElement('help'));
$helpXML->appendChild($dom->createTextNode(str_replace("\n", "\n ", $command->getProcessedHelp())));
if ($short) {
foreach ($command->getAliases() as $usage) {
$usagesXML->appendChild($dom->createElement('usage', $usage));
}
} else {
$command->mergeApplicationDefinition(false);

$definitionXML = $this->getInputDefinitionDocument($command->getDefinition());
$this->appendDocument($commandXML, $definitionXML->getElementsByTagName('definition')->item(0));
foreach (array_merge([$command->getSynopsis()], $command->getAliases(), $command->getUsages()) as $usage) {
$usagesXML->appendChild($dom->createElement('usage', $usage));
}

$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));
}

return $dom;
}

public function getApplicationDocument(Application $application, string $namespace = null): \DOMDocument
public function getApplicationDocument(Application $application, string $namespace = null, bool $short = false): \DOMDocument
{
$dom = new \DOMDocument('1.0', 'UTF-8');
$dom->appendChild($rootXml = $dom->createElement('symfony'));
Expand All @@ -94,7 +100,7 @@ public function getApplicationDocument(Application $application, string $namespa
}

foreach ($description->getCommands() as $command) {
$this->appendDocument($commandsXML, $this->getCommandDocument($command));
$this->appendDocument($commandsXML, $this->getCommandDocument($command, $short));
}

if (!$namespace) {
Expand Down Expand Up @@ -143,15 +149,15 @@ protected function describeInputDefinition(InputDefinition $definition, array $o
*/
protected function describeCommand(Command $command, array $options = [])
{
$this->writeDocument($this->getCommandDocument($command));
$this->writeDocument($this->getCommandDocument($command, $options['short'] ?? false));
}

/**
* {@inheritdoc}
*/
protected function describeApplication(Application $application, array $options = [])
{
$this->writeDocument($this->getApplicationDocument($application, $options['namespace'] ?? null));
$this->writeDocument($this->getApplicationDocument($application, $options['namespace'] ?? null, $options['short'] ?? false));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public function testExecuteForApplicationCommandWithXmlOption()
$application = new Application();
$commandTester = new CommandTester($application->get('help'));
$commandTester->execute(['command_name' => 'list', '--format' => 'xml']);
$this->assertStringContainsString('list [--raw] [--format FORMAT] [--] [&lt;namespace&gt;]', $commandTester->getDisplay(), '->execute() returns a text help for the given command');
$this->assertStringContainsString('list [--raw] [--format FORMAT] [--short] [--] [&lt;namespace&gt;]', $commandTester->getDisplay(), '->execute() returns a text help for the given command');
$this->assertStringContainsString('<command', $commandTester->getDisplay(), '->execute() returns an XML help text if --format=xml is passed');
}
}
11 changes: 10 additions & 1 deletion src/Symfony/Component/Console/Tests/Fixtures/application_1.json
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@
"name": "list",
"hidden": false,
"usage": [
"list [--raw] [--format FORMAT] [--] [<namespace>]"
"list [--raw] [--format FORMAT] [--short] [--] [<namespace>]"
],
"description": "Lists commands",
"help": "The <info>list<\/info> command lists all commands:\n\n <info>app\/console list<\/info>\n\nYou can also display the commands for a specific namespace:\n\n <info>app\/console list test<\/info>\n\nYou can also output the information in other formats by using the <comment>--format<\/comment> option:\n\n <info>app\/console list --format=xml<\/info>\n\nIt's also possible to get raw list of commands (useful for embedding command runner):\n\n <info>app\/console list --raw<\/info>",
Expand Down Expand Up @@ -202,6 +202,15 @@
"is_multiple": false,
"description": "Do not ask any interactive question",
"default": false
},
"short": {
"name": "--short",
"shortcut": "",
"accept_value": false,
"is_value_required": false,
"is_multiple": false,
"description": "To skip describing commands' arguments",
"default": false
}
}
}
Expand Down
12 changes: 11 additions & 1 deletion src/Symfony/Component/Console/Tests/Fixtures/application_1.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ Lists commands

### Usage

* `list [--raw] [--format FORMAT] [--] [<namespace>]`
* `list [--raw] [--format FORMAT] [--short] [--] [<namespace>]`

The list command lists all commands:

Expand Down Expand Up @@ -172,6 +172,16 @@ The output format (txt, xml, json, or md)
* Is negatable: no
* Default: `'txt'`

#### `--short`

To skip describing commands' arguments

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

#### `--help|-h`

Display help for the given command. When no command is given display help for the list command
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
</command>
<command id="list" name="list" hidden="0">
<usages>
<usage>list [--raw] [--format FORMAT] [--] [&lt;namespace&gt;]</usage>
<usage>list [--raw] [--format FORMAT] [--short] [--] [&lt;namespace&gt;]</usage>
</usages>
<description>Lists commands</description>
<help>The &lt;info&gt;list&lt;/info&gt; command lists all commands:
Expand Down Expand Up @@ -92,6 +92,9 @@
<default>txt</default>
</defaults>
</option>
<option name="--short" shortcut="" accept_value="0" is_value_required="0" is_multiple="0">
<description>To skip describing commands' arguments</description>
</option>
<option name="--help" shortcut="-h" accept_value="0" is_value_required="0" is_multiple="0">
<description>Display help for the given command. When no command is given display help for the &lt;info&gt;list&lt;/info&gt; command</description>
</option>
Expand Down
11 changes: 10 additions & 1 deletion src/Symfony/Component/Console/Tests/Fixtures/application_2.json
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@
"name": "list",
"hidden": false,
"usage": [
"list [--raw] [--format FORMAT] [--] [<namespace>]"
"list [--raw] [--format FORMAT] [--short] [--] [<namespace>]"
],
"description": "Lists commands",
"help": "The <info>list<\/info> command lists all commands:\n\n <info>app\/console list<\/info>\n\nYou can also display the commands for a specific namespace:\n\n <info>app\/console list test<\/info>\n\nYou can also output the information in other formats by using the <comment>--format<\/comment> option:\n\n <info>app\/console list --format=xml<\/info>\n\nIt's also possible to get raw list of commands (useful for embedding command runner):\n\n <info>app\/console list --raw<\/info>",
Expand Down Expand Up @@ -206,6 +206,15 @@
"is_multiple": false,
"description": "Do not ask any interactive question",
"default": false
},
"short": {
"name": "--short",
"shortcut": "",
"accept_value": false,
"is_value_required": false,
"is_multiple": false,
"description": "To skip describing commands' arguments",
"default": false
}
}
}
Expand Down
12 changes: 11 additions & 1 deletion src/Symfony/Component/Console/Tests/Fixtures/application_2.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ Lists commands

### Usage

* `list [--raw] [--format FORMAT] [--] [<namespace>]`
* `list [--raw] [--format FORMAT] [--short] [--] [<namespace>]`

The list command lists all commands:

Expand Down Expand Up @@ -185,6 +185,16 @@ The output format (txt, xml, json, or md)
* Is negatable: no
* Default: `'txt'`

#### `--short`

To skip describing commands' arguments

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

#### `--help|-h`

Display help for the given command. When no command is given display help for the list command
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
</command>
<command id="list" name="list" hidden="0">
<usages>
<usage>list [--raw] [--format FORMAT] [--] [&lt;namespace&gt;]</usage>
<usage>list [--raw] [--format FORMAT] [--short] [--] [&lt;namespace&gt;]</usage>
</usages>
<description>Lists commands</description>
<help>The &lt;info&gt;list&lt;/info&gt; command lists all commands:
Expand Down Expand Up @@ -92,6 +92,9 @@
<default>txt</default>
</defaults>
</option>
<option name="--short" shortcut="" accept_value="0" is_value_required="0" is_multiple="0">
<description>To skip describing commands' arguments</description>
</option>
<option name="--help" shortcut="-h" accept_value="0" is_value_required="0" is_multiple="0">
<description>Display help for the given command. When no command is given display help for the &lt;info&gt;list&lt;/info&gt; command</description>
</option>
Expand Down
Loading