哪些 cmdlet 使用 IHostUISupportsMultipleChoiceSelection 接口来提示选择?

发布于 2024-09-18 16:01:11 字数 102 浏览 2 评论 0原文

我不记得以前在 PowerShell 中曾被提示进行多项选择,但我见过几个实现此接口的主机示例。不幸的是,这些是我见过的对该界面的唯一参考。我从未见过“这是如何测试您是否正确实施它的方法”。

I don't remember ever being prompted for multiple selections before in PowerShell, but I've seen several examples of hosts implementing this interface. Unfortunately, those are the only references I've seen to the interface. I've never seen "here's how to test that you're implementing it correctly".

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

兲鉂ぱ嘚淚 2024-09-25 16:01:12

例如:命令 Remove-Item C:\TEMP\Test 提示您选择:

Confirm
The item at C:\TEMP\Test has children and the Recurse parameter was not specified. If you continue, all children will be removed with the item. Are you sure you want to continue?
[Y] Yes  [A] Yes to All  [N] No  [L] No to All  [S] Suspend  [?] Help (default is "Y"):

或者您可以使用此脚本(或其想法)构建您自己的调用:
Get-Choice.ps1 - 显示 PowerShell 样式菜单并获取用户选择< /a>

For example: the command Remove-Item C:\TEMP\Test prompts you to choose:

Confirm
The item at C:\TEMP\Test has children and the Recurse parameter was not specified. If you continue, all children will be removed with the item. Are you sure you want to continue?
[Y] Yes  [A] Yes to All  [N] No  [L] No to All  [S] Suspend  [?] Help (default is "Y"):

Or you can build your own call using this script (or its idea):
Get-Choice.ps1 - Displays PowerShell style menu and gets a user choice

因为看清所以看轻 2024-09-25 16:01:11

请忽略我的第一个答案;正如我现在所看到的,这根本不是一个答案。谢谢你提出了一个非常有趣的问题。

我仍然不知道使用该接口的 cmdlet。但我们可以通过脚本自己使用它。让我们修改提到的 Get-Choice.ps1 并调用新的 Get-Choice2.ps1:

<#
.SYNOPSIS
    Displays PowerShell style menu and gets user choices

.DESCRIPTION
    *) Returns choice indexes.
    *) Choice keys are indicated by '&' in menu items.
    *) Help strings can be empty or nulls (items are used themselves).
#>

param
(
    # Menu caption
    [string]$Caption = 'Confirm',
    # Menu message
    [string]$Message = 'Are you sure you want to continue?',
    # Choice info pairs: item1, help1, item2, help2, ...
    [string[]]$Choices = ('&Yes', 'Continue', '&No', 'Stop'),
    # Default choice indexes (i.e. selected on [Enter])
    [int[]]$DefaultChoice = @(0)
)
if ($args) { throw "Unknown parameters: $args" }
if ($Choices.Count % 2) { throw "Choice count must be even." }

$descriptions = @()
for($i = 0; $i -lt $Choices.Count; $i += 2) {
    $c = [System.Management.Automation.Host.ChoiceDescription]$Choices[$i]
    $c.HelpMessage = $Choices[$i + 1]
    if (!$c.HelpMessage) {
        $c.HelpMessage = $Choices[$i].Replace('&', '')
    }
    $descriptions += $c
}

$Host.UI.PromptForChoice($Caption, $Message, [System.Management.Automation.Host.ChoiceDescription[]]$descriptions, $DefaultChoice)

现在我们测试它:

Get-Choice2 'Title' 'Message' -DefaultChoice 0, 1, 2 -Choices @(
    'Choice &1', 'This is choice 1'
    'Choice &2', ''
    'Choice &3', ''
    'Choice &4', ''
    'Choice &5', ''
    'Choice &6', ''
    'Choice &7', ''
    'Choice &8', ''
    'Choice &9', ''
    'Choice &0', ''
)

它打印 10 个选择,前 3 个突出显示(在控制台主机中),并提示:

0> Test-Get-Choice2.ps1
Title
Message
[1] Choice 1
[2] Choice 2
[3] Choice 3
[4] Choice 4
[5] Choice 5
[6] Choice 6
[7] Choice 7
[8] Choice 8
[9] Choice 9
[0] Choice 0
[?] Help
(default choices are 1,2,3)
Choice[0]:

如果我们按 Enter 立即输出是默认的 3 个索引:0、1、2。如果我们输入,例如:5 + Enter + 3 + Enter + 1 + Enter + Enter 那么输出是 4, 2, 0。

它有效。 PowerShell ISE 也支持此功能,但 GUI 版本中的 UI 可能会更好。

Please, disregard my first answer; it is not an answer at all, as I can see now. And thank you for a really interesting question.

I still do not know cmdlets that use that interface. But we can use it on our own from scripts. Let's modify the mentioned Get-Choice.ps1 and call the new one Get-Choice2.ps1:

<#
.SYNOPSIS
    Displays PowerShell style menu and gets user choices

.DESCRIPTION
    *) Returns choice indexes.
    *) Choice keys are indicated by '&' in menu items.
    *) Help strings can be empty or nulls (items are used themselves).
#>

param
(
    # Menu caption
    [string]$Caption = 'Confirm',
    # Menu message
    [string]$Message = 'Are you sure you want to continue?',
    # Choice info pairs: item1, help1, item2, help2, ...
    [string[]]$Choices = ('&Yes', 'Continue', '&No', 'Stop'),
    # Default choice indexes (i.e. selected on [Enter])
    [int[]]$DefaultChoice = @(0)
)
if ($args) { throw "Unknown parameters: $args" }
if ($Choices.Count % 2) { throw "Choice count must be even." }

$descriptions = @()
for($i = 0; $i -lt $Choices.Count; $i += 2) {
    $c = [System.Management.Automation.Host.ChoiceDescription]$Choices[$i]
    $c.HelpMessage = $Choices[$i + 1]
    if (!$c.HelpMessage) {
        $c.HelpMessage = $Choices[$i].Replace('&', '')
    }
    $descriptions += $c
}

$Host.UI.PromptForChoice($Caption, $Message, [System.Management.Automation.Host.ChoiceDescription[]]$descriptions, $DefaultChoice)

Now we test it:

Get-Choice2 'Title' 'Message' -DefaultChoice 0, 1, 2 -Choices @(
    'Choice &1', 'This is choice 1'
    'Choice &2', ''
    'Choice &3', ''
    'Choice &4', ''
    'Choice &5', ''
    'Choice &6', ''
    'Choice &7', ''
    'Choice &8', ''
    'Choice &9', ''
    'Choice &0', ''
)

It prints 10 choices, the first 3 are highlighted (in the console host), and prompts:

0> Test-Get-Choice2.ps1
Title
Message
[1] Choice 1
[2] Choice 2
[3] Choice 3
[4] Choice 4
[5] Choice 5
[6] Choice 6
[7] Choice 7
[8] Choice 8
[9] Choice 9
[0] Choice 0
[?] Help
(default choices are 1,2,3)
Choice[0]:

If we press Enter immediately the output is the default 3 indexes: 0, 1, 2. If we type, for example: 5 + Enter + 3 + Enter + 1 + Enter + Enter then the output is 4, 2, 0.

It works. PowerShell ISE also supports this but the UI might be something better in GUI version, perhaps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文