Last active
November 22, 2024 20:37
-
-
Save CN-CODEGOD/0e7a583ebe22dd498060c04612345897 to your computer and use it in GitHub Desktop.
使用powershell 添加autocompletion
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 1.Dynamic ValidateSet values using classes | |
| Class SoundNames : System.Management.Automation.IValidateSetValuesGenerator { | |
| [string[]] GetValidValues() { | |
| $SoundPaths = '/System/Library/Sounds/', | |
| '/Library/Sounds', | |
| '~/Library/Sounds' | |
| $SoundNames = ForEach ($SoundPath in $SoundPaths) { | |
| If (Test-Path $SoundPath) { | |
| (Get-ChildItem $SoundPath).BaseName | |
| } | |
| } | |
| return [string[]] $SoundNames | |
| } | |
| } | |
| The [SoundNames] class is then implemented as a dynamic ValidateSet value as follows: | |
| PowerShell | |
| Copy | |
| Param( | |
| [ValidateSet([SoundNames])] | |
| [string]$Sound | |
| ) | |
| 2.ArgumentCompletions attribute | |
| [CmdletBinding()] | |
| param ( | |
| [Parameter(Mandatory=$true)] | |
| [ArgumentCompletions('Fruits', 'Vegetables')] | |
| $Type, | |
| [Parameter()] | |
| [ArgumentCompletions('Apple', 'Banana', 'Orange')] | |
| $Fruit, | |
| [Parameter()] | |
| [ArgumentCompletions('Onion', 'Carrot', 'Lettuce')] | |
| $Vegetable | |
| ) | |
| 3. | |
| function MyArgumentCompleter { | |
| Param( | |
| [Parameter(Mandatory)] | |
| [ArgumentCompleter( { | |
| param ( $commandName, | |
| $parameterName, | |
| $wordToComplete, | |
| $commandAst, | |
| $fakeBoundParameters ) | |
| # Perform calculation of tab completed values here. | |
| } )] | |
| $ParamName | |
| ) | |
| } | |
| ArgumentCompleter script block | |
| The script block parameters are set to the following values: | |
| $commandName (Position 0) - This parameter is set to the name of the command for which the script block is providing tab completion. | |
| $parameterName (Position 1) - This parameter is set to the parameter whose value requires tab completion. | |
| $wordToComplete (Position 2) - This parameter is set to value the user has provided before they pressed Tab. Your script block should use this value to determine tab completion values. | |
| $commandAst (Position 3) - This parameter is set to the Abstract Syntax Tree (AST) for the current input line. For more information, see the AST type documentation. | |
| $fakeBoundParameters (Position 4) - This parameter is set to a hashtable containing the $PSBoundParameters for the cmdlet, before the user pressed Tab. For more information, see about_Automatic_Variables. | |
| The ArgumentCompleter script block must unroll the values using the pipeline, such as ForEach-Object, Where-Object, or another suitable method. Returning an array of values causes PowerShell to treat the entire array as one tab completion value. | |
| The following example adds tab completion to the Value parameter. If only the Value parameter is specified, all possible values, or arguments, for Value are displayed. When the Type parameter is specified, the Value parameter only displays the possible values for that type. | |
| In addition, the -like operator ensures that if the user types the following command and uses Tab completion, only Apple is returned. | |
| Test-ArgumentCompleter -Type Fruits -Value A | |
| function MyArgumentCompleter{ | |
| param ( $commandName, | |
| $parameterName, | |
| $wordToComplete, | |
| $commandAst, | |
| $fakeBoundParameters ) | |
| $possibleValues = @{ | |
| Fruits = @('Apple', 'Orange', 'Banana') | |
| Vegetables = @('Onion', 'Carrot', 'Lettuce') | |
| } | |
| if ($fakeBoundParameters.ContainsKey('Type')) { | |
| $possibleValues[$fakeBoundParameters.Type] | Where-Object { | |
| $_ -like "$wordToComplete*" | |
| } | |
| } else { | |
| $possibleValues.Values | ForEach-Object {$_} | |
| } | |
| } | |
| function Test-ArgumentCompleter { | |
| [CmdletBinding()] | |
| param ( | |
| [Parameter(Mandatory=$true)] | |
| [ValidateSet('Fruits', 'Vegetables')] | |
| $Type, | |
| [Parameter(Mandatory=$true)] | |
| [ArgumentCompleter({ MyArgumentCompleter @args })] | |
| $Value | |
| ) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment