8000 Implement negatable option · symfony/symfony@ca2d46a · GitHub
[go: up one dir, main page]

Skip to content

Commit ca2d46a

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

File tree

70 files changed

+511
-565
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

+511
-565
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_BOOLEAN, 'Force [or 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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ private function getInputOptionData(InputOption $option): array
119119
'accept_value' => $option->acceptValue(),
120120
'is_value_required' => $option->isValueRequired(),
121121
'is_multiple' => $option->isArray(),
122-
'is_negatable' => $option->isNegatable(),
122+
'is_boolean' => $option->isBoolean(),
123123
'description' => preg_replace('/\s*[\r\n]\s*/', ' ', $option->getDescription()),
124124
'default' => \INF === $option->getDefault() ? 'INF' : $option->getDefault(),
125125
];
@@ -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: 5 additions & 6 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->isBoolean()) {
73+
$name .= '|--no-'.$option->getName();
74+
}
7375
if ($option->getShortcut()) {
7476
$name .= '|-'.str_replace('|', '|-', $option->getShortcut()).'';
7577
}
@@ -80,7 +82,7 @@ protected function describeInputOption(InputOption $option, array $options = [])
8082
.'* Accept value: '.($option->acceptValue() ? 'yes' : 'no')."\n"
8183
.'* Is value required: '.($option->isValueRequired() ? 'yes' : 'no')."\n"
8284
.'* Is multiple: '.($option->isArray() ? 'yes' : 'no')."\n"
83-
.'* Is negatable: '.($option->isNegatable() ? 'yes' : 'no')."\n"
85+
.'* Is boolean: '.($option->isBoolean() ? 'yes' : 'no')."\n"
8486
.'* Default: `'.str_replace("\n", '', var_export($option->getDefault(), true)).'`'
8587
);
8688
}
@@ -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->isBoolean() ? '--%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->isBoolean()) {
329+
$nameLength += 4 + 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

10000

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

Lines changed: 2 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,7 @@ 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);
213+
$objectXML->setAttribute('is_boolean', $option->isBoolean() ? 1 : 0);
216214
$objectXML->appendChild($descriptionXML = $dom->createElement('description'));
217215
$descriptionXML->appendChild($dom->createTextNode($option->getDescription()));
218216

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

Lines 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

Lines 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

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
}

0 commit comments

Comments
 (0)
0