From a6951912c2b692103bf78064b46ac7df5f0b2970 Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Sun, 12 Jan 2025 21:26:07 +0100 Subject: [PATCH] Remove dynamic param and use ArgumentCompleter instead --- src/functions/public/Install-NerdFont.ps1 | 21 +-------------------- src/functions/public/completers.ps1 | 8 ++++++++ 2 files changed, 9 insertions(+), 20 deletions(-) create mode 100644 src/functions/public/completers.ps1 diff --git a/src/functions/public/Install-NerdFont.ps1 b/src/functions/public/Install-NerdFont.ps1 index e60f1eb..fabe4d9 100644 --- a/src/functions/public/Install-NerdFont.ps1 +++ b/src/functions/public/Install-NerdFont.ps1 @@ -1,4 +1,4 @@ -#Requires -Modules Admin, Fonts, DynamicParams +#Requires -Modules Admin, Fonts function Install-NerdFont { <# @@ -38,25 +38,6 @@ function Install-NerdFont { [Scope] $Scope = 'CurrentUser' ) - dynamicparam { - $DynamicParamDictionary = New-DynamicParamDictionary - - $dynPath = @{ - Name = 'Name' - Type = [string[]] - Mandatory = $true - ParameterSetName = 'Name' - HelpMessage = 'Name of the font to install.' - ValueFromPipeline = $true - ValueFromPipelineByPropertyName = $true - ValidateSet = Get-NerdFonts | Select-Object -ExpandProperty Name - DynamicParamDictionary = $DynamicParamDictionary - } - New-DynamicParam @dynPath - - return $DynamicParamDictionary - } - begin { if ($Scope -eq 'AllUsers' -and -not (IsAdmin)) { $errorMessage = @' diff --git a/src/functions/public/completers.ps1 b/src/functions/public/completers.ps1 new file mode 100644 index 0000000..7234fde --- /dev/null +++ b/src/functions/public/completers.ps1 @@ -0,0 +1,8 @@ +Register-ArgumentCompleter -CommandName Install-NerdFont, Get-NerdFont -ParameterName Name -ScriptBlock { + param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter) + $null = $commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter + + Get-NerdFont | Where-Object { $_.Name -like $wordToComplete } | ForEach-Object { + [System.Management.Automation.CompletionResult]::new($_.Name, $_.Name, 'ParameterValue', $_.Name) + } +}