有没有办法在我的脚本中为用户输入显示 "console selection menu",例如 PSReadLine 中的 Ctrl+Space
Is there a way to show a "console selection menu" for user input in my script like Ctrl+Space in PSReadLine
PSReadLine 具有 FANTASTIC 功能,因为它是 Ctrl+Space 键绑定。
有没有一种方法可以让我的脚本用户(通常是我)从可能的值列表中 select 使用相同的 "console menu" 功能?我不想有一个单独的网格视图 (Out-GridView
) 来制作 selection.
使用高级功能是实现此目的的常用方法之一,如果您想要动态路径完成(根据您的评论),则可以使用 Register-ArgumentCompleter
添加此功能
$scriptBlock = {
param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameters)
Get-ChildItem -Path $pwd -Directory | Where-Object {
$_ -like "*$wordToComplete*"
} | ForEach-Object {
"'$_'"
}
}
#Register the above scriptblock to the foo function Path Parameter
Register-ArgumentCompleter -CommandName foo -ParameterName Path -ScriptBlock $scriptBlock
function foo {
param(
[ValidateSet("Tom","Dick","Jane")]
$Name,
[ValidateRange(21, 65)]
$Age,
[string]
$Path
)
Write-Host ($Name + $Age + $Path)
}
可以通过 Get-Help about_Functions_Advanced
找到更多信息
以上也适用于部分目录名称,例如,如果您知道目录名称中有 "test",请键入 foo -path test
并按 CTRL+Space,您将得到一个筛选列表 - 很酷,对吧?
试一试
PSReadLine 具有 FANTASTIC 功能,因为它是 Ctrl+Space 键绑定。
有没有一种方法可以让我的脚本用户(通常是我)从可能的值列表中 select 使用相同的 "console menu" 功能?我不想有一个单独的网格视图 (Out-GridView
) 来制作 selection.
使用高级功能是实现此目的的常用方法之一,如果您想要动态路径完成(根据您的评论),则可以使用 Register-ArgumentCompleter
$scriptBlock = {
param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameters)
Get-ChildItem -Path $pwd -Directory | Where-Object {
$_ -like "*$wordToComplete*"
} | ForEach-Object {
"'$_'"
}
}
#Register the above scriptblock to the foo function Path Parameter
Register-ArgumentCompleter -CommandName foo -ParameterName Path -ScriptBlock $scriptBlock
function foo {
param(
[ValidateSet("Tom","Dick","Jane")]
$Name,
[ValidateRange(21, 65)]
$Age,
[string]
$Path
)
Write-Host ($Name + $Age + $Path)
}
可以通过 Get-Help about_Functions_Advanced
以上也适用于部分目录名称,例如,如果您知道目录名称中有 "test",请键入 foo -path test
并按 CTRL+Space,您将得到一个筛选列表 - 很酷,对吧?
试一试