Skip to content

Commit 04076f4

Browse files
committed
\cli\menu now uses indexes for default and return instead of values.
1 parent 4f4b7fc commit 04076f4

1 file changed

Lines changed: 5 additions & 6 deletions

File tree

lib/cli/cli.php

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -140,16 +140,15 @@ function choose($question, $choices= 'yn', $default = 'n') {
140140
* or objects with a `__toString()` method.
141141
*
142142
* @param array $items The list of items the user can choose from.
143-
* @param string $default The *value* of the default item. `array_search` is
144-
* used to find the proper index.
143+
* @param string $default The index of the default item.
145144
* @param string $title The message displayed to the user when prompted.
146-
* @return string The *value* of the chosen item.
145+
* @return string The index of the chosen item.
147146
*/
148147
function menu($items, $default = false, $title = 'Choose an item') {
149148
$map = array_values($items);
150149

151-
if ($default && strpos($title, '[') === false && in_array($default, $items)) {
152-
$title .= ' ['.$default.']';
150+
if ($default && strpos($title, '[') === false && isset($items[$default])) {
151+
$title .= ' ['.$items[$default].']';
153152
}
154153

155154
foreach ($map as $idx => $item) {
@@ -164,7 +163,7 @@ function menu($items, $default = false, $title = 'Choose an item') {
164163
if (is_numeric($line)) {
165164
$line--;
166165
if (isset($map[$line])) {
167-
return $map[$line];
166+
return array_search($map[$line], $items);
168167
}
169168

170169
if ($line < 0 || $line >= count($map)) {

0 commit comments

Comments
 (0)