Skip to content

Navigation Menu

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 1a9a3c3

Browse filesBrowse files
committed
Implement negatable option
1 parent 8d455db commit 1a9a3c3
Copy full SHA for 1a9a3c3

Some content is hidden

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

46 files changed

+399
-408
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Console/Application.php
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1033,7 +1033,7 @@ protected function getDefaultInputDefinition()
10331033
new InputOption('--quiet', '-q', InputOption::VALUE_NONE, 'Do not output any message'),
10341034
new InputOption('--verbose', '-v|vv|vvv', InputOption::VALUE_NONE, 'Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug'),
10351035
new InputOption('--version', '-V', InputOption::VALUE_NONE, 'Display this application version'),
1036-
new InputOption('--ansi', '', InputOption::VALUE_BINARY, 'Force ANSI output', null),
1036+
new InputOption('--ansi', '', InputOption::VALUE_NEGATABLE, 'Force (or disable --no-ansi) ANSI output', null),
10371037
new InputOption('--no-interaction', '-n', InputOption::VALUE_NONE, 'Do not ask any interactive question'),
10381038
]);
10391039
}

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

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

77
* Added `GithubActionReporter` to render annotations in a Github Action
8+
* Added `InputOption::VALUE_NEGATABLE` flag to handle `--foo`/`--no-foo` options.
89

910
5.2.0
1011
-----

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Console/Descriptor/JsonDescriptor.php
+16-6Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ protected function describeInputArgument(InputArgument $argument, array $options
4040
protected function describeInputOption(InputOption $option, array $options = [])
4141
{
4242
$this->writeData($this->getInputOptionData($option), $options);
43+
if ($option->isNegatable()) {
44+
$this->writeData($this->getInputOptionData($option, true), $options);
45+
}
4346
}
4447

4548
/**
@@ -111,15 +114,22 @@ private function getInputArgumentData(InputArgument $argument): array
111114
];
112115
}
113116

114-
private function getInputOptionData(InputOption $option): array
117+
private function getInputOptionData(InputOption $option, bool $negated = false): array
115118
{
116-
return [
119+
return $negated ? [
120+
'name' => '--no-'.$option->getName(),
121+
'shortcut' => '',
122+
'accept_value' => false,
123+
'is_value_required' => false,
124+
'is_multiple' => false,
125+
'description' => 'Negate the "--'.$option->getName().'" option',
126+
'default' => false,
127+
] : [
117128
'name' => '--'.$option->getName(),
118129
'shortcut' => $option->getShortcut() ? '-'.str_replace('|', '|-', $option->getShortcut()) : '',
119130
'accept_value' => $option->acceptValue(),
120131
'is_value_required' => $option->isValueRequired(),
121132
'is_multiple' => $option->isArray(),
122-
'is_negatable' => $option->isNegatable(),
123133
'description' => preg_replace('/\s*[\r\n]\s*/', ' ', $option->getDescription()),
124134
'default' => \INF === $option->getDefault() ? 'INF' : $option->getDefault(),
125135
];
@@ -134,10 +144,10 @@ private function getInputDefinitionData(InputDefinition $definition): array
134144

135145
$inputOptions = [];
136146
foreach ($definition->getOptions() as $name => $option) {
137-
if ($option->isHidden()) {
138-
continue;
139-
}
140147
$inputOptions[$name] = $this->getInputOptionData($option);
148+
if ($option->isNegatable()) {
149+
$inputOptions['no-'.$name] = $this->getInputOptionData($option, true);
150+
}
141151
}
142152

143153
return ['arguments' => $inputArguments, 'options' => $inputOptions];

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Console/Descriptor/MarkdownDescriptor.php
+4-5Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,10 @@ protected function describeInputArgument(InputArgument $argument, array $options
6868
*/
6969
protected function describeInputOption(InputOption $option, array $options = [])
7070
{
71-
$negatable = $option->isNegatable() ? '[no-]' : '';
72-
$name = '--'.$negatable.$option->getName();
71+
$name = '--'.$option->getName();
72+
if ($option->isNegatable()) {
73+
$name .= '|--no-'.$option->getName();
74+
}
7375
if ($option->getShortcut()) {
7476
$name .= '|-'.str_replace('|', '|-', $option->getShortcut()).'';
7577
}
@@ -107,9 +109,6 @@ protected function describeInputDefinition(InputDefinition $definition, array $o
107109

108110
$this->write('### Options');
109111
foreach ($definition->getOptions() as $option) {
110-
if ($option->isHidden()) {
111-
continue;
112-
}
113112
$this->write("\n\n");
114113
if (null !== $describeInputOption = $this->describeInputOption($option)) {
115114
$this->write($describeInputOption);

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Console/Descriptor/TextDescriptor.php
+6-11Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,10 @@ protected function describeInputArgument(InputArgument $argument, array $options
5656
*/
5757
protected function describeInputOption(InputOption $option, array $options = [])
5858
{
59-
$default = '';
6059
if ($option->acceptValue() && null !== $option->getDefault() && (!\is_array($option->getDefault()) || \count($option->getDefault()))) {
6160
$default = sprintf('<comment> [default: %s]</comment>', $this->formatDefaultValue($option->getDefault()));
62-
} elseif ($option->isNegatable() && (null !== $option->getDefault())) {
63-
$negative_default = $option->getDefault() ? '' : 'no-';
64-
$default = sprintf('<comment> [default: --%s%s]</comment>', $negative_default, $option->getName());
61+
} else {
62+
$default = '';
6563
}
6664

6765
$value = '';
@@ -74,10 +72,9 @@ protected function describeInputOption(InputOption $option, array $options = [])
7472
}
7573

7674
$totalWidth = isset($options['total_width']) ? $options['total_width'] : $this->calculateTotalWidthForOptions([$option]);
77-
$negatable = $option->isNegatable() ? '[no-]' : '';
7875
$synopsis = sprintf('%s%s',
7976
$option->getShortcut() ? sprintf('-%s, ', $option->getShortcut()) : ' ',
80-
sprintf('--%s%s%s', $negatable, $option->getName(), $value)
77+
sprintf($option->isNegatable() ? '--%1$s|--no-%1$s' : '--%1$s%2$s', $option->getName(), $value)
8178
);
8279

8380
$spacingWidth = $totalWidth - Helper::strlen($synopsis);
@@ -120,9 +117,6 @@ protected function describeInputDefinition(InputDefinition $definition, array $o
120117

121118
$this->writeText('<comment>Options:</comment>', $options);
122119
foreach ($definition->getOptions() as $option) {
123-
if ($option->isHidden()) {
124-
continue;
125-
}
126120
if (\strlen($option->getShortcut()) > 1) {
127121
$laterOptions[] = $option;
128122
continue;
@@ -331,8 +325,9 @@ private function calculateTotalWidthForOptions(array $options): int
331325
foreach ($options as $option) {
332326
// "-" + shortcut + ", --" + name
333327
$nameLength = 1 + max(Helper::strlen($option->getShortcut()), 1) + 4 + Helper::strlen($option->getName());
334-
335-
if ($option->acceptValue()) {
328+
if ($option->isNegatable()) {
329+
$nameLength += 6 + Helper::strlen($option->getName()); // |--no- + name
330+
} elseif ($option->acceptValue()) {
336331
$valueLength = 1 + Helper::strlen($option->getName()); // = + value
337332
$valueLength += $option->isValueOptional() ? 2 : 0; // [ + ]
338333

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Console/Descriptor/XmlDescriptor.php
+12-4Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,7 @@ public function getInputDefinitionDocument(InputDefinition $definition): \DOMDoc
3838

3939
$definitionXML->appendChild($optionsXML = $dom->createElement('options'));
4040
foreach ($definition->getOptions() as $option) {
41-
if (!$option->isHidden()) {
42-
$this->appendDocument($optionsXML, $this->getInputOptionDocument($option));
43-
}
41+
$this->appendDocument($optionsXML, $this->getInputOptionDocument($option));
4442
}
4543

4644
return $dom;
@@ -212,7 +210,6 @@ private function getInputOptionDocument(InputOption $option): \DOMDocument
212210
$objectXML->setAttribute('accept_value', $option->acceptValue() ? 1 : 0);
213211
$objectXML->setAttribute('is_value_required', $option->isValueRequired() ? 1 : 0);
214212
$objectXML->setAttribute('is_multiple', $option->isArray() ? 1 : 0);
215-
$objectXML->setAttribute('is_negatable', $option->isNegatable() ? 1 : 0);
216213
$objectXML->appendChild($descriptionXML = $dom->createElement('description'));
217214
$descriptionXML->appendChild($dom->createTextNode($option->getDescription()));
218215

@@ -228,6 +225,17 @@ private function getInputOptionDocument(InputOption $option): \DOMDocument
228225
}
229226
}
230227

228+
if ($option->isNegatable()) {
229+
$dom->appendChild($objectXML = $dom->createElement('option'));
230+
$objectXML->setAttribute('name', '--no-'.$option->getName());
231+
$objectXML->setAttribute('shortcut', '');
232+
$objectXML->setAttribute('accept_value', 0);
233+
$objectXML->setAttribute('is_value_required', 0);
234+
$objectXML->setAttribute('is_multiple', 0);
235+
$objectXML->appendChild($descriptionXML = $dom->createElement('description'));
236+
$descriptionXML->appendChild($dom->createTextNode('Negate the "--'.$option->getName().'" option'));
237+
}
238+
231239
return $dom;
232240
}
233241
}

‎src/Symfony/Component/Console/Input/ArgvInput.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Console/Input/ArgvInput.php
+24-3Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,21 @@ private function addShortOption(string $shortcut, $value)
208208
*/
209209
private function addLongOption(string $name, $value)
210210
{
211-
$option = $this->getOptionDefinition($name);
211+
if (!$this->definition->hasOption($name)) {
212+
if (!$this->definition->hasNegation($name)) {
213+
throw new RuntimeException(sprintf('The "--%s" option does not exist.', $name));
214+
}
215+
216+
$optionName = $this->definition->negationToName($name);
217+
if (null !== $value) {
218+
throw new RuntimeException(sprintf('The "--%s" option does not accept a value.', $name));
219+
}
220+
$this->options[$optionName] = false;
221+
222+
return;
223+
}
224+
225+
$option = $this->definition->getOption($name);
212226

213227
if (null !== $value && !$option->acceptValue()) {
214228
throw new RuntimeException(sprintf('The "--%s" option does not accept a value.', $name));
@@ -225,8 +239,15 @@ private function addLongOption(string $name, $value)
225239
}
226240
}
227241

228-
$name = $option->effectiveName();
229-
$value = $option->checkValue($value);
242+
if (null === $value) {
243+
if ($option->isValueRequired()) {
244+
throw new RuntimeException(sprintf('The "--%s" option requires a value.', $name));
245+
}
246+
247+
if (!$option->isArray() && !$option->isValueOptional()) {
248+
$value = true;
249+
}
250+
}
230251

231252
if ($option->isArray()) {
232253
$this->options[$name][] = $value;

‎src/Symfony/Component/Console/Input/ArrayInput.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Console/Input/ArrayInput.php
+24-1Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,30 @@ private function addShortOption(string $shortcut, $value)
164164
*/
165165
private function addLongOption(string $name, $value)
166166
{
167-
$this->setOption($name, $value);
167+
if (!$this->definition->hasOption($name)) {
168+
if (!$this->definition->hasNegation($name)) {
169+
throw new InvalidOptionException(sprintf('The "--%s" option does not exist.', $name));
170+
}
171+
172+
$optionName = $this->definition->negationToName($name);
173+
$this->options[$optionName] = false;
174+
175+
return;
176+
}
177+
178+
$option = $this->definition->getOption($name);
179+
180+
if (null === $value) {
181+
if ($option->isValueRequired()) {
182+
throw new InvalidOptionException(sprintf('The "--%s" option requires a value.', $name));
183+
}
184+
185+
if (!$option->isValueOptional()) {
186+
$value = true;
187+
}
188+
}
189+
190+
$this->options[$name] = $value;
168191
}
169192

170193
/**

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Console/Input/Input.php
+10-20Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -146,17 +146,23 @@ public function getOptions()
146146
*/
147147
public function getOption(string $name)
148148
{
149-
$option = $this->getOptionDefinition($name);
150-
return \array_key_exists($name, $this->options) ? $this->options[$name] : $option->getDefault();
149+
if (!$this->definition->hasOption($name)) {
150+
throw new InvalidArgumentException(sprintf('The "%s" option does not exist.', $name));
151+
}
152+
153+
return \array_key_exists($name, $this->options) ? $this->options[$name] : $this->definition->getOption($name)->getDefault();
151154
}
152155

153156
/**
154157
* {@inheritdoc}
155158
*/
156159
public function setOption(string $name, $value)
157160
{
158-
$option = $this->getOptionDefinition($name);
159-
$this->options[$option->effectiveName()] = $option->checkValue($value);
161+
if (!$this->definition->hasOption($name)) {
162+
throw new InvalidArgumentException(sprintf('The "%s" option does not exist.', $name));
163+
}
164+
165+
$this->options[$name] = $value;
160166
}
161167

162168
/**
@@ -192,20 +198,4 @@ public function getStream()
192198
{
193199
return $this->stream;
194200
}
195-
196-
/**
197-
* Look up the option definition for the given option name.
198-
*
199-
* @param string $name
200-
*
201-
* @return InputOption
202-
*/
203-
protected function getOptionDefinition($name)
204-
{
205-
if (!$this->definition->hasOption($name)) {
206-
throw new RuntimeException(sprintf('The "--%s" option does not exist.', $name));
207-
}
208-
209-
return $this->definition->getOption($name);
210-
}
211201
}

0 commit comments

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