Iga*_*nik 4 powershell tab-completion
我想知道是否可以在 PowerShell 中创建自定义选项卡补全,以便在某个命令后提供特定的参数列表?
从命令行启动 Visual Studio 时,您可以键入devenv /rootsuffix HiveName让 Visual Studio 启动具有该名称的新“配置单元”,从而HiveName在磁盘上的特殊位置创建一个名为的文件夹。
我希望能够输入 PowerShell: devenv /rootsuffix [tab],并获取现有配置单元的列表(通过在目录/注册表中查找,没关系),覆盖默认行为,即文件名完成。
做这样的事情可能吗?
这可以使用命令来完成Register-ArgumentCompleter。
以下是使用该命令的代码示例(可以将其复制并粘贴到 PowerShell 窗口中):
# Function that will be registered with the command
function Cmd {
Param(
[string] $Param
)
Write-Host "Param: $param"
}
# The code that will perform the auto-completion
$scriptBlock = {
# The parameters passed into the script block by the
# Register-ArgumentCompleter command
param(
$commandName, $parameterName, $wordToComplete,
$commandAst, $fakeBoundParameters
)
# The list of values that the typed text is compared to
$values = 'abc','adf','ghi'
foreach ($val in $values) {
# What has been typed matches the value from the list
if ($val -like "$wordToComplete*") {
# Print the value
$val
}
}
}
# Register our function and auto-completion code with the command
Register-ArgumentCompleter -CommandName Cmd `
-ParameterName Param -ScriptBlock $scriptBlock
Run Code Online (Sandbox Code Playgroud)
我们可以通过在 shell 中键入以下不完整的命令并按提示符中的TAB键(键入的位置)来检查此代码是否正常工作:<PRESS TAB>
# Using autocomplete with the TAB key will produce the results: abc, adf
PS > Cmd -Param a<PRESS TAB>
# Using autocomplete with the TAB key will produce the results: abc
PS > Cmd -Param ab<PRESS TAB>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2243 次 |
| 最近记录: |