@@ -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
534D
+ * 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