Skip to content

Commit 81d5728

Browse files
[Console] add option --short to the list command
1 parent 4cb45fe commit 81d5728

20 files changed

+132
-35
lines changed

src/Symfony/Bundle/FrameworkBundle/Command/DebugAutowiringCommand.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ class DebugAutowiringCommand extends ContainerDebugCommand
3131
{
3232
protected static $defaultName = 'debug:autowiring';
3333
protected static $defaultDescription = 'Lists classes/interfaces you can use for autowiring';
34+
3435
private $supportsHref;
3536
private $fileLinkFormatter;
3637

src/Symfony/Bundle/FrameworkBundle/Command/XliffLintCommand.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
class XliffLintCommand extends BaseLintCommand
2626
{
2727
protected static $defaultName = 'lint:xliff';
28+
protected static $defaultDescription = 'Lints a XLIFF file and outputs encountered errors';
2829

2930
public function __construct()
3031
{

src/Symfony/Bundle/FrameworkBundle/Command/YamlLintCommand.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
class YamlLintCommand extends BaseLintCommand
2525
{
2626
protected static $defaultName = 'lint:yaml';
27+
protected static $defaultDescription = 'Lints a file and outputs encountered errors';
2728

2829
public function __construct()
2930
{

src/Symfony/Bundle/TwigBundle/Command/LintCommand.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222
*/
2323
final class LintCommand extends BaseLintCommand
2424
{
25+
protected static $defaultName = 'lint:twig';
26+
protected static $defaultDescription = 'Lints a template and outputs encountered errors';
27+
2528
/**
2629
* {@inheritdoc}
2730
*/

src/Symfony/Component/Console/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ CHANGELOG
88
* Add `InputOption::VALUE_NEGATABLE` flag to handle `--foo`/`--no-foo` options
99
* Add the `Command::$defaultDescription` static property and the `description` attribute
1010
on the `console.command` tag to allow the `list` command to instantiate commands lazily
11+
* Add option `--short` to the `list` command
1112

1213
5.2.0
1314
-----

src/Symfony/Component/Console/Command/ListCommand.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ protected function configure()
3535
new InputArgument('namespace', InputArgument::OPTIONAL, 'The namespace name'),
3636
new InputOption('raw', null, InputOption::VALUE_NONE, 'To output raw command list'),
3737
new InputOption('format', null, InputOption::VALUE_REQUIRED, 'The output format (txt, xml, json, or md)', 'txt'),
38+
new InputOption('short', null, InputOption::VALUE_NONE, 'To skip describing commands\' arguments'),
3839
])
3940
->setDescription('Lists commands')
4041
->setHelp(<<<'EOF'
@@ -68,6 +69,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
6869
'format' => $input->getOption('format'),
6970
'raw_text' => $input->getOption('raw'),
7071
'namespace' => $input->getArgument('namespace'),
72+
'short' => $input->getOption('short'),
7173
]);
7274

7375
return 0;

src/Symfony/Component/Console/Descriptor/JsonDescriptor.php

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ protected function describeInputDefinition(InputDefinition $definition, array $o
5858
*/
5959
protected function describeCommand(Command $command, array $options = [])
6060
{
61-
$this->writeData($this->getCommandData($command), $options);
61+
$this->writeData($this->getCommandData($command, $options['short'] ?? false), $options);
6262
}
6363

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

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

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

156-
private function getCommandData(Command $command): array
156+
private function getCommandData(Command $command, bool $short = false): array
157157
{
158-
$command->mergeApplicationDefinition(false);
159-
160-
return [
158+
$data = [
161159
'name' => $command->getName(),
162-
'usage' => array_merge([$command->getSynopsis()], $command->getUsages(), $command->getAliases()),
163160
'description' => $command->getDescription(),
164-
'help' => $command->getProcessedHelp(),
165-
'definition' => $this->getInputDefinitionData($command->getDefinition()),
166-
'hidden' => $command->isHidden(),
167161
];
162+
163+
if ($short) {
164+
$data += [
165+
'usage' => $command->getAliases(),
166+
];
167+
} else {
168+
$command->mergeApplicationDefinition(false);
169+
170+
$data += [
171+
'usage' => array_merge([$command->getSynopsis()], $command->getUsages(), $command->getAliases()),
172+
'help' => $command->getProcessedHelp(),
173+
'definition' => $this->getInputDefinitionData($command->getDefinition()),
174+
];
175+
}
176+
177+
$data['hidden'] = $command->isHidden();
178+
179+
return $data;
168180
}
169181
}

src/Symfony/Component/Console/Descriptor/MarkdownDescriptor.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,20 @@ protected function describeInputDefinition(InputDefinition $definition, array $o
122122
*/
123123
protected function describeCommand(Command $command, array $options = [])
124124
{
125+
if ($options['short'] ?? false) {
126+
$this->write(
127+
'`'.$command->getName()."`\n"
128+
.str_repeat('-', Helper::strlen($command->getName()) + 2)."\n\n"
129+
.($command->getDescription() ? $command->getDescription()."\n\n" : '')
130+
.'### Usage'."\n\n"
131+
.array_reduce($command->getAliases(), function ($carry, $usage) {
132+
return $carry.'* `'.$usage.'`'."\n";
133+
})
134+
);
135+
136+
return;
137+
}
138+
125139
$command->mergeApplicationDefinition(false);
126140

127141
$this->write(
@@ -171,7 +185,7 @@ protected function describeApplication(Application $application, array $options
171185

172186
foreach ($description->getCommands() as $command) {
173187
$this->write("\n\n");
174-
if (null !== $describeCommand = $this->describeCommand($command)) {
188+
if (null !== $describeCommand = $this->describeCommand($command, $options)) {
175189
$this->write($describeCommand);
176190
}
177191
}

src/Symfony/Component/Console/Descriptor/XmlDescriptor.php

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -44,36 +44,42 @@ public function getInputDefinitionDocument(InputDefinition $definition): \DOMDoc
4444
return $dom;
4545
}
4646

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

52-
$command->mergeApplicationDefinition(false);
53-
5452
$commandXML->setAttribute('id', $command->getName());
5553
$commandXML->setAttribute('name', $command->getName());
5654
$commandXML->setAttribute('hidden', $command->isHidden() ? 1 : 0);
5755

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

60-
foreach (array_merge([$command->getSynopsis()], $command->getAliases(), $command->getUsages()) as $usage) {
61-
$usagesXML->appendChild($dom->createElement('usage', $usage));
62-
}
63-
6458
$commandXML->appendChild($descriptionXML = $dom->createElement('description'));
6559
$descriptionXML->appendChild($dom->createTextNode(str_replace("\n", "\n ", $command->getDescription())));
6660

67-
$commandXML->appendChild($helpXML = $dom->createElement('help'));
68-
$helpXML->appendChild($dom->createTextNode(str_replace("\n", "\n ", $command->getProcessedHelp())));
61+
if ($short) {
62+
foreach ($command->getAliases() as $usage) {
63+
$usagesXML->appendChild($dom->createElement('usage', $usage));
64+
}
65+
} else {
66+
$command->mergeApplicationDefinition(false);
6967

70-
$definitionXML = $this->getInputDefinitionDocument($command->getDefinition());
71-
$this->appendDocument($commandXML, $definitionXML->getElementsByTagName('definition')->item(0));
68+
foreach (array_merge([$command->getSynopsis()], $command->getAliases(), $command->getUsages()) as $usage) {
69+
$usagesXML->appendChild($dom->createElement('usage', $usage));
70+
}
71+
72+
$commandXML->appendChild($helpXML = $dom->createElement('help'));
73+
$helpXML->appendChild($dom->createTextNode(str_replace("\n", "\n ", $command->getProcessedHelp())));
74+
75+
$definitionXML = $this->getInputDefinitionDocument($command->getDefinition());
76+
$this->appendDocument($commandXML, $definitionXML->getElementsByTagName('definition')->item(0));
77+
}
7278

7379
return $dom;
7480
}
7581

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

96102
foreach ($description->getCommands() as $command) {
97-
$this->appendDocument($commandsXML, $this->getCommandDocument($command));
103+
$this->appendDocument($commandsXML, $this->getCommandDocument($command, $short));
98104
}
99105

100106
if (!$namespace) {
@@ -143,15 +149,15 @@ protected function describeInputDefinition(InputDefinition $definition, array $o
143149
*/
144150
protected function describeCommand(Command $command, array $options = [])
145151
{
146-
$this->writeDocument($this->getCommandDocument($command));
152+
$this->writeDocument($this->getCommandDocument($command, $options['short'] ?? false));
147153
}
148154

149155
/**
150156
* {@inheritdoc}
151157
*/
152158
protected function describeApplication(Application $application, array $options = [])
153159
{
154-
$this->writeDocument($this->getApplicationDocument($application, $options['namespace'] ?? null));
160+
$this->writeDocument($this->getApplicationDocument($application, $options['namespace'] ?? null, $options['short'] ?? false));
155161
}
156162

157163
/**

src/Symfony/Component/Console/Tests/Command/HelpCommandTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public function testExecuteForApplicationCommandWithXmlOption()
6565
$application = new Application();
6666
$commandTester = new CommandTester($application->get('help'));
6767
$commandTester->execute(['command_name' => 'list', '--format' => 'xml']);
68-
$this->assertStringContainsString('list [--raw] [--format FORMAT] [--] [&lt;namespace&gt;]', $commandTester->getDisplay(), '->execute() returns a text help for the given command');
68+
$this->assertStringContainsString('list [--raw] [--format FORMAT] [--short] [--] [&lt;namespace&gt;]', $commandTester->getDisplay(), '->execute() returns a text help for the given command');
6969
$this->assertStringContainsString('<command', $commandTester->getDisplay(), '->execute() returns an XML help text if --format=xml is passed');
7070
}
7171
}

0 commit comments

Comments
 (0)