Skip to content

Commit b551c10

Browse files
committed
Merge pull request #36 from igorsantos07/confirm
Including `confirm()` method
2 parents e5d20d4 + 8df4c34 commit b551c10

File tree

2 files changed

+31
-13
lines changed

2 files changed

+31
-13
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
.idea
1+
.idea
2+
vendor

lib/cli/cli.php

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -111,10 +111,11 @@ function input( $format = null ) {
111111
* Displays an input prompt. If no default value is provided the prompt will
112112
* continue displaying until input is received.
113113
*
114-
* @param string $question The question to ask the user.
115-
* @param string $default A default value if the user provides no input.
116-
* @param string $marker A string to append to the question and default value
117-
* on display.
114+
* @param string $question The question to ask the user.
115+
* @param bool|string $default A default value if the user provides no input.
116+
* @param string $marker A string to append to the question and default value
117+
* on display.
118+
* @param boolean $hide Optionally hides what the user types in.
118119
* @return string The users input.
119120
* @see cli\input()
120121
*/
@@ -126,25 +127,41 @@ function prompt( $question, $default = false, $marker = ': ' ) {
126127
* Presents a user with a multiple choice question, useful for 'yes/no' type
127128
* questions (which this function defaults too).
128129
*
129-
* @param string $question The question to ask the user.
130-
* @param string $valid A string of characters allowed as a response. Case
131-
* is ignored.
132-
* @param string $default The default choice. NULL if a default is not allowed.
130+
* @param string $question The question to ask the user.
131+
* @param string $choice
132+
* @param string|null $default The default choice. NULL if a default is not allowed.
133+
* @internal param string $valid A string of characters allowed as a response. Case
134+
* is ignored.
133135
* @return string The users choice.
134-
* @see cli\prompt()
136+
* @see cli\prompt()
135137
*/
136138
function choose( $question, $choice = 'yn', $default = 'n' ) {
137139
return \cli\Streams::choose( $question, $choice, $default );
138140
}
139141

142+
/**
143+
* Does the same as {@see choose()}, but always asks yes/no and returns a boolean
144+
*
145+
* @param string $question The question to ask the user.
146+
* @param bool|null $default The default choice, in a boolean format.
147+
* @return bool
148+
*/
149+
function confirm( $question, $default = false ) {
150+
if ( is_bool( $default ) ) {
151+
$default = $default? 'y' : 'n';
152+
}
153+
$result = choose( $question, 'yn', $default );
154+
return $result == 'y';
155+
}
156+
140157
/**
141158
* Displays an array of strings as a menu where a user can enter a number to
142159
* choose an option. The array must be a single dimension with either strings
143160
* or objects with a `__toString()` method.
144161
*
145-
* @param array $items The list of items the user can choose from.
146-
* @param string $default The index of the default item.
147-
* @param string $title The message displayed to the user when prompted.
162+
* @param array $items The list of items the user can choose from.
163+
* @param bool|string $default The index of the default item.
164+
* @param string $title The message displayed to the user when prompted.
148165
* @return string The index of the chosen item.
149166
* @see cli\line()
150167
* @see cli\input()

0 commit comments

Comments
 (0)