Sep 032011
If you have ever developed a user-interactive script then you are well aware of the need to provide user choices and validate input. It seems that I’ve done this hundreds of times, continuously re-inventing code that performs three simple functions:
- Present the user with choices
- Accept input from the user
- Validate the user’s choice
To that end, I developed the “SelectFromList” function. Note, to use this function, you must also include the “isNumeric” function.
Here are the steps needed to use this function:
- Create a string array containing the choices that you will present to the user
- Call the SelectFromList function, passing the string array created in step 1 and receive a response to a variable
Example: $choice = SelectFromList $ArrayOfChoices - Test for a non-null response in the value returned from the function call
- A non-null response will be the numeric index value of the users choice from the selection array
- A null response indicates that the user quit without making a choice
The user is not permitted to make an invalid choice. If an invalid choice is received, the user is re-prompted for a valid response.
Screenshot:
Code:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# # SelectFromList.ps1 # # Date : 09/02/2011 # Author: xb90@PoshTips.com # # Get more FREE PowerShell scripts at http://poshtips.com # # Description: # This is a sample script to demonostrate the use of the function: SelectFromList # To use, copy SelectFromList AND isNumeric functions to your own script and call # SelectFromList, passing a string array containing selection choices. # function isNumeric ($x) { $x2 = 0 $isNum = [System.Int32]::TryParse($x, [ref]$x2) return $isNum } function SelectFromList { param([string[]]$List,[string]$Title="Choices",[switch]$verbose=$false) write-host $Title.padright(80) -back green -fore black $digits = ([string]$List.length).length $fmt = "{0,$digits}" #display selection list for ($LN=0; $LN -lt $List.length) { write-host (" $fmt : $($List[$LN])" -f ++$ln) } #query user until valid selection is made do { write-host (" Please select from list (1 to {0}) or `"q`" to quit" -f ($list.length)) ` -back black -fore green -nonewline $sel = read-host " " if ($sel -eq "q") { write-host " quiting selection per user request..." -back black -fore yellow } elseif (isNumeric $sel) { if (([int]$sel -gt 0) -and ([int]$sel -le $list.length)) { if ($verbose) { write-host (" You selected item #{0} ({1})" -f $sel,$List[$sel-1]) ` -back black -fore green } } else { $sel = $null } } else { $sel = $null } } until ($sel) if (isNumeric $sel) {$sel -1} else {$null} } # # Sample code to use the "SelectFromList" function # $choices = ("Apple","Orange","Lemon","Pineapple","Mango","Banana","Lilikoi","Rambutan","Lychee") clear-host $sel = SelectFromList $choices " Please choose your favorite fruit" if ($sel -ne $null) { write-host "`nYUM! $($choices[$sel]) is a great choice!`n" } else { write-host "`nYou don't like any of these choices?`n" -back black -fore red } |

so, what happens when the user selects the first option ….
is re-using $sel a good idea – I’m running into all kinds of logic errors.
if ($sel) {
should be
if ($sel -ne $null) {
or else if you choose the first option, it will resolve to false (0) …
Jordon – you are absolutely right! Not sure how that got past my QA process
Your suggested correction has been made to the code sample.
Thanks!