IInput
in
Interface that lets you retrieve the input from a command.
Tags
Attributes
- #[Consumable]
- $since: '35.0.0'
Table of Contents
Methods
- ask() : mixed
- Asks the user to provide some value.
- askHidden() : mixed
- Ask the user to provide some value but the user's input will be hidden, and it cannot define a default value.
- choice() : mixed
- Ask a question whose answer is constrained to the given list of valid answers:
- confirm() : bool
- Ask a Yes/No question to the user, and it only returns true or false:
- getArgument() : string|bool|int|float|array<string|int, mixed>|null
- Returns the argument value for a given argument name.
- getArguments() : array<string|int, string|bool|int|float|array<string|int, mixed>|null>
- Returns all the given arguments merged with the default values.
- getOption() : string|bool|int|float|array<string|int, mixed>|null
- Returns the option value for a given option name.
- getOptions() : array<string|int, string|bool|int|float|array<string|int, mixed>|null>
- Returns all the given options merged with the default values.
- hasArgument() : bool
- Returns true if an argument exists by name or position.
- hasOption() : bool
- Returns true if an option exists by name.
Methods
ask()
Asks the user to provide some value.
public
ask(string $question[, string|null $default = null ][, callable(string): mixed|null $validator = null ]) : mixed
$input->ask('What is your name?');
You can pass the default value as the second argument so the user can hit the <Enter> key to select that value:
$input->ask('Where are you from?', 'United States');
In case you need to validate the given value, pass a callback validator as the third argument:
$input->ask('Number of workers to start', '1', function (string $number): int {
if (!is_numeric($number)) {
throw new \RuntimeException('You must type a number.');
}
return (int) $number;
});
Parameters
- $question : string
- $default : string|null = null
- $validator : callable(string): mixed|null = null
Tags
askHidden()
Ask the user to provide some value but the user's input will be hidden, and it cannot define a default value.
public
askHidden(string $question[, callable(string): mixed|null $validator = null ]) : mixed
Use it when asking for sensitive information:
$input->askHidden('What is your password?');
In case you need to validate the given value, pass a callback validator as the second argument:
$input->askHidden('What is your password?', function (string $password): string {
if (empty($password)) {
throw new \RuntimeException('Password cannot be empty.');
}
return $password;
});
Parameters
- $question : string
- $validator : callable(string): mixed|null = null
Tags
choice()
Ask a question whose answer is constrained to the given list of valid answers:
public
choice(string $question, array<string|int, string> $choices[, mixed $default = null ][, bool $multiSelect = false ]) : mixed
$input->choice('Select the queue to analyze', ['queue1', 'queue2', 'queue3']);
You can pass the default value as the third argument so the user can hit the <Enter> key to select that value:
$input->choice('Select the queue to analyze', ['queue1', 'queue2', 'queue3'], 'queue1');
Choice questions display both the choice value and a numeric index, which starts from 0 by default. To use custom indices, pass an array with custom numeric keys as the choice values:
$input->choice('Select the queue to analyze', [5 => 'queue1', 6 => 'queue2', 7 => 'queue3']);
Finally, you can allow users to select multiple choices. To do so, users must separate each choice with a comma (e.g. typing 1, 2 will select choice 1 and 2):
$input->choice('Select the queue to analyze', ['queue1', 'queue2', 'queue3'], multiSelect: true);
Parameters
- $question : string
- $choices : array<string|int, string>
- $default : mixed = null
- $multiSelect : bool = false
Tags
confirm()
Ask a Yes/No question to the user, and it only returns true or false:
public
confirm(string $question[, bool $default = true ]) : bool
$input->confirm('Restart the web server?');
You can pass the default value as the second argument so the user can hit the <Enter> key to select that value:
$input->confirm('Restart the web server?', true);
Parameters
- $question : string
- $default : bool = true
Tags
Return values
boolgetArgument()
Returns the argument value for a given argument name.
public
getArgument(string $name) : string|bool|int|float|array<string|int, mixed>|null
Parameters
- $name : string
Tags
Return values
string|bool|int|float|array<string|int, mixed>|nullgetArguments()
Returns all the given arguments merged with the default values.
public
getArguments() : array<string|int, string|bool|int|float|array<string|int, mixed>|null>
Tags
Return values
array<string|int, string|bool|int|float|array<string|int, mixed>|null>getOption()
Returns the option value for a given option name.
public
getOption(string $name) : string|bool|int|float|array<string|int, mixed>|null
Parameters
- $name : string
Tags
Return values
string|bool|int|float|array<string|int, mixed>|nullgetOptions()
Returns all the given options merged with the default values.
public
getOptions() : array<string|int, string|bool|int|float|array<string|int, mixed>|null>
Tags
Return values
array<string|int, string|bool|int|float|array<string|int, mixed>|null>hasArgument()
Returns true if an argument exists by name or position.
public
hasArgument(string $name) : bool
Parameters
- $name : string
Tags
Return values
boolhasOption()
Returns true if an option exists by name.
public
hasOption(string $name) : bool
Parameters
- $name : string