예제 #1
0
파일: Input.php 프로젝트: newtoid/cli
 /**
  * Produces a menu with the given attributes
  *
  * @param [array]   $choices      Menu options for the user
  * @param [mixed]   $default      Given as null option in the menu
  * @param [string]  $text         Prompt printed to STDOUT
  * @param [boolean] $return_value If true, returns selection. False, the index
  * @return [string] Either the selection, its index, or the default
  */
 public static function menu($choices, $default = null, $text = "Select one", $return_value = false)
 {
     echo PHP_EOL;
     $index = \cli\Streams::menu($choices, $default, $text);
     if ($return_value) {
         return $choices[$index];
     }
     return $index;
 }
예제 #2
0
파일: Input.php 프로젝트: andrefy/cli
 /**
  * Produces a menu with the given attributes
  *
  * @param [array]   $choices      Menu options for the user
  * @param [mixed]   $default      Given as null option in the menu
  * @param [string]  $text         Prompt printed to STDOUT
  * @param [boolean] $return_value If true, returns selection. False, the index
  * @return [string] Either the selection, its index, or the default
  */
 public static function menu($choices, $default = null, $text = "Select one", $return_value = false)
 {
     if (count($choices) == 1) {
         $only_choice = array_shift($choices);
         return $only_choice;
     }
     $index = \cli\Streams::menu($choices, $default, $text);
     if ($return_value) {
         return $choices[$index];
     }
     return $index;
 }
예제 #3
0
 /**
  * Produces a menu with the given attributes
  *
  * @param array $arg_options Elements as follow:
  *        bool   autoselect_solo Automatically selects the only given option
  *        array  choices         Menu options for the user
  *        mixed  default         Given as null option in the menu
  *        string message         Prompt printed to STDOUT
  *        bool   return_value    If true, returns selection. False, the index
  * @return string Either the selection, its index, or the default
  */
 public function menu(array $arg_options = [])
 {
     $default_options = ['autoselect_solo' => true, 'args' => [], 'choices' => [$this->NULL_INPUTS[0]], 'default' => null, 'key' => null, 'message' => 'Select one', 'return_value' => false];
     $options = array_merge($default_options, $arg_options);
     if (!is_null($options['key']) && isset($options['args'][$options['key']])) {
         return $options['args'][$options['key']];
     }
     if (count($options['choices']) == 1 && $options['autoselect_solo']) {
         $indices = array_keys($options['choices']);
         $index = array_shift($indices);
     } else {
         $index = \cli\Streams::menu($options['choices'], $options['default'], $options['message']);
     }
     if ($options['return_value']) {
         return $options['choices'][$index];
     }
     return $index;
 }
예제 #4
0
 /**
  * Produces a menu with the given attributes
  *
  * @param array $arg_options Elements as follow:
  *        array  choices      Menu options for the user
  *        mixed  default      Given as null option in the menu
  *        string message      Prompt printed to STDOUT
  *        bool   return_value If true, returns selection. False, the index
  * @return string Either the selection, its index, or the default
  */
 public function menu(array $arg_options = [])
 {
     $default_options = ['choices' => [$this->NULL_INPUTS[0]], 'default' => null, 'message' => 'Select one', 'return_value' => false];
     $options = array_merge($default_options, $arg_options);
     if (count($options['choices']) == 1) {
         $index = 0;
     } else {
         $index = \cli\Streams::menu($options['choices'], $options['default'], $options['message']);
     }
     if ($options['return_value']) {
         return $options['choices'][$index];
     }
     return $index;
 }
예제 #5
0
 /**
  * Offers a menu to user and returns selection
  *
  * @param [array]   $data         Menu items
  * @param [mixed]   $default      Default menu selection
  * @param [string]  $text         Prompt text for menu
  * @param [boolean] $return_value True to return selected value, false for
  *   list ordinal
  * @return [string] $data[$index] or $index
  */
 static function menu($data, $default = null, $text = 'Select one', $return_value = false)
 {
     echo PHP_EOL;
     $index = \cli\Streams::menu($data, $default, $text);
     if ($return_value) {
         return $data[$index];
     }
     return $index;
 }
예제 #6
0
/**
 * Displays an array of strings as a menu where a user can enter a number to
 * choose an option. The array must be a single dimension with either strings
 * or objects with a `__toString()` method.
 *
 * @param array   $items    The list of items the user can choose from.
 * @param string  $default  The index of the default item.
 * @param string  $title    The message displayed to the user when prompted.
 * @return string  The index of the chosen item.
 * @see cli\line()
 * @see cli\input()
 * @see cli\err()
 */
function menu($items, $default = false, $title = 'Choose an item')
{
    return \cli\Streams::menu($items, $default, $title);
}