diff --git a/cli/completion/completion.go b/cli/completion/completion.go index 1dd2a05c6bb..b73dd190ded 100644 --- a/cli/completion/completion.go +++ b/cli/completion/completion.go @@ -32,8 +32,8 @@ var ( // NewCommand created a new `completion` command func NewCommand() *cobra.Command { command := &cobra.Command{ - Use: "completion [bash|zsh|fish] [--no-descriptions]", - ValidArgs: []string{"bash", "zsh", "fish"}, + Use: "completion [bash|zsh|fish|powershell] [--no-descriptions]", + ValidArgs: []string{"bash", "zsh", "fish", "powershell"}, Args: cobra.ExactArgs(1), Short: tr("Generates completion scripts"), Long: tr("Generates completion scripts for various shells"), @@ -47,7 +47,7 @@ func NewCommand() *cobra.Command { } func run(cmd *cobra.Command, args []string) { - if completionNoDesc && (args[0] == "bash") { + if completionNoDesc && (args[0] == "bash" || args[0] == "powershell") { feedback.Errorf(tr("Error: command description is not supported by %v"), args[0]) os.Exit(errorcodes.ErrGeneric) } @@ -65,5 +65,8 @@ func run(cmd *cobra.Command, args []string) { case "fish": cmd.Root().GenFishCompletion(os.Stdout, !completionNoDesc) break + case "powershell": + cmd.Root().GenPowerShellCompletion(os.Stdout) + break } } diff --git a/docs/command-line-completion.md b/docs/command-line-completion.md index 9d696333cd7..5d1433ccc89 100644 --- a/docs/command-line-completion.md +++ b/docs/command-line-completion.md @@ -1,5 +1,5 @@ `arduino-cli` supports command-line completion (also known as _tab completion_) for basic commands. Currently only -`bash`, `zsh`, `fish` shells are supported +`bash`, `zsh`, `fish`, and `powershell` shells are supported ### Before you start @@ -8,9 +8,9 @@ first. ### Generate the completion file -To generate the completion file you can use `arduino-cli completion [bash|zsh|fish] [--no-descriptions]`. By default -this command will print on the standard output (the shell window) the content of the completion file. To save to an -actual file use the `>` redirect symbol. +To generate the completion file you can use `arduino-cli completion [bash|zsh|fish|powershell] [--no-descriptions]`. By +default this command will print on the standard output (the shell window) the content of the completion file. To save to +an actual file use the `>` redirect symbol. ### Bash @@ -44,13 +44,29 @@ with `mv arduino-cli.fish ~/.config/fish/completions/` Remember to open a new shell to test the functionality. +### Powershell + +Use `arduino-cli completion powershell > arduino-cli.ps1` to generate a temporary completion file. At this point you +need to add the content of the generated file to your PowerShell profile file. + +1. `Get-Content -Path arduino-cli.ps1 | Add-Content -Path $profile` or add it by hand with your favourite text editor. +1. The previous command added two `using namespace` lines, move them on top of the `$profile` file. +1. If not already done, add the line `Set-PSReadlineKeyHandler -Key Tab -Function MenuComplete` to your `$profile` file: + it is needed to enable the TAB completion in PowerShell. +1. `del arduino-cli.ps1` to remove the temporary file. + +Remember to open a new shell to test the functionality. + +For more information on tab-completion on PowerShell, please, refer to +[Autocomplete in PowerShell](https://techcommunity.microsoft.com/t5/itops-talk-blog/autocomplete-in-powershell/ba-p/2604524). + #### Disabling command and flag descriptions By default fish and zsh completion have command and flag description enabled by default. If you want to disable this behaviour you can simply pass the `--no-descriptions` flag when calling `completion` command and the generated file will not have descriptions -_N.B._ This flag is not compatible with bash +_N.B._ This flag is not compatible with bash nor powershell ### Brew diff --git a/test/test_completion.py b/test/test_completion.py index 3d7b02eafef..9a07af2be12 100644 --- a/test/test_completion.py +++ b/test/test_completion.py @@ -45,6 +45,14 @@ def test_completion_fish(run_command): assert "function __arduino_cli_perform_completion" in result.stdout +def test_completion_powershell(run_command): + result = run_command("completion powershell") + assert result.ok + assert result.stderr == "" + assert "Register-ArgumentCompleter -Native -CommandName 'arduino-cli' -ScriptBlock {" in result.stdout + assert "'arduino-cli;completion' {" in result.stdout + + def test_completion_bash_no_desc(run_command): result = run_command("completion bash --no-descriptions") assert not result.ok @@ -68,3 +76,10 @@ def test_completion_fish_no_desc(run_command): assert "# fish completion for arduino-cli" in result.stdout assert "function __arduino_cli_perform_completion" in result.stdout assert "__completeNoDesc" in result.stdout + + +def test_completion_powershell_no_desc(run_command): + result = run_command("completion powershell --no-descriptions") + assert not result.ok + assert result.stdout == "" + assert "Error: command description is not supported by powershell" in result.stderr