ArgvInput
class ArgvInput extends Input
ArgvInput represents an input coming from the CLI arguments.
Usage:
$input = new ArgvInput();
By default, the $_SERVER['argv']
array is used for the input values.
This can be overridden by explicitly passing the input values in the constructor:
$input = new ArgvInput($_SERVER['argv']);
If you pass it yourself, don't forget that the first element of the array is the name of the running application.
When passing an argument to the constructor, be sure that it respects the same rules as the argv one. It's almost always better to use the StringInput
when you want to provide your own input.
Methods
__construct(array $argv = null, InputDefinition $definition = null) | ||
bind(InputDefinition $definition) Binds the current Input instance with the given arguments and options. | from Input | |
validate() Validates the input. | from Input | |
bool | isInteractive() Is this input means interactive? | from Input |
setInteractive(bool $interactive) Sets the input interactivity. | from Input | |
array | getArguments() Returns all the given arguments merged with the default values. | from Input |
mixed | getArgument(string $name) Returns the argument value for a given argument name. | from Input |
setArgument(string $name, string $value) Sets an argument value by name. | from Input | |
bool | hasArgument(string|int $name) Returns true if an InputArgument object exists by name or position. | from Input |
array | getOptions() Returns all the given options merged with the default values. | from Input |
mixed | getOption(string $name) Returns the option value for a given option name. | from Input |
setOption(string $name, string|bool $value) Sets an option value by name. | from Input | |
bool | hasOption(string $name) Returns true if an InputOption object exists by name. | from Input |
string | escapeToken(string $token) Escapes a token through escapeshellarg if it contains unsafe chars. | from Input |
setStream(resource $stream) Sets the input stream to read from when interacting with the user. | from Input | |
resource|null | getStream() Returns the input stream. | from Input |
string | getFirstArgument() Returns the first argument from the raw parameters (not parsed). | |
bool | hasParameterOption(string|array $values, bool $onlyParams = false) Returns true if the raw parameters (not parsed) contain a value. | |
mixed | getParameterOption(string|array $values, mixed $default = false, bool $onlyParams = false) Returns the value of a raw option (not parsed). | |
string | __toString() Returns a stringified representation of the args passed to the command. |
Details
__construct(array $argv = null, InputDefinition $definition = null)
Parameters
array | $argv | An array of parameters from the CLI (in the argv format) |
InputDefinition | $definition |
bind(InputDefinition $definition)
Binds the current Input instance with the given arguments and options.
Parameters
InputDefinition | $definition |
validate()
Validates the input.
Exceptions
RuntimeException | When not enough arguments are given |
bool isInteractive()
Is this input means interactive?
Return Value
bool |
setInteractive(bool $interactive)
Sets the input interactivity.
Parameters
bool | $interactive | If the input should be interactive |
array getArguments()
Returns all the given arguments merged with the default values.
Return Value
array |
mixed getArgument(string $name)
Returns the argument value for a given argument name.
Parameters
string | $name | The argument name |
Return Value
mixed | The argument value |
Exceptions
InvalidArgumentException | When argument given doesn't exist |
setArgument(string $name, string $value)
Sets an argument value by name.
Parameters
string | $name | The argument name |
string | $value | The argument value |
Exceptions
InvalidArgumentException | When argument given doesn't exist |
bool hasArgument(string|int $name)
Returns true if an InputArgument object exists by name or position.
Parameters
string|int | $name | The InputArgument name or position |
Return Value
bool | true if the InputArgument object exists, false otherwise |
array getOptions()
Returns all the given options merged with the default values.
Return Value
array |
mixed getOption(string $name)
Returns the option value for a given option name.
Parameters
string | $name | The option name |
Return Value
mixed | The option value |
Exceptions
InvalidArgumentException | When option given doesn't exist |
setOption(string $name, string|bool $value)
Sets an option value by name.
Parameters
string | $name | The option name |
string|bool | $value | The option value |
Exceptions
InvalidArgumentException | When option given doesn't exist |
bool hasOption(string $name)
Returns true if an InputOption object exists by name.
Parameters
string | $name | The InputOption name |
Return Value
bool | true if the InputOption object exists, false otherwise |
string escapeToken(string $token)
Escapes a token through escapeshellarg if it contains unsafe chars.
Parameters
string | $token |
Return Value
string |
setStream(resource $stream)
Sets the input stream to read from when interacting with the user.
This is mainly useful for testing purpose.
Parameters
resource | $stream | The input stream |
resource|null getStream()
Returns the input stream.
Return Value
resource|null |
string getFirstArgument()
Returns the first argument from the raw parameters (not parsed).
Return Value
string | The value of the first argument or null otherwise |
bool hasParameterOption(string|array $values, bool $onlyParams = false)
Returns true if the raw parameters (not parsed) contain a value.
This method is to be used to introspect the input parameters before they have been validated. It must be used carefully.
Parameters
string|array | $values | The values to look for in the raw parameters (can be an array) |
bool | $onlyParams | Only check real parameters, skip those following an end of options (--) signal |
Return Value
bool | true if the value is contained in the raw parameters |
mixed getParameterOption(string|array $values, mixed $default = false, bool $onlyParams = false)
Returns the value of a raw option (not parsed).
This method is to be used to introspect the input parameters before they have been validated. It must be used carefully.
Parameters
string|array | $values | The value(s) to look for in the raw parameters (can be an array) |
mixed | $default | The default value to return if no result is found |
bool | $onlyParams | Only check real parameters, skip those following an end of options (--) signal |
Return Value
mixed | The option value |
string __toString()
Returns a stringified representation of the args passed to the command.
Return Value
string |
© 2004–2017 Fabien Potencier
Licensed under the MIT License.
http://api.symfony.com/4.0/Symfony/Component/Console/Input/ArgvInput.html