Skip to content

Commit 1b2301f

Browse files
chore: hide flag completions by default (#19)
1 parent d46fb20 commit 1b2301f

File tree

2 files changed

+25
-18
lines changed

2 files changed

+25
-18
lines changed

completion.go

+17-10
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package serpent
22

33
import (
4+
"strings"
5+
46
"github.com/spf13/pflag"
57
)
68

@@ -14,20 +16,25 @@ func (inv *Invocation) IsCompletionMode() bool {
1416
return ok
1517
}
1618

17-
// DefaultCompletionHandler is a handler that prints all known flags and
18-
// subcommands that haven't been exhaustively set.
19+
// DefaultCompletionHandler is a handler that prints all the subcommands, or
20+
// all the options that haven't been exhaustively set, if the current word
21+
// starts with a dash.
1922
func DefaultCompletionHandler(inv *Invocation) []string {
23+
_, cur := inv.CurWords()
2024
var allResps []string
25+
if strings.HasPrefix(cur, "-") {
26+
for _, opt := range inv.Command.Options {
27+
_, isSlice := opt.Value.(pflag.SliceValue)
28+
if opt.ValueSource == ValueSourceNone ||
29+
opt.ValueSource == ValueSourceDefault ||
30+
isSlice {
31+
allResps = append(allResps, "--"+opt.Flag)
32+
}
33+
}
34+
return allResps
35+
}
2136
for _, cmd := range inv.Command.Children {
2237
allResps = append(allResps, cmd.Name())
2338
}
24-
for _, opt := range inv.Command.Options {
25-
_, isSlice := opt.Value.(pflag.SliceValue)
26-
if opt.ValueSource == ValueSourceNone ||
27-
opt.ValueSource == ValueSourceDefault ||
28-
isSlice {
29-
allResps = append(allResps, "--"+opt.Flag)
30-
}
31-
}
3239
return allResps
3340
}

completion_test.go

+8-8
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ func TestCompletion(t *testing.T) {
2525
io := fakeIO(i)
2626
err := i.Run()
2727
require.NoError(t, err)
28-
require.Equal(t, "altfile\nfile\nrequired-flag\ntoupper\n--prefix\n--verbose\n", io.Stdout.String())
28+
require.Equal(t, "altfile\nfile\nrequired-flag\ntoupper\n", io.Stdout.String())
2929
})
3030

3131
t.Run("SubcommandNoPartial", func(t *testing.T) {
@@ -35,7 +35,7 @@ func TestCompletion(t *testing.T) {
3535
io := fakeIO(i)
3636
err := i.Run()
3737
require.NoError(t, err)
38-
require.Equal(t, "altfile\nfile\nrequired-flag\ntoupper\n--prefix\n--verbose\n", io.Stdout.String())
38+
require.Equal(t, "altfile\nfile\nrequired-flag\ntoupper\n", io.Stdout.String())
3939
})
4040

4141
t.Run("SubcommandComplete", func(t *testing.T) {
@@ -50,7 +50,7 @@ func TestCompletion(t *testing.T) {
5050

5151
t.Run("ListFlags", func(t *testing.T) {
5252
t.Parallel()
53-
i := cmd().Invoke("required-flag", "")
53+
i := cmd().Invoke("required-flag", "-")
5454
i.Environ.Set(serpent.CompletionModeEnv, "1")
5555
io := fakeIO(i)
5656
err := i.Run()
@@ -60,7 +60,7 @@ func TestCompletion(t *testing.T) {
6060

6161
t.Run("ListFlagsAfterArg", func(t *testing.T) {
6262
t.Parallel()
63-
i := cmd().Invoke("altfile", "")
63+
i := cmd().Invoke("altfile", "-")
6464
i.Environ.Set(serpent.CompletionModeEnv, "1")
6565
io := fakeIO(i)
6666
err := i.Run()
@@ -70,7 +70,7 @@ func TestCompletion(t *testing.T) {
7070

7171
t.Run("FlagExhaustive", func(t *testing.T) {
7272
t.Parallel()
73-
i := cmd().Invoke("required-flag", "--req-bool", "--req-string", "foo bar", "--req-array", "asdf", "--req-array", "qwerty")
73+
i := cmd().Invoke("required-flag", "--req-bool", "--req-string", "foo bar", "--req-array", "asdf", "--req-array", "qwerty", "-")
7474
i.Environ.Set(serpent.CompletionModeEnv, "1")
7575
io := fakeIO(i)
7676
err := i.Run()
@@ -80,7 +80,7 @@ func TestCompletion(t *testing.T) {
8080

8181
t.Run("FlagShorthand", func(t *testing.T) {
8282
t.Parallel()
83-
i := cmd().Invoke("required-flag", "-b", "-s", "foo bar", "-a", "asdf")
83+
i := cmd().Invoke("required-flag", "-b", "-s", "foo bar", "-a", "asdf", "-")
8484
i.Environ.Set(serpent.CompletionModeEnv, "1")
8585
io := fakeIO(i)
8686
err := i.Run()
@@ -90,12 +90,12 @@ func TestCompletion(t *testing.T) {
9090

9191
t.Run("NoOptDefValueFlag", func(t *testing.T) {
9292
t.Parallel()
93-
i := cmd().Invoke("--verbose", "")
93+
i := cmd().Invoke("--verbose", "-")
9494
i.Environ.Set(serpent.CompletionModeEnv, "1")
9595
io := fakeIO(i)
9696
err := i.Run()
9797
require.NoError(t, err)
98-
require.Equal(t, "altfile\nfile\nrequired-flag\ntoupper\n--prefix\n", io.Stdout.String())
98+
require.Equal(t, "--prefix\n", io.Stdout.String())
9999
})
100100

101101
t.Run("EnumOK", func(t *testing.T) {

0 commit comments

Comments
 (0)