Skip to content

Commit ad5f315

Browse files
committed
Implement negatable option
1 parent 8d455db commit ad5f315

File tree

70 files changed

+494
-548
lines changed

Some content is hidden

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

70 files changed

+494
-548
lines changed

src/Symfony/Component/Console/Application.php

Lines 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

Lines 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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,9 +134,6 @@ private function getInputDefinitionData(InputDefinition $definition): array
134134

135135
$inputOptions = [];
136136
foreach ($definition->getOptions() as $name => $option) {
137-
if ($option->isHidden()) {
138-
continue;
139-
}
140137
$inputOptions[$name] = $this->getInputOptionData($option);
141138
}
142139

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

Lines 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

Lines 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

Lines changed: 1 addition & 3 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;

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

Lines 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

Lines 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

Lines 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
}

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

Lines changed: 37 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ class InputDefinition
3333
private $hasAnArrayArgument = false;
3434
private $hasOptional;
3535
private $options;
36+
private $negations;
3637
private $shortcuts;
3738

3839
/**
@@ -208,6 +209,7 @@ public function setOptions(array $options = [])
208209
{
209210
$this->options = [];
210211
$this->shortcuts = [];
212+
$this->negations = [];
211213
$this->addOptions($options);
212214
}
213215

@@ -227,19 +229,6 @@ public function addOptions(array $options = [])
227229
* @throws LogicException When option given already exist
228230
*/
229231
public function addOption(InputOption $option)
230-
{
231-
$this->doAddOption($option);
232-
233-
if ($option->isNegatable()) {
234-
$negatedOption = new NegatedInputOption($option);
235-
$this->doAddOption($negatedOption);
236-
}
237-
}
238-
239-
/**
240-
* @throws LogicException When option given already exist
241-
*/
242-
private function doAddOption(InputOption $option)
243232
{
244233
if (isset($this->options[$option->getName()]) && !$option->equals($this->options[$option->getName()])) {
245234
throw new LogicException(sprintf('An option named "%s" already exists.', $option->getName()));
@@ -259,6 +248,14 @@ private function doAddOption(InputOption $option)
259248
$this->shortcuts[$shortcut] = $option->getName();
260249
}
261250
}
251+
252+
if ($option->isNegatable()) {
253+
$negatedName = 'no-'.$option->getName();
254+
if (isset($this->options[$negatedName])) {
255+
throw new LogicException(sprintf('An option named "%s" already exists.', $negatedName));
256+
}
257+
$this->negations[$negatedName] = $option->getName();
258+
}
262259
}
263260

264261
/**
@@ -310,6 +307,14 @@ public function hasShortcut(string $name)
310307
return isset($this->shortcuts[$name]);
311308
}
312309

310+
/**
311+
* Returns true if an InputOption object exists by negated name.
312+
*/
313+
public function hasNegation(string $name): bool
314+
{
315+
return isset($this->negations[$name]);
316+
}
317+
313318
/**
314319
* Gets an InputOption by shortcut.
315320
*
@@ -329,7 +334,7 @@ public function getOptionDefaults()
329334
{
330335
$values = [];
331336
foreach ($this->options as $option) {
332-
$values[$option->effectiveName()] = $option->getDefault();
337+
$values[$option->getName()] = $option->getDefault();
333338
}
334339

335340
return $values;
@@ -351,6 +356,22 @@ public function shortcutToName(string $shortcut): string
351356
return $this->shortcuts[$shortcut];
352357
}
353358

359+
/**
360+
* Returns the InputOption name given a negation.
361+
*
362+
* @throws InvalidArgumentException When option given does not exist
363+
*
364+
* @internal
365+
*/
366+
public function negationToName(string $negation): string
367+
{
368+
if (!isset($this->negations[$negation])) {
369+
throw new InvalidArgumentException(sprintf('The "--%s" option does not exist.', $negation));
370+
}
371+
372+
return $this->negations[$negation];
373+
}
374+
354375
/**
355376
* Gets the synopsis.
356377
*
@@ -364,9 +385,6 @@ public function getSynopsis(bool $short = false)
364385
$elements[] = '[options]';
365386
} elseif (!$short) {
366387
foreach ($this->getOptions() as $option) {
367-
if ($option->isHidden()) {
368-
continue;
369-
}
370388
$value = '';
371389
if ($option->acceptValue()) {
372390
$value = sprintf(
@@ -378,7 +396,8 @@ public function getSynopsis(bool $short = false)
378396
}
379397

380398
$shortcut = $option->getShortcut() ? sprintf('-%s|', $option->getShortcut()) : '';
381-
$elements[] = sprintf('[%s--%s%s]', $shortcut, $option->getName(), $value);
399+
$negation = $option->isNegatable() ? sprintf('|--no-%s', $option->getName()) : '';
400+
$elements[] = sprintf('[%s--%s%s%s]', $shortcut, $option->getName(), $value, $negation);
382401
}
383402
}
384403

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy