一點背景知識:我們正在開發一個經過數百個條目的函式,類似于以下內容:
| 城市 | 狀態 | 人口 |
|---|---|---|
| 紐約 | 紐約 | 8467513 |
| 洛杉磯 | 加利福尼亞 | 3849297 |
| 芝加哥 | 伊利諾伊州 | 2696555 |
| 休斯頓 | 德克薩斯州 | 2288250 |
| 鳳凰 | 亞利桑那 | 1624569 |
| 費城 | 賓夕法尼亞州 | 1576251 |
| 圣安東尼奧 | 德克薩斯州 | 1451853 |
| 圣地亞哥 | 加利福尼亞 | 1381611 |
| 達拉斯 | 德克薩斯州 | 1288457 |
| 圣荷西 | 加利福尼亞 | 983489 |
原始資料將使用Import-Csv. CSV 會定期更新。
我們正在嘗試使用 PowerShell 類來使人們能夠City根據State他們選擇的內容進行選擇。這是到目前為止我們得到的 MWE:
$Global:StateData = Import-Csv \\path\to\city-state-population.csv
class State : System.Management.Automation.IValidateSetValuesGenerator {
[string[]] GetValidValues() {
return (($Global:StateData).State | Select-Object -Unique)
}
}
class City : System.Management.Automation.IValidateSetValuesGenerator {
[string[]] GetValidValues($State) {
return ($Global:StateData | Where-Object State -eq $State).City
}
}
function Get-Population {
param (
# Name of the state
[Parameter(Mandatory, Position = 0)]
[ValidateSet([State])]
$State,
# Name of the city
[Parameter(Mandatory, Position = 1)]
[ValidateSet([City])]
$City
)
$City | ForEach-Object {
$TargetCity = $City | Where-Object City -match $PSItem
"The population of $($TargetCity.City), $($TargetCity.State) is $($TargetCity.Population)."
}
}
當然,按照
uj5u.com熱心網友回復:
我喜歡將Register-ArgumentCompletercmdlet 用于此類事情。如果您只是在尋找引數完成,那么它將完美地作業。您必須在函式中自己進行驗證,因為它不會阻止輸入錯誤的條目。
但是,它將提供可能引數的串列,并且顯示的城市將僅是與所選州相關的城市。
這是一個例子。
$Data = @'
City|State|Population
New York|New York|8467513
Los Angeles|California|3849297
Chicago|Illinois|2696555
Houston|Texas|2288250
Phoenix|Arizona|1624569
Philadelphia|Pennsylvania|1576251
San Antonio|Texas|1451853
San Diego|California|1381611
Dallas|Texas|1288457
San Jose|California|983489
'@|ConvertFrom-Csv -Delimiter '|'
Function Get-Population{
Param(
[Parameter(Mandatory = $true)]
$State,
[Parameter(Mandatory = $true)]
$City
)
return $Data | Where-Object {$_.State -eq $State -and $_.City -eq $City} | Select-Object -ExpandProperty Population
}
$PopulationArgCompletion = {
param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameters)
switch ($ParameterName) {
'State' { $Data | Select-Object -ExpandProperty State -Unique | Sort-Object | % { [System.Management.Automation.CompletionResult]::new($_) } }
'City' {
if ($fakeBoundParameters.ContainsKey('State')) {
$Data | Where-Object -Property State -eq $fakeBoundParameters.Item('State') | Select-Object -ExpandProperty City -Unique | Sort-Object | % { [System.Management.Automation.CompletionResult]::new($_) }
}
else {
$Data | Select-Object -ExpandProperty City -Unique | Sort-Object | % { [System.Management.Automation.CompletionResult]::new($_) }
}
}
}
}
Register-ArgumentCompleter -CommandName Get-Population -ParameterName State -ScriptBlock $PopulationArgCompletion
Register-ArgumentCompleter -CommandName Get-Population -ParameterName City -ScriptBlock $PopulationArgCompletion
附加說明
如果您確實對此進行了測驗,請確保在與執行上述腳本的位置不同的檔案中進行嘗試。Get-Population出于某種原因,如果您嘗試在運行上述腳本的同一檔案中進行測驗(例如:呼叫以查看引數完成),則 VSCode 和/或 PS 擴展名不會顯示引數完成。
獎金 VSCode 代碼段
這是我用來快速生成模板的片段,作為在需要時在任何地方進行引數完成的基礎。
"ArgumentCompletion": {
"prefix": "ArgComplete",
"body": [
"$${1:MyArgumentCompletion} = {",
" param(\\$commandName, \\$parameterName, \\$wordToComplete, \\$commandAst, \\$fakeBoundParameters)",
"",
" # [System.Management.Automation.CompletionResult]::new(\\$_)",
"}",
"",
"Register-ArgumentCompleter -CommandName ${2:Command-Name} -ParameterName ${3:Name} -ScriptBlock $${1:MyArgumentCompletion}"
]
}
參考
注冊引數完成者
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/496663.html
