Skip to content

Argument Completers

Roman Kuzmin edited this page May 20, 2017 · 6 revisions

PowerShell v3+ provides powerful code completion tools and ability to customize and extend them even though official guidelines are seemingly missing. The most useful feature is custom argument completers.

The script Invoke-Build.ArgumentCompleters.ps1 registers Invoke-Build completers. In PowerShell v5 it can be installed to the path from PSGallery:

    Install-Script Invoke-Build.ArgumentCompleters

or downloaded:

    Save-Script Invoke-Build.ArgumentCompleters -Path .

Completers are defined for two parameters:

  • Task - Task names are completed from the default or specified build script.
  • File - For ** gets directories otherwise directories and .ps1 files.

Note that it is possible to complete Invoke-Build dynamic parameters which come from a build script, the default or specified. This feature is built-in and it should work right away without custom tools.

How to use with different completion engines

PowerShell v5 native Register-ArgumentCompleter

Simply invoke Invoke-Build.ArgumentCompleters.ps1, e.g. in a profile.

Put Invoke-Build.ArgumentCompleters.ps1 to the TabExpansionPlusPlus module directory in order to be loaded automatically. Or invoke it after importing the module, e.g. in a profile.

Put Invoke-Build.ArgumentCompleters.ps1 to the path in order to be loaded automatically on the first completion. Or invoke after TabExpansion2.ps1, e.g. in a profile.

Use $WhatIf or Enter-Build in build scripts

This is recommended for all build scripts and especially for scripts designed for task name completion. If a build script does anything significant before invoking tasks then this code should be wrapped by if (!$WhatIf) {...} or Enter-Build {...}. Otherwise it is invoked on task name completion, getting task info (Invoke-Build ?), showing code (Invoke-Build -WhatIf), and other similar scenarios.