PoshCode Archive  Artifact [1d639bcda9]

Artifact 1d639bcda968baf659c52c9e4f2914310ef4b0e1f8179b0e829aec63cda27a7d:

  • File Read-Choice.ps1 — part of check-in [475f120458] at 2018-06-10 13:00:45 on branch trunk — A wrapper for $Host.UI.PromptForChoice (fixed a copy-paste error) (user: Joel Bennett size: 1441)

# encoding: ascii
# api: powershell
# title: Read-Choice
# description: A wrapper for $Host.UI.PromptForChoice (fixed a copy-paste error)
# version: 0.1
# type: function
# author: Joel Bennett
# license: CC0
# function: Read-Choice
# x-poshcode-id: 1742
# x-archived: 2015-11-28T15:13:04
# x-published: 2010-04-06T20:11: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( $Title, $message, [Management.Automation.Host.ChoiceDescription[]]$choices, $defaultChoice )
}