Skip to content

Encoding completer bug #134

@kMitty

Description

@kMitty

Description

  1. 'Get-AllowedEncoding' function relies on 'Out-File', but it could be overloaded - for example with WindowsOutFilePatch
  2. Scriptblock parameter of Register-ArgumentCompleter for 'Export-Ini' missed param block, so $wordToComplete is always empty string

Steps To Reproduce

In PowerShell console:
PS> Export-Ini -Encoding a
press Tab
Result:
PS> Export-Ini -Encoding unknown

Expected behaviors

In PowerShell console:
PS> Export-Ini -Encoding a
press Tab
Result:
PS> Export-Ini -Encoding ascii

Your Environment

PsIni 4.0.1

PSVersion 5.1.14409.1029
PSEdition Desktop
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
BuildVersion 10.0.14409.1029
CLRVersion 4.0.30319.42000
WSManStackVersion 3.0
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1

Possible Solution

function Get-AllowedEncoding {
    # targeting built-in Cmdlet
    $command = Get-Command -Name Microsoft.PowerShell.Utility\Out-File

    if ($PSVersionTable.PSVersion.Major -ge 6) {
        (
            $command.Parameters['Encoding'].Attributes |
                Where-Object { $_ -is [ArgumentCompletions] }
        )[0].CompleteArgument('Out-File', 'Encoding', '*', $null, @{ }).CompletionText
    }
    else {
        (
            $command.Parameters['Encoding'].Attributes |
                Where-Object { $_.TypeId -eq [ValidateSet] }
        )[0].ValidValues
    }
}

$EncodingCompleter = {
# missed param block
param(
	$commandName,
	$parameterName,
	$wordToComplete,
	$commandAst,
	$fakeBoundParameters
)

Get-AllowedEncoding |
	Where-Object { $_ -like "$wordToComplete*" } |
	ForEach-Object {
		[System.Management.Automation.CompletionResult]::new(
			$_,
			$_,
			[System.Management.Automation.CompletionResultType]::ParameterValue,
			$_
		)
	}
}

# Using the same completer for both functions
Register-ArgumentCompleter -CommandName Export-Ini -ParameterName Encoding -ScriptBlock $EncodingCompleter
Register-ArgumentCompleter -CommandName Import-Ini -ParameterName Encoding -ScriptBlock $EncodingCompleter

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions