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 497f03c

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

File tree

70 files changed

+475
-534
lines changed
Filter options

Some content is hidden

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

70 files changed

+475
-534
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/Disable ANSI output', null),
10371037
new InputOption('--no-interaction', '-n', InputOption::VALUE_NONE, 'Do not ask any interactive question'),
10381038
]);
10391039
}

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Console/Descriptor/JsonDescriptor.php
-3Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Console/Descriptor/MarkdownDescriptor.php
+1-5Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,7 @@ 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->isNegatable() ? '[no-]' : '').$option->getName();
7372
if ($option->getShortcut()) {
7473
$name .= '|-'.str_replace('|', '|-', $option->getShortcut()).'';
7574
}
@@ -107,9 +106,6 @@ protected function describeInputDefinition(InputDefinition $definition, array $o
107106

108107
$this->write('### Options');
109108
foreach ($definition->getOptions() as $option) {
110-
if ($option->isHidden()) {
111-
continue;
112-
}
113109
$this->write("\n\n");
114110
if (null !== $describeInputOption = $this->describeInputOption($option)) {
115111
$this->write($describeInputOption);

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Console/Descriptor/TextDescriptor.php
+3-9Lines changed: 3 additions & 9 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('--%s%s%s', $option->isNegatable() ? '[no-]' : '', $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;

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Console/Descriptor/XmlDescriptor.php
+1-3Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Console/Input/ArgvInput.php
+27-6Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ private function parseShortOption(string $token)
104104
/**
105105
* Parses a short option set.
106106
*
107-
* @throws RuntimeException When option given doesn't exist
107+
* @throws RuntimeException When given option doesn't exist
108108
*/
109109
private function parseShortOptionSet(string $name)
110110
{
@@ -190,7 +190,7 @@ private function parseArgument(string $token)
190190
/**
191191
* Adds a short option value.
192192
*
193-
* @throws RuntimeException When option given doesn't exist
193+
* @throws RuntimeException When given option doesn't exist
194194
*/
195195
private function addShortOption(string $shortcut, $value)
196196
{
@@ -204,11 +204,25 @@ private function addShortOption(string $shortcut, $value)
204204
/**
205205
* Adds a long option value.
206206
*
207-
* @throws RuntimeException When option given doesn't exist
207+
* @throws RuntimeException When given option doesn't exist
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
+26-3Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ protected function parse()
145145
/**
146146
* Adds a short option value.
147147
*
148-
* @throws InvalidOptionException When option given doesn't exist
148+
* @throws InvalidOptionException When given option doesn't exist
149149
*/
150150
private function addShortOption(string $shortcut, $value)
151151
{
@@ -159,12 +159,35 @@ private function addShortOption(string $shortcut, $value)
159159
/**
160160
* Adds a long option value.
161161
*
162-
* @throws InvalidOptionException When option given doesn't exist
162+
* @throws InvalidOptionException When given option doesn't exist
163163
* @throws InvalidOptionException When a required value is missing
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
}

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Console/Input/InputDefinition.php
+39-21Lines changed: 39 additions & 21 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
/**
@@ -50,6 +51,7 @@ public function setDefinition(array $definition)
5051
{
5152
$arguments = [];
5253
$options = [];
54+
$negations = [];
5355
foreach ($definition as $item) {
5456
if ($item instanceof InputOption) {
5557
$options[] = $item;
@@ -224,22 +226,9 @@ public function addOptions(array $options = [])
224226
}
225227

226228
/**
227-
* @throws LogicException When option given already exist
229+
* @throws LogicException When given option 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,14 +248,22 @@ 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
/**
265262
* Returns an InputOption by name.
266263
*
267264
* @return InputOption A InputOption object
268265
*
269-
* @throws InvalidArgumentException When option given doesn't exist
266+
* @throws InvalidArgumentException When given option doesn't exist
270267
*/
271268
public function getOption(string $name)
272269
{
@@ -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;
@@ -338,7 +343,7 @@ public function getOptionDefaults()
338343
/**
339344
* Returns the InputOption name given a shortcut.
340345
*
341-
* @throws InvalidArgumentException When option given does not exist
346+
* @throws InvalidArgumentException When given option does not exist
342347
*
343348
* @internal
344349
*/
@@ -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 given option 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,7 @@ 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+
$elements[] = sprintf('[%s--%s%s%s]', $shortcut, $option->isNegatable() ? '[no-]' : '', $option->getName(), $value);
382400
}
383401
}
384402

0 commit comments

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