Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 332b4ef

Browse filesBrowse files
committed
[Console] Add of hidden and deprecation option flags
1 parent 3f2ed0f commit 332b4ef
Copy full SHA for 332b4ef

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Dismiss banner

50 files changed

+835
-70
lines changed
+7Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
; Unix-style newlines
2+
[Tests/Fixtures/*.{json,md,rst,txt}]
3+
trim_trailing_whitespace = false
4+
5+
[Tests/Fixtures/*.xml]
6+
trim_trailing_whitespace = false
7+
indent_size = 2

‎src/Symfony/Component/Console/CHANGELOG.md

Copy file name to clipboardExpand all lines: src/Symfony/Component/Console/CHANGELOG.md
+2Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ CHANGELOG
55
---
66

77
* Add `ArgvInput::getRawTokens()`
8+
* Add `InputOption::HIDDEN` flag to hide options
9+
* Add `InputOption::DEPRECATED` flag to mark options as deprecated
810

911
7.0
1012
---

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Console/Command/Command.php
+28-1Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,8 @@ public function run(InputInterface $input, OutputInterface $output): int
235235

236236
// bind the input against the command specific arguments/options
237237
try {
238-
$input->bind($this->getDefinition());
238+
$inputDefinition = $this->getDefinition();
239+
$input->bind($inputDefinition);
239240
} catch (ExceptionInterface $e) {
240241
if (!$this->ignoreValidationErrors) {
241242
throw $e;
@@ -273,6 +274,10 @@ public function run(InputInterface $input, OutputInterface $output): int
273274

274275
$input->validate();
275276

277+
if (isset($inputDefinition)) {
278+
$this->printDeprecationMessages($inputDefinition, $input, $output);
279+
}
280+
276281
if ($this->code) {
277282
$statusCode = ($this->code)($input, $output);
278283
} else {
@@ -648,6 +653,28 @@ public function getHelper(string $name): HelperInterface
648653
return $this->helperSet->get($name);
649654
}
650655

656+
private function printDeprecationMessages(InputDefinition $inputDefinition, InputInterface $input, OutputInterface $output): void
657+
{
658+
$deprecationMessages = [];
659+
foreach ($inputDefinition->getOptions() as $inputOption) {
660+
if ($inputOption->isDeprecated()) {
661+
try {
662+
$optionName = $inputOption->getName();
663+
$optionValue = $input->getOption($optionName);
664+
if (isset($optionValue) && $optionValue !== $inputOption->getDefault()) {
665+
$deprecationMessages[] = sprintf('The option "--%s" is deprecated.', $optionName);
666+
}
667+
} catch (\InvalidArgumentException $exception) {
668+
// option not used, ignore
669+
}
670+
}
671+
}
672+
if (!empty($deprecationMessages)) {
673+
$formatter = $this->getHelper('formatter');
674+
$output->writeln($formatter->formatBlock($deprecationMessages, 'fg=black;bg=yellow', true));
675+
}
676+
}
677+
651678
/**
652679
* Validates a command name.
653680
*

‎src/Symfony/Component/Console/Command/HelpCommand.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Console/Command/HelpCommand.php
+2Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ protected function configure(): void
3737
new InputArgument('command_name', InputArgument::OPTIONAL, 'The command name', 'help', fn () => array_keys((new ApplicationDescription($this->getApplication()))->getCommands())),
3838
new InputOption('format', null, InputOption::VALUE_REQUIRED, 'The output format (txt, xml, json, or md)', 'txt', fn () => (new DescriptorHelper())->getFormats()),
3939
new InputOption('raw', null, InputOption::VALUE_NONE, 'To output raw command help'),
40+
new InputOption('show-hidden-options', null, InputOption::VALUE_NONE | InputOption::HIDDEN, 'Show hidden options'),
4041
])
4142
->setDescription('Display help for a command')
4243
->setHelp(<<<'EOF'
@@ -67,6 +68,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
6768
$helper->describe($output, $this->command, [
6869
'format' => $input->getOption('format'),
6970
'raw_text' => $input->getOption('raw'),
71+
'show-hidden-options' => $input->getOption('show-hidden-options'),
7072
]);
7173

7274
unset($this->command);

‎src/Symfony/Component/Console/Completion/CompletionInput.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Console/Completion/CompletionInput.php
+9-3Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -198,11 +198,17 @@ private function getOptionFromToken(string $optionToken): ?InputOption
198198

199199
if ('-' === ($optionToken[1] ?? ' ')) {
200200
// long option name
201-
return $this->definition->hasOption($optionName) ? $this->definition->getOption($optionName) : null;
201+
if ($this->definition->hasOption($optionName) && !$this->definition->getOption($optionName)->isHidden()) {
202+
return $this->definition->getOption($optionName);
203+
}
204+
return null;
202205
}
203206

204207
// short option name
205-
return $this->definition->hasShortcut($optionName[0]) ? $this->definition->getOptionForShortcut($optionName[0]) : null;
208+
if ($this->definition->hasShortcut($optionName[0]) && !$this->definition->getOptionForShortcut($optionName[0])->isHidden()) {
209+
return $this->definition->getOptionForShortcut($optionName[0]);
210+
}
211+
return null;
206212
}
207213

208214
/**
@@ -226,7 +232,7 @@ private function isCursorFree(): bool
226232
return $this->currentIndex >= $nrOfTokens;
227233
}
228234

229-
public function __toString()
235+
public function __toString(): string
230236
{
231237
$str = '';
232238
foreach ($this->tokens as $i => $token) {

‎src/Symfony/Component/Console/Completion/CompletionSuggestions.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Console/Completion/CompletionSuggestions.php
+3-1Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,9 @@ public function suggestValues(array $values): static
5858
*/
5959
public function suggestOption(InputOption $option): static
6060
{
61-
$this->optionSuggestions[] = $option;
61+
if (!$option->isHidden()) {
62+
$this->optionSuggestions[] = $option;
63+
}
6264

6365
return $this;
6466
}

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Console/Descriptor/Descriptor.php
+18Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,24 @@ public function describe(OutputInterface $output, object $object, array $options
4242
};
4343
}
4444

45+
/**
46+
* Filter hidden options from list.
47+
* @param array<string,InputOption> $inputOptions
48+
* @return array<string,InputOption>
49+
*/
50+
protected function removeHiddenOptions(array $inputOptions, array $options = []): array
51+
{
52+
return array_filter($inputOptions, fn(InputOption $option) => !$this->skipHiddenOption($option, $options));
53+
}
54+
55+
/**
56+
* Should InputOption be skipped?
57+
*/
58+
protected function skipHiddenOption(InputOption $inputOption, array $options = []): bool
59+
{
60+
return $inputOption->isHidden() && !($options['show-hidden-options'] ?? false);
61+
}
62+
4563
protected function write(string $content, bool $decorated = false): void
4664
{
4765
$this->output->write($content, false, $decorated ? OutputInterface::OUTPUT_NORMAL : OutputInterface::OUTPUT_RAW);

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Console/Descriptor/JsonDescriptor.php
+23-12Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ protected function describeInputArgument(InputArgument $argument, array $options
3333

3434
protected function describeInputOption(InputOption $option, array $options = []): void
3535
{
36+
if ($this->skipHiddenOption($option, $options)) {
37+
return;
38+
}
3639
$this->writeData($this->getInputOptionData($option), $options);
3740
if ($option->isNegatable()) {
3841
$this->writeData($this->getInputOptionData($option, true), $options);
@@ -41,12 +44,12 @@ protected function describeInputOption(InputOption $option, array $options = [])
4144

4245
protected function describeInputDefinition(InputDefinition $definition, array $options = []): void
4346
{
44-
$this->writeData($this->getInputDefinitionData($definition), $options);
47+
$this->writeData($this->getInputDefinitionData($definition, $options), $options);
4548
}
4649

4750
protected function describeCommand(Command $command, array $options = []): void
4851
{
49-
$this->writeData($this->getCommandData($command, $options['short'] ?? false), $options);
52+
$this->writeData($this->getCommandData($command, $options), $options);
5053
}
5154

5255
protected function describeApplication(Application $application, array $options = []): void
@@ -56,7 +59,7 @@ protected function describeApplication(Application $application, array $options
5659
$commands = [];
5760

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

6265
$data = [];
@@ -101,34 +104,40 @@ private function getInputArgumentData(InputArgument $argument): array
101104

102105
private function getInputOptionData(InputOption $option, bool $negated = false): array
103106
{
104-
return $negated ? [
105-
'name' => '--no-'.$option->getName(),
107+
$data = $negated ? [
108+
'name' => '--no-' . $option->getName(),
106109
'shortcut' => '',
107110
'accept_value' => false,
108111
'is_value_required' => false,
109112
'is_multiple' => false,
110-
'description' => 'Negate the "--'.$option->getName().'" option',
113+
'is_deprecated' => $option->isDeprecated(),
114+
'description' => 'Negate the "--' . $option->getName() . '" option',
111115
'default' => false,
112116
] : [
113-
'name' => '--'.$option->getName(),
114-
'shortcut' => $option->getShortcut() ? '-'.str_replace('|', '|-', $option->getShortcut()) : '',
117+
'name' => '--' . $option->getName(),
118+
'shortcut' => $option->getShortcut() ? '-' . str_replace('|', '|-', $option->getShortcut()) : '',
115119
'accept_value' => $option->acceptValue(),
116120
'is_value_required' => $option->isValueRequired(),
117121
'is_multiple' => $option->isArray(),
122+
'is_deprecated' => $option->isDeprecated(),
118123
'description' => preg_replace('/\s*[\r\n]\s*/', ' ', $option->getDescription()),
119124
'default' => \INF === $option->getDefault() ? 'INF' : $option->getDefault(),
120125
];
126+
if (!$option->isDeprecated()) {
127+
unset($data['is_deprecated']);
128+
}
129+
return $data;
121130
}
122131

123-
private function getInputDefinitionData(InputDefinition $definition): array
132+
private function getInputDefinitionData(InputDefinition $definition, array $options): array
124133
{
125134
$inputArguments = [];
126135
foreach ($definition->getArguments() as $name => $argument) {
127136
$inputArguments[$name] = $this->getInputArgumentData($argument);
128137
}
129138

130139
$inputOptions = [];
131-
foreach ($definition->getOptions() as $name => $option) {
140+
foreach ($this->removeHiddenOptions($definition->getOptions(), $options) as $name => $option) {
132141
$inputOptions[$name] = $this->getInputOptionData($option);
133142
if ($option->isNegatable()) {
134143
$inputOptions['no-'.$name] = $this->getInputOptionData($option, true);
@@ -138,8 +147,10 @@ private function getInputDefinitionData(InputDefinition $definition): array
138147
return ['arguments' => $inputArguments, 'options' => $inputOptions];
139148
}
140149

141-
private function getCommandData(Command $command, bool $short = false): array
150+
private function getCommandData(Command $command, array $options = []): array
142151
{
152+
$short = $options['short'] ?? false;
153+
143154
$data = [
144155
'name' => $command->getName(),
145156
'description' => $command->getDescription(),
@@ -155,7 +166,7 @@ private function getCommandData(Command $command, bool $short = false): array
155166
$data += [
156167
'usage' => array_merge([$command->getSynopsis()], $command->getUsages(), $command->getAliases()),
157168
'help' => $command->getProcessedHelp(),
158-
'definition' => $this->getInputDefinitionData($command->getDefinition()),
169+
'definition' => $this->getInputDefinitionData($command->getDefinition(), $options),
159170
];
160171
}
161172

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Console/Descriptor/MarkdownDescriptor.php
+8-6Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ protected function describeInputOption(InputOption $option, array $options = [])
7070
.'* Accept value: '.($option->acceptValue() ? 'yes' : 'no')."\n"
7171
.'* Is value required: '.($option->isValueRequired() ? 'yes' : 'no')."\n"
7272
.'* Is multiple: '.($option->isArray() ? 'yes' : 'no')."\n"
73+
.($option->isDeprecated() ? ('* Is deprecated: yes'."\n") : '')
7374
.'* Is negatable: '.($option->isNegatable() ? 'yes' : 'no')."\n"
7475
.'* Default: `'.str_replace("\n", '', var_export($option->getDefault(), true)).'`'
7576
);
@@ -81,19 +82,20 @@ protected function describeInputDefinition(InputDefinition $definition, array $o
8182
$this->write('### Arguments');
8283
foreach ($definition->getArguments() as $argument) {
8384
$this->write("\n\n");
84-
$this->describeInputArgument($argument);
85+
$this->describeInputArgument($argument, $options);
8586
}
8687
}
8788

88-
if (\count($definition->getOptions()) > 0) {
89+
$inputOptions = $this->removeHiddenOptions($definition->getOptions(), $options);
90+
if (!empty($inputOptions)) {
8991
if ($showArguments) {
9092
$this->write("\n\n");
9193
}
9294

9395
$this->write('### Options');
94-
foreach ($definition->getOptions() as $option) {
96+
foreach ($inputOptions as $option) {
9597
$this->write("\n\n");
96-
$this->describeInputOption($option);
98+
$this->describeInputOption($option, $options);
9799
}
98100
}
99101
}
@@ -128,9 +130,9 @@ protected function describeCommand(Command $command, array $options = []): void
128130
}
129131

130132
$definition = $command->getDefinition();
131-
if ($definition->getOptions() || $definition->getArguments()) {
133+
if ($this->removeHiddenOptions($definition->getOptions(), $options) || $definition->getArguments()) {
132134
$this->write("\n\n");
133-
$this->describeInputDefinition($definition);
135+
$this->describeInputDefinition($definition, $options);
134136
}
135137
}
136138

‎src/Symfony/Component/Console/Descriptor/ReStructuredTextDescriptor.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Console/Descriptor/ReStructuredTextDescriptor.php
+9-7Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ protected function describeInputOption(InputOption $option, array $options = [])
8484
.'- **Accept value**: '.($option->acceptValue() ? 'yes' : 'no')."\n"
8585
.'- **Is value required**: '.($option->isValueRequired() ? 'yes' : 'no')."\n"
8686
.'- **Is multiple**: '.($option->isArray() ? 'yes' : 'no')."\n"
87+
.($option->isDeprecated() ? ('- **Is deprecated**: yes'."\n") : '')
8788
.'- **Is negatable**: '.($option->isNegatable() ? 'yes' : 'no')."\n"
8889
.'- **Default**: ``'.str_replace("\n", '', var_export($option->getDefault(), true)).'``'."\n"
8990
);
@@ -95,18 +96,19 @@ protected function describeInputDefinition(InputDefinition $definition, array $o
9596
$this->write("Arguments\n".str_repeat($this->subsubsectionChar, 9))."\n\n";
9697
foreach ($definition->getArguments() as $argument) {
9798
$this->write("\n\n");
98-
$this->describeInputArgument($argument);
99+
$this->describeInputArgument($argument, $options);
99100
}
100101
}
101102

102-
if ($nonDefaultOptions = $this->getNonDefaultOptions($definition)) {
103+
$inputOptions = $this->removeHiddenOptions($this->getNonDefaultOptions($definition), $options);
104+
if (!empty($inputOptions)) {
103105
if ($showArguments) {
104106
$this->write("\n\n");
105107
}
106108

107-
$this->write("Options\n".str_repeat($this->subsubsectionChar, 7)."\n\n");
108-
foreach ($nonDefaultOptions as $option) {
109-
$this->describeInputOption($option);
109+
$this->write("Options\n" . str_repeat($this->subsubsectionChar, 7) . "\n\n");
110+
foreach ($inputOptions as $option) {
111+
$this->describeInputOption($option, $options);
110112
$this->write("\n");
111113
}
112114
}
@@ -145,9 +147,9 @@ protected function describeCommand(Command $command, array $options = []): void
145147
}
146148

147149
$definition = $command->getDefinition();
148-
if ($definition->getOptions() || $definition->getArguments()) {
150+
if ($this->removeHiddenOptions($definition->getOptions(), $options) || $definition->getArguments()) {
149151
$this->write("\n\n");
150-
$this->describeInputDefinition($definition);
152+
$this->describeInputDefinition($definition, $options);
151153
}
152154
}
153155

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.