PoshCode Archive  Artifact [1d2bf3de7e]

Artifact 1d2bf3de7ea9895ff9d262d6128315a552b5a468b3b0e8234e6af2f940d69f04:

  • File Read-Choice.ps1 — part of check-in [64ce942d99] at 2018-06-10 13:00:43 on branch trunk — A wrapper for $Host.UI.PromptForChoice (user: Joel Bennett size: 1416)

# encoding: ascii
# api: powershell
# title: Read-Choice
# description: A wrapper for $Host.UI.PromptForChoice
# version: 0.1
# type: function
# author: Joel Bennett
# license: CC0
# function: Read-Choice
# x-poshcode-id: 1740
# x-archived: 2015-11-28T15:20:33
# x-published: 2010-04-05T20:01:00
#
#
function Read-Choice {
#.Synopsis
#  Prompt the user for a choice, and return the (0-based) index of the selected item
#.Parameter Message
#  The question to ask
#.Parameter Choices
#  An array of strings representing the "menu" items, with optional ampersands (&) in them to mark (unique) characters to be used to select each item
#.Parameter DefaultChoice
#  The (0-based) index of the menu item to select by default (defaults to zero).
#.Parameter Title
#  An additional caption that can be displayed (usually above the Message) as part of the prompt
#.Example
#  Read-Choice "WEBPAGE BUILDER MENU"  "Create Webpage","View HTML code","Publish Webpage","Remove Webpage","E&xit"
PARAM([string]$message, [string[]]$choices, [int]$defaultChoice=0, [string]$Title=$null )
   if($choices[0].IndexOf('&') -lt 0) {
      $i = 0; 
      $choices = $choices | ForEach-Object {
         if($_ -notmatch '&.') { "&$i $_" } else { $_ }
         $i++
      }
   }
   $Host.UI.PromptForChoice( $caption, $message, [Management.Automation.Host.ChoiceDescription[]]$choices, $defaultChoice )
}