Skip to content

Commit 848f855

Browse files
committed
Unify how --format is handle by commands
1 parent ce67e41 commit 848f855

File tree

19 files changed

+107
-32
lines changed

19 files changed

+107
-32
lines changed

src/Symfony/Bridge/Twig/Command/DebugCommand.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ protected function configure(): void
5757
->setDefinition([
5858
new InputArgument('name', InputArgument::OPTIONAL, 'The template name'),
5959
new InputOption('filter', null, InputOption::VALUE_REQUIRED, 'Show details for all entries matching this filter'),
60-
new InputOption('format', null, InputOption::VALUE_REQUIRED, \sprintf('The output format ("%s")', implode('", "', $this->getAvailableFormatOptions())), 'text'),
60+
new InputOption('format', null, InputOption::VALUE_REQUIRED, \sprintf('The output format ("%s")', implode('", "', $this->getAvailableFormatOptions())), 'txt'),
6161
])
6262
->setHelp(<<<'EOF'
6363
The <info>%command.name%</info> command outputs a list of twig functions,
@@ -93,8 +93,14 @@ protected function execute(InputInterface $input, OutputInterface $output): int
9393
throw new InvalidArgumentException(\sprintf('Argument "name" not supported, it requires the Twig loader "%s".', FilesystemLoader::class));
9494
}
9595

96-
match ($input->getOption('format')) {
97-
'text' => $name ? $this->displayPathsText($io, $name) : $this->displayGeneralText($io, $filter),
96+
$format = $input->getOption('format');
97+
if ('text' === $format) {
98+
trigger_deprecation('symfony/twig-bridge', '7.2', 'The "text" format is deprecated, use "txt" instead.');
99+
100+
$format = 'txt';
101+
}
102+
match ($format) {
103+
'txt' => $name ? $this->displayPathsText($io, $name) : $this->displayGeneralText($io, $filter),
98104
'json' => $name ? $this->displayPathsJson($io, $name) : $this->displayGeneralJson($io, $filter),
99105
default => throw new InvalidArgumentException(\sprintf('Supported formats are "%s".', implode('", "', $this->getAvailableFormatOptions()))),
100106
};
@@ -582,8 +588,9 @@ private function getFileLink(string $absolutePath): string
582588
return (string) $this->fileLinkFormatter?->format($absolutePath, 1);
583589
}
584590

591+
/** @return string[] */
585592
private function getAvailableFormatOptions(): array
586593
{
587-
return ['text', 'json'];
594+
return ['txt', 'json'];
588595
}
589596
}

src/Symfony/Bridge/Twig/Command/LintCommand.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,10 @@ protected function configure(): void
7171
Or of a whole directory:
7272
7373
<info>php %command.full_name% dirname</info>
74-
<info>php %command.full_name% dirname --format=json</info>
7574
75+
The <info>--format</info> option specifies the format of the command output:
76+
77+
<info>php %command.full_name% dirname --format=json</info>
7678
EOF
7779
)
7880
;
@@ -280,6 +282,7 @@ public function complete(CompletionInput $input, CompletionSuggestions $suggesti
280282
}
281283
}
282284

285+
/** @return string[] */
283286
private function getAvailableFormatOptions(): array
284287
{
285288
return ['txt', 'json', 'github'];

src/Symfony/Bridge/Twig/Tests/Command/DebugCommandTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ public function testComplete(array $input, array $expectedSuggestions)
314314
public static function provideCompletionSuggestions(): iterable
315315
{
316316
yield 'name' => [['email'], []];
317-
yield 'option --format' => [['--format', ''], ['text', 'json']];
317+
yield 'option --format' => [['--format', ''], ['txt', 'json']];
318318
}
319319

320320
private function createCommandTester(array $paths = [], array $bundleMetadata = [], ?string $defaultPath = null, bool $useChainLoader = false, array $globals = []): CommandTester

src/Symfony/Bridge/Twig/composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
],
1818
"require": {
1919
"php": ">=8.2",
20+
"symfony/deprecation-contracts": "^2.5|^3",
2021
"symfony/translation-contracts": "^2.5|^3",
2122
"twig/twig": "^3.9"
2223
},

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

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,6 @@ class ConfigDebugCommand extends AbstractConfigCommand
4141
{
4242
protected function configure(): void
4343
{
44-
$commentedHelpFormats = array_map(fn ($format) => \sprintf('<comment>%s</comment>', $format), $this->getAvailableFormatOptions());
45-
$helpFormats = implode('", "', $commentedHelpFormats);
46-
4744
$this
4845
->setDefinition([
4946
new InputArgument('name', InputArgument::OPTIONAL, 'The bundle name or the extension alias'),
@@ -60,8 +57,7 @@ protected function configure(): void
6057
<info>php %command.full_name% framework</info>
6158
<info>php %command.full_name% FrameworkBundle</info>
6259
63-
The <info>--format</info> option specifies the format of the configuration,
64-
these are "{$helpFormats}".
60+
The <info>--format</info> option specifies the format of the command output:
6561
6662
<info>php %command.full_name% framework --format=json</info>
6763
@@ -268,6 +264,7 @@ private static function buildPathsCompletion(array $paths, string $prefix = ''):
268264
return $completionPaths;
269265
}
270266

267+
/** @return string[] */
271268
private function getAvailableFormatOptions(): array
272269
{
273270
return ['txt', 'yaml', 'json'];

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

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,6 @@ class ConfigDumpReferenceCommand extends AbstractConfigCommand
3939
{
4040
protected function configure(): void
4141
{
42-
$commentedHelpFormats = array_map(fn ($format) => \sprintf('<comment>%s</comment>', $format), $this->getAvailableFormatOptions());
43-
$helpFormats = implode('", "', $commentedHelpFormats);
44-
4542
$this
4643
->setDefinition([
4744
new InputArgument('name', InputArgument::OPTIONAL, 'The Bundle name or the extension alias'),
@@ -57,10 +54,9 @@ protected function configure(): void
5754
<info>php %command.full_name% framework</info>
5855
<info>php %command.full_name% FrameworkBundle</info>
5956
60-
The <info>--format</info> option specifies the format of the configuration,
61-
these are "{$helpFormats}".
57+
The <info>--format</info> option specifies the format of the command output:
6258
63-
<info>php %command.full_name% FrameworkBundle --format=xml</info>
59+
<info>php %command.full_name% FrameworkBundle --format=json</info>
6460
6561
For dumping a specific option, add its path as second argument (only available for the yaml format):
6662
@@ -181,6 +177,7 @@ private function getAvailableBundles(): array
181177
return $bundles;
182178
}
183179

180+
/** @return string[] */
184181
private function getAvailableFormatOptions(): array
185182
{
186183
return ['yaml', 'xml'];

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,9 @@ protected function configure(): void
106106
107107
<info>php %command.full_name% --show-hidden</info>
108108
109+
The <info>--format</info> option specifies the format of the command output:
110+
111+
<info>php %command.full_name% --format=json</info>
109112
EOF
110113
)
111114
;
@@ -358,6 +361,7 @@ public function filterToServiceTypes(string $serviceId): bool
358361
return class_exists($serviceId) || interface_exists($serviceId, false);
359362
}
360363

364+
/** @return string[] */
361365
private function getAvailableFormatOptions(): array
362366
{
363367
return (new DescriptorHelper())->getFormats();

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@ protected function configure(): void
6060
To get specific listeners for an event, specify its name:
6161
6262
<info>php %command.full_name% kernel.request</info>
63+
64+
The <info>--format</info> option specifies the format of the command output:
65+
66+
<info>php %command.full_name% --format=json</info>
6367
EOF
6468
)
6569
;
@@ -153,6 +157,7 @@ private function searchForEvent(EventDispatcherInterface $dispatcher, string $ne
153157
return $output;
154158
}
155159

160+
/** @return string[] */
156161
private function getAvailableFormatOptions(): array
157162
{
158163
return (new DescriptorHelper())->getFormats();

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@ protected function configure(): void
6161
6262
<info>php %command.full_name%</info>
6363
64+
The <info>--format</info> option specifies the format of the command output:
65+
66+
<info>php %command.full_name% --format=json</info>
6467
EOF
6568
)
6669
;
@@ -164,6 +167,7 @@ private function findRouteContaining(string $name, RouteCollection $routes): Rou
164167
return $foundRoutes;
165168
}
166169

170+
/** @return string[] */
167171
private function getAvailableFormatOptions(): array
168172
{
169173
return (new DescriptorHelper())->getFormats();

src/Symfony/Component/AssetMapper/Command/ImportMapAuditCommand.php

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
use Symfony\Component\AssetMapper\ImportMap\ImportMapPackageAuditVulnerability;
1616
use Symfony\Component\Console\Attribute\AsCommand;
1717
use Symfony\Component\Console\Command\Command;
18+
use Symfony\Component\Console\Completion\CompletionInput;
19+
use Symfony\Component\Console\Completion\CompletionSuggestions;
1820
use Symfony\Component\Console\Input\InputInterface;
1921
use Symfony\Component\Console\Input\InputOption;
2022
use Symfony\Component\Console\Output\OutputInterface;
@@ -41,12 +43,19 @@ public function __construct(
4143

4244
protected function configure(): void
4345
{
44-
$this->addOption(
45-
name: 'format',
46-
mode: InputOption::VALUE_REQUIRED,
47-
description: \sprintf('The output format ("%s")', implode(', ', $this->getAvailableFormatOptions())),
48-
default: 'txt',
49-
);
46+
$this
47+
->addOption(
48+
name: 'format',
49+
mode: InputOption::VALUE_REQUIRED,
50+
description: \sprintf('The output format ("%s")', implode(', ', $this->getAvailableFormatOptions())),
51+
default: 'txt',
52+
)
53+
->setHelp(<<<'EOT'
54+
The <info>--format</info> option specifies the format of the command output:
55+
56+
<info>php %command.full_name% --format=json</info>
57+
EOT
58+
);
5059
}
5160

5261
protected function initialize(InputInterface $input, OutputInterface $output): void
@@ -180,6 +189,14 @@ private function displayJson(array $audit): int
180189
return 0 < array_sum($json['summary']) ? self::FAILURE : self::SUCCESS;
181190
}
182191

192+
public function complete(CompletionInput $input, CompletionSuggestions $suggestions): void
193+
{
194+
if ($input->mustSuggestOptionValuesFor('format')) {
195+
$suggestions->suggestValues($this->getAvailableFormatOptions());
196+
}
197+
}
198+
199+
/** @return string[] */
183200
private function getAvailableFormatOptions(): array
184201
{
185202
return ['txt', 'json'];

0 commit comments

Comments
 (0)