Search code examples
powershellpowershell-3.0tabexpansion

Formatting tab argument completion powershell


I'm using TabExpansion2 in PowerShell 3 and when I tab to complete an argument it brings up the string I want but wrapped in syntax I don't want.

For instance when I hit tab after -binName:

Use-Bin -binName @{Name=5.0}

what I need is:

Use-Bin -binName 5.0

I'm using this script: https://www.powershellgallery.com/packages/PowerShellCookbook/1.3.6/Content/TabExpansion.ps1

with these adjusted options:

$options["CustomArgumentCompleters"] = @{
            "binName" = {Get-ChildItem -Path $global:TH_BinDir | Select-Object Name}
            "dbName" = {Get-ChildItem -Path $global:TH_DBDir\RT5.7\ | Select-Object Name}
            "patchSubDir" ={Get-ChildItem -Path $global:TH_BinDir\Patches\ | Select-Object Name}
            "hmiSubDir" = {Get-ChildItem -Path $global:TH_HMIDir | Select-Object Name}
            "moduleScript" = {Get-ChildItem -Path $global:TH_ModPaths | Select-Object Name}
            "items" = {"bins", "databases", "modules"}           
        }

Thanks!


Solution

  • I am not familiar with tabexpansion but your issue is you are returning objects with name properties. You want to return just the strings.

    $options["CustomArgumentCompleters"] = @{
        "binName" = {Get-ChildItem -Path $global:TH_BinDir | Select-Object -ExpandProperty Name}
        "dbName" = {Get-ChildItem -Path $global:TH_DBDir\RT5.7\ | Select-Object -ExpandProperty Name}
        "patchSubDir" ={Get-ChildItem -Path $global:TH_BinDir\Patches\ | Select-Object -ExpandProperty Name}
        "hmiSubDir" = {Get-ChildItem -Path $global:TH_HMIDir | Select-Object -ExpandProperty Name}
        "moduleScript" = {Get-ChildItem -Path $global:TH_ModPaths | Select-Object -ExpandProperty Name}
        "items" = {"bins", "databases", "modules"}   
    }
    

    Since you are using 3.0 this would be more terse and accomplish the same thing.

    $options["CustomArgumentCompleters"] = @{
        "binName" = {(Get-ChildItem -Path $global:TH_BinDir).Name}
        "dbName" = {(Get-ChildItem -Path $global:TH_DBDir\RT5.7\).Name}
        "patchSubDir" ={(Get-ChildItem -Path $global:TH_BinDir\Patches\).Name}
        "hmiSubDir" = {(Get-ChildItem -Path $global:TH_HMIDir).Name}
        "moduleScript" = {(Get-ChildItem -Path $global:TH_ModPaths).Name}
        "items" = {"bins", "databases", "modules"}           
    }
    

    Both solutions work by expanding the strings of the single property name.