【发布时间】:2023-02-03 03:41:18
【问题描述】:
我想创建“Select-Multiple”功能。
该函数有一些参数,但最重要的参数是选项列表。
比方说
@("First Option", "Second Option")
然后该函数将显示如下内容:
全部
b 第一个选项
c 第二个选项
d 退出
选择您的选项: > ...
“选择您的选项:> ...”文本将重复出现,只要:
- 用户选择“全部”或“退出”选项
- 用户将选择所有可能的选项(“全部”和“退出”除外)
最后,该函数返回用户选择的选项列表。
简单的。但是......我想强调用户已经选择的选项。 因此,如果用户选择“b”,则“b First Option”会变成绿色。
是否可以在不使用
Clear-Host的情况下执行类似的操作,因为我不想清除之前的步骤?我在 powershell 中附上了我的“Select-Multiple”功能,如果写得不好,我很抱歉,但我不经常使用 powershell。
function Select-Multiple { Param( [Parameter(Mandatory=$false)] [string] $title, [Parameter(Mandatory=$false)] [string] $description, [Parameter(Mandatory=$true)] [ValidateNotNullOrEmpty()] $options, [Parameter(Mandatory=$true)] [string] $question ) if ($title) { Write-Host -ForegroundColor Yellow $title Write-Host -ForegroundColor Yellow ("-"*$title.Length) } if ($description) { Write-Host $description Write-Host } $chosen = @() $values = @() $offset = 0 $all = "All" $values += @($all) Write-Host -ForegroundColor Yellow "$([char]($offset+97)) " -NoNewline Write-Host $all $offset++ $options.GetEnumerator() | ForEach-Object { Write-Host -ForegroundColor Yellow "$([char]($offset+97)) " -NoNewline $values += @($_) Write-Host $_ $offset++ } $exit = "Exit" $values += @($exit) Write-Host -ForegroundColor Yellow "$([char]($offset+97)) " -NoNewline Write-Host $exit $answer = -1 while($chosen.Count -ne $options.Count) { Write-Host "$question " -NoNewline $selection = (Read-Host).ToLowerInvariant() if (($selection.Length -ne 1) -or (([int][char]($selection)) -lt 97 -or ([int][char]($selection)) -gt (97+$offset))) { Write-Host -ForegroundColor Red "Illegal answer. " -NoNewline } else { $answer = ([int][char]($selection))-97 $value = $($values)[$answer] if ($value -eq $exit) { return $chosen } if ($value -eq $all) { return $options } else { if ($chosen.Contains($value)) { Write-Host -ForegroundColor Red "The value $value was already chosen." } else { $chosen += ($value) } } } if ($answer -eq -1) { Write-Host -ForegroundColor Red "Please answer one letter, from a to $([char]($offset+97))" } $answer = -1; } return $chosen }
【问题讨论】:
-
我不相信这可以使用写主机来完成。你可以有一个输出链,每个新的输出都可以有亮点,或者你可以按照你的建议做,清除主机,然后只做一个新的写入主机。
标签: powershell