Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

237 lines (160 sloc) 5.324 kb

Console prompts

In addition to :doc:`console abstraction layer <zend.console.adapter>` Zend Framework 2 provides numerous convenience classes for interacting with the user in console environment. This chapter describes available Zend\Console\Prompt classes and their example usage.

All prompts can be instantiated as objects and provide show() method.

There is also a shorter method of displaying prompts, using static prompt() method:

Both of above examples will display something like this:

../images/zend.console.prompt.png

Confirm

This prompt is best used for a yes / no type of choices.


Confirm( string $text, string $yesChar = 'y', string $noChar = 'n' )
$text
(string) The text to show with the prompt
$yesChar
(string) The char that corresponds with YES choice. Defaults to y.
$noChar
(string) The char that corresponds with NO choice. Defaults to n.

Example usage:


use Zend\Console\Prompt\Confirm;

if ( Confirm::prompt('Is this the correct answer? [y/n]', 'y', 'n') ) {
    $console->write("You chose YES");
} else {
    $console->write("You chose NO");
}
../images/zend.console.prompt2.png

Line

This prompt asks for a line of text input.


Line(
    string $text = 'Please enter value',
    bool $allowEmpty = false,
    bool $maxLength = 2048
)
$text
(string) The text to show with the prompt
$allowEmpty
(boolean) Can this prompt be skipped, by pressing [ENTER] ? (default fo false)
$maxLength
(integer) Maximum length of the input. Anything above this limit will be truncated.

Example usage:


use Zend\Console\Prompt\Line;

$name = Line::prompt(
    'What is your name?',
    false,
    100
);

$console->write("Good day to you $name!");
../images/zend.console.prompt3.png

Char

This prompt reads a single keystroke and optionally validates it against a list o allowed characters.


Char(
    string $text = 'Please hit a key',
    string $allowedChars = 'abc',
    bool   $ignoreCase = true,
    bool   $allowEmpty = false,
    bool   $echo = true
)
$text
(string) The text to show with the prompt
$allowedChars
(string) A list of allowed keys that can be pressed.
$ignoreCase
(boolean) Ignore the case of chars pressed (default to true)
$allowEmpty
(boolean) Can this prompt be skipped, by pressing [ENTER] ? (default fo false)
$echo
(boolean) Should the selection be displayed on the screen ?

Example usage:


use Zend\Console\Prompt\Char;

$answer = Char::prompt(
    'What is the correct answer? [a,b,c,d,e]',
    'abcde',
    true,
    false,
    true
);

if ($answer == 'b') {
    $console->write('Correct. This it the right answer');
} else {
    $console->write('Wrong ! Try again.');
}
../images/zend.console.prompt4.png

Select

This prompt displays a number of choices and asks the user to pick one.


Select(
    string $text = 'Please select one option',
    array  $options = array(),
    bool   $allowEmpty = false,
    bool   $echo = false
)
$text
(string) The text to show with the prompt
$options
(array) An associative array with keys strokes (chars) and their displayed values.
$allowEmpty
(boolean) Can this prompt be skipped, by pressing [ENTER] ? (default fo false)
$echo
(boolean) Should the selection be displayed on the screen ?

Example usage:


$options = array(
    'a' => 'Apples',
    'o' => 'Oranges',
    'p' => 'Pears',
    'b' => 'Bananas',
    'n' => 'none of the above...'
);

$answer = Select::prompt(
    'Which fruit do you like the best?',
    $options,
    false,
    false
);

$console->write("You told me that you like " . $options[$answer]);
../images/zend.console.prompt5.png
Jump to Line
Something went wrong with that request. Please try again.