Skip to content

Commit 804b8d0

Browse files
Add completion for PowerShell (#1413)
* Add completion for PowerShell * Manage command description for PS completion * Add test for PS completion * Add PS documentation * Fix formatting * Fix python lint for PS tests * Update docs/command-line-completion.md Co-authored-by: Silvano Cerza <[email protected]> Co-authored-by: Silvano Cerza <[email protected]>
1 parent a07258b commit 804b8d0

File tree

3 files changed

+42
-8
lines changed

3 files changed

+42
-8
lines changed

Diff for: cli/completion/completion.go

+6-3
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ var (
3232
// NewCommand created a new `completion` command
3333
func NewCommand() *cobra.Command {
3434
command := &cobra.Command{
35-
Use: "completion [bash|zsh|fish] [--no-descriptions]",
36-
ValidArgs: []string{"bash", "zsh", "fish"},
35+
Use: "completion [bash|zsh|fish|powershell] [--no-descriptions]",
36+
ValidArgs: []string{"bash", "zsh", "fish", "powershell"},
3737
Args: cobra.ExactArgs(1),
3838
Short: tr("Generates completion scripts"),
3939
Long: tr("Generates completion scripts for various shells"),
@@ -47,7 +47,7 @@ func NewCommand() *cobra.Command {
4747
}
4848

4949
func run(cmd *cobra.Command, args []string) {
50-
if completionNoDesc && (args[0] == "bash") {
50+
if completionNoDesc && (args[0] == "bash" || args[0] == "powershell") {
5151
feedback.Errorf(tr("Error: command description is not supported by %v"), args[0])
5252
os.Exit(errorcodes.ErrGeneric)
5353
}
@@ -65,5 +65,8 @@ func run(cmd *cobra.Command, args []string) {
6565
case "fish":
6666
cmd.Root().GenFishCompletion(os.Stdout, !completionNoDesc)
6767
break
68+
case "powershell":
69+
cmd.Root().GenPowerShellCompletion(os.Stdout)
70+
break
6871
}
6972
}

Diff for: docs/command-line-completion.md

+21-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
`arduino-cli` supports command-line completion (also known as _tab completion_) for basic commands. Currently only
2-
`bash`, `zsh`, `fish` shells are supported
2+
`bash`, `zsh`, `fish`, and `powershell` shells are supported
33

44
### Before you start
55

@@ -8,9 +8,9 @@ first.
88

99
### Generate the completion file
1010

11-
To generate the completion file you can use `arduino-cli completion [bash|zsh|fish] [--no-descriptions]`. By default
12-
this command will print on the standard output (the shell window) the content of the completion file. To save to an
13-
actual file use the `>` redirect symbol.
11+
To generate the completion file you can use `arduino-cli completion [bash|zsh|fish|powershell] [--no-descriptions]`. By
12+
default this command will print on the standard output (the shell window) the content of the completion file. To save to
13+
an actual file use the `>` redirect symbol.
1414

1515
### Bash
1616

@@ -44,13 +44,29 @@ with `mv arduino-cli.fish ~/.config/fish/completions/`
4444

4545
Remember to open a new shell to test the functionality.
4646

47+
### Powershell
48+
49+
Use `arduino-cli completion powershell > arduino-cli.ps1` to generate a temporary completion file. At this point you
50+
need to add the content of the generated file to your PowerShell profile file.
51+
52+
1. `Get-Content -Path arduino-cli.ps1 | Add-Content -Path $profile` or add it by hand with your favourite text editor.
53+
1. The previous command added two `using namespace` lines, move them on top of the `$profile` file.
54+
1. If not already done, add the line `Set-PSReadlineKeyHandler -Key Tab -Function MenuComplete` to your `$profile` file:
55+
it is needed to enable the TAB completion in PowerShell.
56+
1. `del arduino-cli.ps1` to remove the temporary file.
57+
58+
Remember to open a new shell to test the functionality.
59+
60+
For more information on tab-completion on PowerShell, please, refer to
61+
[Autocomplete in PowerShell](https://techcommunity.microsoft.com/t5/itops-talk-blog/autocomplete-in-powershell/ba-p/2604524).
62+
4763
#### Disabling command and flag descriptions
4864

4965
By default fish and zsh completion have command and flag description enabled by default. If you want to disable this
5066
behaviour you can simply pass the `--no-descriptions` flag when calling `completion` command and the generated file will
5167
not have descriptions
5268

53-
_N.B._ This flag is not compatible with bash
69+
_N.B._ This flag is not compatible with bash nor powershell
5470

5571
### Brew
5672

Diff for: test/test_completion.py

+15
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,14 @@ def test_completion_fish(run_command):
4545
assert "function __arduino_cli_perform_completion" in result.stdout
4646

4747

48+
def test_completion_powershell(run_command):
49+
result = run_command("completion powershell")
50+
assert result.ok
51+
assert result.stderr == ""
52+
assert "Register-ArgumentCompleter -Native -CommandName 'arduino-cli' -ScriptBlock {" in result.stdout
53+
assert "'arduino-cli;completion' {" in result.stdout
54+
55+
4856
def test_completion_bash_no_desc(run_command):
4957
result = run_command("completion bash --no-descriptions")
5058
assert not result.ok
@@ -68,3 +76,10 @@ def test_completion_fish_no_desc(run_command):
6876
assert "# fish completion for arduino-cli" in result.stdout
6977
assert "function __arduino_cli_perform_completion" in result.stdout
7078
assert "__completeNoDesc" in result.stdout
79+
80+
81+
def test_completion_powershell_no_desc(run_command):
82+
result = run_command("completion powershell --no-descriptions")
83+
assert not result.ok
84+
assert result.stdout == ""
85+
assert "Error: command description is not supported by powershell" in result.stderr

0 commit comments

Comments
 (0)