diff --git a/src/__phutil_library_map__.php b/src/__phutil_library_map__.php --- a/src/__phutil_library_map__.php +++ b/src/__phutil_library_map__.php @@ -491,6 +491,7 @@ 'phutil_console_get_terminal_width' => 'console/format.php', 'phutil_console_prompt' => 'console/format.php', 'phutil_console_require_tty' => 'console/format.php', + 'phutil_console_select' => 'console/format.php', 'phutil_console_wrap' => 'console/format.php', 'phutil_count' => 'internationalization/pht.php', 'phutil_date_format' => 'utils/viewutils.php', diff --git a/src/console/format.php b/src/console/format.php --- a/src/console/format.php +++ b/src/console/format.php @@ -24,6 +24,20 @@ } } +function phutil_console_select($prompt, $min, $max) { + $select_options = '['.$min.' - '.$max.']'; + do { + $response = phutil_console_prompt($prompt.' '.$select_options); + $selection = trim($response); + + if (preg_match('/^\d+\z/', $selection)) { + $selection = (int)$selection; + if ($selection >= $min && $selection <= $max) { + return $selection; + } + } + } while (true); +} function phutil_console_prompt($prompt, $history = '') { echo "\n\n";