【问题标题】:PowerShell alias is not seeing parameters during tab completionPowerShell 别名在选项卡完成期间看不到参数
【发布时间】:2020-03-19 17:47:37
【问题描述】:

我正在使用一个可以执行一些更改目录功能的函数,然后将/重新别名 cd 重定向到该函数。

function go ($jumpto, [switch]$DisableAliasCD, [switch]$ResetGoHash, [switch]$ResetCDHistory) {
    <stuff>
}

Set-Alias cc go
if (Test-Path Alias:cd) { Remove-Item -Path Alias:cd }    # Remove the default:   cd -> Set-Location
# Remove-Alias was not added until PS v6 so have to use Remove-Item with the Alias PSProvider
Set-Alias cd go -Option AllScope

检查每个gocccd的语法

get-command go -syntax
go [[-jumpto] <Object>] [-DisableAliasCD] [-ResetGoHash] [-ResetCDHistory]
get-command cc -syntax
go
get-command cd -syntax
go

这似乎是有道理的。但是,我发现 IntelliSense 对于 cc 工作正常(即,如果我输入 cc - 然后 Tab 出现上述 4 个参数,但对于 cd 没有任何反应。

这是一个错误吗?或者这与使用AllScope 选项声明Set-Alias cd 有什么关系(我必须这样做,Set-Alias 没有它对于cd 不起作用)

【问题讨论】:

标签: powershell scope alias tab-completion


【解决方案1】:

您似乎遇到了一个在 PowerShell 7.0 中仍然存在的错误

大多数 (Windows PowerShell) / 少数 (PowerShell [Core] 6+) 内置别名是使用 AllScope 选项定义的。
(您可以通过
powershell -NoProfile { Get-Alias | ? options -like '*AllScope*' }
发现它们 /
pwsh -NoProfile { Get-Alias | ? options -like '*AllScope*' })。

重新定义 any 别名在调用方面正常工作。

然而,tab补全而言,重新定义AllScope的故障,如你所描述:original别名定义的参数依然正在完成。

# Sample function
function foo { param($bar) "[$bar]" }

# Remove the built-in AllScope `cd` alias and redefine it to execute `foo`.
Remove-Item alias:cd; Set-Alias cd foo

# Make sure that the redefinition works:
cd -bar baz # OK: prints '[baz]'

# Try to tab-complete:
cd -b<tab> # NO COMPLETION instead of the expected '-bar'

# Try a parameter from `cd`'s *original* definition, `Set-Location`:
cd -li<tab> # UNEXPECTEDLY EXPANDS TO '-LiteralPath'

this GitHub issue 已报告此问题行为。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-11-08
    • 1970-01-01
    • 1970-01-01
    • 2011-04-04
    • 1970-01-01
    • 2017-03-12
    • 2023-04-10
    相关资源
    最近更新 更多