-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Console] Support binary / negatable options #26292
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -146,23 +146,17 @@ public function getOptions() | |
*/ | ||
public function getOption($name) | ||
{ | ||
if (!$this->definition->hasOption($name)) { | ||
throw new InvalidArgumentException(sprintf('The "%s" option does not exist.', $name)); | ||
} | ||
|
||
return array_key_exists($name, $this->options) ? $this->options[$name] : $this->definition->getOption($name)->getDefault(); | ||
$option = $this->getOptionDefinition($name); | ||
return array_key_exists($name, $this->options) ? $this->options[$name] : $option->getDefault(); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function setOption($name, $value) | ||
{ | ||
if (!$this->definition->hasOption($name)) { | ||
throw new InvalidArgumentException(sprintf('The "%s" option does not exist.', $name)); | ||
} | ||
|
||
$this->options[$name] = $value; | ||
$option = $this->getOptionDefinition($name); | ||
$this->options[$option->effectiveName()] = $option->checkValue($value); | ||
} | ||
|
||
/** | ||
|
@@ -200,4 +194,20 @@ public function getStream() | |
{ | ||
return $this->stream; | ||
} | ||
|
||
/** | ||
* Look up the option definition for the given option name. | ||
* | ||
* @param string $name | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As this would target 4.4, you can use type hints instead of phpdocs. |
||
* | ||
* @return InputOption | ||
*/ | ||
protected function getOptionDefinition($name) | ||
{ | ||
if (!$this->definition->hasOption($name)) { | ||
throw new RuntimeException(sprintf('The "--%s" option does not exist.', $name)); | ||
} | ||
|
||
return $this->definition->getOption($name); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,7 @@ | |
namespace Symfony\Component\Console\Input; | ||
|
||
use Symfony\Component\Console\Exception\InvalidArgumentException; | ||
use Symfony\Component\Console\Exception\InvalidOptionException; | ||
use Symfony\Component\Console\Exception\LogicException; | ||
|
||
/** | ||
|
@@ -25,6 +26,9 @@ class InputOption | |
const VALUE_REQUIRED = 2; | ||
const VALUE_OPTIONAL = 4; | ||
const VALUE_IS_ARRAY = 8; | ||
const VALUE_NEGATABLE = 16; | ||
const VALUE_HIDDEN = 32; | ||
const VALUE_BINARY = (self::VALUE_NONE | self::VALUE_NEGATABLE); | ||
|
||
private $name; | ||
private $shortcut; | ||
|
@@ -70,7 +74,7 @@ public function __construct(string $name, $shortcut = null, int $mode = null, st | |
|
||
if (null === $mode) { | ||
$mode = self::VALUE_NONE; | ||
} elseif ($mode > 15 || $mode < 1) { | ||
} elseif ($mode >= (self::VALUE_HIDDEN << 1) || $mode < 1) { | ||
throw new InvalidArgumentException(sprintf('Option mode "%s" is not valid.', $mode)); | ||
} | ||
|
||
|
@@ -106,6 +110,11 @@ public function getName() | |
return $this->name; | ||
} | ||
|
||
public function effectiveName() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should probably be |
||
{ | ||
return $this->getName(); | ||
} | ||
|
||
/** | ||
* Returns true if the option accepts a value. | ||
* | ||
|
@@ -146,6 +155,39 @@ public function isArray() | |
return self::VALUE_IS_ARRAY === (self::VALUE_IS_ARRAY & $this->mode); | ||
} | ||
|
||
/** | ||
* Returns true if the option is negatable (option --foo can be forced | ||
* to 'false' via the --no-foo option). | ||
* | ||
* @return bool true if mode is self::VALUE_NEGATABLE, false otherwise | ||
*/ | ||
public function isNegatable() | ||
{ | ||
return self::VALUE_NEGATABLE === (self::VALUE_NEGATABLE & $this->mode); | ||
} | ||
|
||
/** | ||
* Returns true if the option should not be shown in help (e.g. a negated | ||
* option). | ||
* | ||
* @return bool true if mode is self::VALUE_HIDDEN, false otherwise | ||
*/ | ||
public function isHidden() | ||
{ | ||
return self::VALUE_HIDDEN === (self::VALUE_HIDDEN & $this->mode); | ||
} | ||
|
||
/** | ||
* Returns true if the option is binary (can be --foo or --no-foo, and | ||
* nothing else). | ||
* | ||
* @return bool true if negatable and does not have a value. | ||
*/ | ||
public function isBinary() | ||
{ | ||
return $this->isNegatable() && !$this->acceptValue(); | ||
} | ||
|
||
/** | ||
* Sets the default value. | ||
* | ||
|
@@ -155,7 +197,7 @@ public function isArray() | |
*/ | ||
public function setDefault($default = null) | ||
{ | ||
if (self::VALUE_NONE === (self::VALUE_NONE & $this->mode) && null !== $default) { | ||
if (self::VALUE_NONE === ((self::VALUE_NONE | self::VALUE_NEGATABLE) & $this->mode) && null !== $default) { | ||
throw new LogicException('Cannot set a default value when using InputOption::VALUE_NONE mode.'); | ||
} | ||
|
||
|
@@ -167,7 +209,7 @@ public function setDefault($default = null) | |
} | ||
} | ||
|
||
$this->default = $this->acceptValue() ? $default : false; | ||
$this->default = ($this->acceptValue() || $this->isNegatable()) ? $default : false; | ||
} | ||
|
||
/** | ||
|
@@ -190,6 +232,27 @@ public function getDescription() | |
return $this->description; | ||
} | ||
|
||
/** | ||
* Checks the validity of a value, and alters it as necessary | ||
* | ||
* @param mixed $value | ||
* | ||
* @return @mixed | ||
*/ | ||
public function checkValue($value) | ||
{ | ||
if (null === $value) { | ||
if ($this->isValueRequired()) { | ||
throw new InvalidOptionException(sprintf('The "--%s" option requires a value.', $this->getName())); | ||
} | ||
|
||
if (!$this->isValueOptional()) { | ||
return true; | ||
} | ||
} | ||
return $value; | ||
} | ||
|
||
/** | ||
* Checks whether the given option equals this one. | ||
* | ||
|
@@ -200,6 +263,8 @@ public function equals(InputOption $option) | |
return $option->getName() === $this->getName() | ||
&& $option->getShortcut() === $this->getShortcut() | ||
&& $option->getDefault() === $this->getDefault() | ||
&& $option->isHidden() === $this->isHidden() | ||
&& $option->isNegatable() === $this->isNegatable() | ||
&& $option->isArray() === $this->isArray() | ||
&& $option->isValueRequired() === $this->isValueRequired() | ||
&& $option->isValueOptional() === $this->isValueOptional() | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks