Skip to content

Add completion for PowerShell #1413

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Aug 26, 2021
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions cli/completion/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand All @@ -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)
}
Expand All @@ -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
}
}
22 changes: 19 additions & 3 deletions docs/command-line-completion.md
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -8,7 +8,7 @@ first.

### Generate the completion file

To generate the completion file you can use `arduino-cli completion [bash|zsh|fish] [--no-descriptions]`. By default
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.

Expand Down Expand Up @@ -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. Move to first two "`using ...`" lines of `arduino-cli.ps1` on the 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

Expand Down
13 changes: 13 additions & 0 deletions test/test_completion.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@ def test_completion_fish(run_command):
assert "# fish completion for arduino-cli" in result.stdout
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")
Expand All @@ -68,3 +75,9 @@ 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