Skip to content

Commit 82df01f

Browse files
authored
Fix zsh completion (#892)
* update cobra to get new zsh completion spf13/cobra@2c5a0d3 and to fix this spf13/cobra@675ae5f * add --no-descriptions also for zsh * update docs * update tests * removed hack spf13/cobra#1121
1 parent bd6c861 commit 82df01f

File tree

5 files changed

+28
-22
lines changed

5 files changed

+28
-22
lines changed

Diff for: cli/completion/completion.go

+8-10
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,7 @@
1616
package completion
1717

1818
import (
19-
"bytes"
2019
"os"
21-
"strings"
2220

2321
"github.com/arduino/arduino-cli/cli/errorcodes"
2422
"github.com/arduino/arduino-cli/cli/feedback"
@@ -29,7 +27,7 @@ var (
2927
completionNoDesc bool //Disable completion description for shells that support it
3028
)
3129

32-
// NewCommand created a new `version` command
30+
// NewCommand created a new `completion` command
3331
func NewCommand() *cobra.Command {
3432
command := &cobra.Command{
3533
Use: "completion [bash|zsh|fish] [--no-descriptions]",
@@ -47,7 +45,7 @@ func NewCommand() *cobra.Command {
4745
}
4846

4947
func run(cmd *cobra.Command, args []string) {
50-
if completionNoDesc && (args[0] == "bash" || args[0] == "zsh") {
48+
if completionNoDesc && (args[0] == "bash") {
5149
feedback.Errorf("Error: command description is not supported by %v", args[0])
5250
os.Exit(errorcodes.ErrGeneric)
5351
}
@@ -56,14 +54,14 @@ func run(cmd *cobra.Command, args []string) {
5654
cmd.Root().GenBashCompletion(os.Stdout)
5755
break
5856
case "zsh":
59-
cmd.Root().GenZshCompletion(os.Stdout)
57+
if completionNoDesc {
58+
cmd.Root().GenZshCompletionNoDesc(os.Stdout)
59+
} else {
60+
cmd.Root().GenZshCompletion(os.Stdout)
61+
}
6062
break
6163
case "fish":
62-
buf := new(bytes.Buffer)
63-
cmd.Root().GenFishCompletion(buf, !completionNoDesc)
64-
// Next 2 lines are Hack, fixed here https://github.com/spf13/cobra/pull/1122
65-
s := strings.ReplaceAll(buf.String(), "arduino-cli_comp", "arduino_cli_comp") //required because fish does not support env variables with "-" in the name
66-
os.Stdout.WriteString(s)
64+
cmd.Root().GenFishCompletion(os.Stdout, !completionNoDesc)
6765
break
6866
}
6967
}

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,11 @@ Remember to open a new shell to test the functionality.
4646

4747
#### Disabling command and flag descriptions
4848

49-
By default fish completion has command and flag description enabled by default. If you want to disable this behaviour
50-
you can simply pass the `--no-descriptions` flag when calling `completion` command and the generated file will not have
51-
descriptions
49+
By default fish and zsh completion have command and flag description enabled by default. If you want to disable this
50+
behaviour you can simply pass the `--no-descriptions` flag when calling `completion` command and the generated file will
51+
not have descriptions
5252

53-
_N.B._ This flag is not compatible with bash or zsh
53+
_N.B._ This flag is not compatible with bash
5454

5555
### Brew
5656

Diff for: go.mod

+2-2
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ require (
3838
github.com/schollz/closestmatch v2.1.0+incompatible
3939
github.com/segmentio/stats/v4 v4.5.3
4040
github.com/sirupsen/logrus v1.4.2
41-
github.com/spf13/cobra v1.0.0
41+
github.com/spf13/cobra v1.0.1-0.20200710201246-675ae5f5a98c
4242
github.com/spf13/jwalterweatherman v1.0.0
4343
github.com/spf13/viper v1.6.2
4444
github.com/stretchr/testify v1.6.1
@@ -55,5 +55,5 @@ require (
5555
google.golang.org/grpc v1.27.0
5656
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect
5757
gopkg.in/mgo.v2 v2.0.0-20180705113604-9856a29383ce // indirect
58-
gopkg.in/yaml.v2 v2.2.4
58+
gopkg.in/yaml.v2 v2.3.0
5959
)

Diff for: go.sum

+6
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,10 @@ github.com/spf13/cast v1.3.0 h1:oget//CVOEoFewqQxwr0Ej5yjygnqGkvggSE/gB35Q8=
199199
github.com/spf13/cast v1.3.0/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE=
200200
github.com/spf13/cobra v1.0.0 h1:6m/oheQuQ13N9ks4hubMG6BnvwOeaJrqSPLahSnczz8=
201201
github.com/spf13/cobra v1.0.0/go.mod h1:/6GTrnGXV9HjY+aR4k0oJ5tcvakLuG6EuKReYlHNrgE=
202+
github.com/spf13/cobra v1.0.1-0.20200629195214-2c5a0d300f8b h1:grM+VdcoRu+xbzmCXM1KuH5UQGk9Lc8yCiwZZ2PKVdU=
203+
github.com/spf13/cobra v1.0.1-0.20200629195214-2c5a0d300f8b/go.mod h1:/6GTrnGXV9HjY+aR4k0oJ5tcvakLuG6EuKReYlHNrgE=
204+
github.com/spf13/cobra v1.0.1-0.20200710201246-675ae5f5a98c h1:/dP/1GnfVIlWnB0YDImenSmneUCw3wjyq2RMgAG1e2o=
205+
github.com/spf13/cobra v1.0.1-0.20200710201246-675ae5f5a98c/go.mod h1:aeNIJzz/GSSVlS+gpCpQWZ83BKbsoW57mr90+YthtkQ=
202206
github.com/spf13/jwalterweatherman v1.0.0 h1:XHEdyB+EcvlqZamSM4ZOMGlc93t6AcsBEu9Gc1vn7yk=
203207
github.com/spf13/jwalterweatherman v1.0.0/go.mod h1:cQK4TGJAtQXfYWX+Ddv3mKDzgVb68N+wFjFa4jdeBTo=
204208
github.com/spf13/pflag v1.0.3 h1:zPAT6CGy6wXeQ7NtTnaTerfKOsV6V6F8agHXFiazDkg=
@@ -310,6 +314,8 @@ gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
310314
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
311315
gopkg.in/yaml.v2 v2.2.4 h1:/eiJrUcujPVeJ3xlSWaiNi3uSVmDGBK1pDHUHAnao1I=
312316
gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
317+
gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU=
318+
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
313319
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
314320
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
315321
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=

Diff for: test/test_completion.py

+8-6
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,15 @@ def test_completion_zsh(run_command):
3434
assert result.ok
3535
assert result.stderr == ""
3636
assert "#compdef _arduino-cli arduino-cli" in result.stdout
37-
assert "function _arduino-cli" in result.stdout
37+
assert "_arduino-cli()" in result.stdout
3838

3939

4040
def test_completion_fish(run_command):
4141
result = run_command("completion fish")
4242
assert result.ok
4343
assert result.stderr == ""
4444
assert "# fish completion for arduino-cli" in result.stdout
45-
assert "function __arduino-cli_perform_completion" in result.stdout
45+
assert "function __arduino_cli_perform_completion" in result.stdout
4646

4747

4848
def test_completion_bash_no_desc(run_command):
@@ -54,15 +54,17 @@ def test_completion_bash_no_desc(run_command):
5454

5555
def test_completion_zsh_no_desc(run_command):
5656
result = run_command("completion zsh --no-descriptions")
57-
assert not result.ok
58-
assert result.stdout == ""
59-
assert "Error: command description is not supported by zsh" in result.stderr
57+
assert result.ok
58+
assert result.stderr == ""
59+
assert "#compdef _arduino-cli arduino-cli" in result.stdout
60+
assert "_arduino-cli()" in result.stdout
61+
assert "__completeNoDesc" in result.stdout
6062

6163

6264
def test_completion_fish_no_desc(run_command):
6365
result = run_command("completion fish --no-descriptions")
6466
assert result.ok
6567
assert result.stderr == ""
6668
assert "# fish completion for arduino-cli" in result.stdout
67-
assert "function __arduino-cli_perform_completion" in result.stdout
69+
assert "function __arduino_cli_perform_completion" in result.stdout
6870
assert "__completeNoDesc" in result.stdout

0 commit comments

Comments
 (0)