Skip to content
New issue

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

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

Already on GitHub? # to your account

chore: hide flag completions by default #19

Merged
merged 2 commits into from
Aug 22, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
29 changes: 18 additions & 11 deletions completion.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package serpent

import (
"strings"

"github.com/spf13/pflag"
)

Expand All @@ -14,19 +16,24 @@ func (inv *Invocation) IsCompletionMode() bool {
return ok
}

// DefaultCompletionHandler is a handler that prints all known flags and
// subcommands that haven't been exhaustively set.
// DefaultCompletionHandler is a handler that prints all the subcommands, or
// all the options that haven't been exhaustively set, if the current word
// starts with a dash.
func DefaultCompletionHandler(inv *Invocation) []string {
_, cur := inv.CurWords()
var allResps []string
for _, cmd := range inv.Command.Children {
allResps = append(allResps, cmd.Name())
}
for _, opt := range inv.Command.Options {
_, isSlice := opt.Value.(pflag.SliceValue)
if opt.ValueSource == ValueSourceNone ||
opt.ValueSource == ValueSourceDefault ||
isSlice {
allResps = append(allResps, "--"+opt.Flag)
if strings.HasPrefix(cur, "-") {
for _, opt := range inv.Command.Options {
_, isSlice := opt.Value.(pflag.SliceValue)
if opt.ValueSource == ValueSourceNone ||
opt.ValueSource == ValueSourceDefault ||
isSlice {
allResps = append(allResps, "--"+opt.Flag)
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Try to minimize indentation — use an early return instead of the else block

} else {
for _, cmd := range inv.Command.Children {
allResps = append(allResps, cmd.Name())
}
}
return allResps
Expand Down
16 changes: 8 additions & 8 deletions completion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func TestCompletion(t *testing.T) {
io := fakeIO(i)
err := i.Run()
require.NoError(t, err)
require.Equal(t, "altfile\nfile\nrequired-flag\ntoupper\n--prefix\n--verbose\n", io.Stdout.String())
require.Equal(t, "altfile\nfile\nrequired-flag\ntoupper\n", io.Stdout.String())
})

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

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

t.Run("ListFlags", func(t *testing.T) {
t.Parallel()
i := cmd().Invoke("required-flag", "")
i := cmd().Invoke("required-flag", "-")
i.Environ.Set(serpent.CompletionModeEnv, "1")
io := fakeIO(i)
err := i.Run()
Expand All @@ -60,7 +60,7 @@ func TestCompletion(t *testing.T) {

t.Run("ListFlagsAfterArg", func(t *testing.T) {
t.Parallel()
i := cmd().Invoke("altfile", "")
i := cmd().Invoke("altfile", "-")
i.Environ.Set(serpent.CompletionModeEnv, "1")
io := fakeIO(i)
err := i.Run()
Expand All @@ -70,7 +70,7 @@ func TestCompletion(t *testing.T) {

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

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

t.Run("NoOptDefValueFlag", func(t *testing.T) {
t.Parallel()
i := cmd().Invoke("--verbose", "")
i := cmd().Invoke("--verbose", "-")
i.Environ.Set(serpent.CompletionModeEnv, "1")
io := fakeIO(i)
err := i.Run()
require.NoError(t, err)
require.Equal(t, "altfile\nfile\nrequired-flag\ntoupper\n--prefix\n", io.Stdout.String())
require.Equal(t, "--prefix\n", io.Stdout.String())
})

t.Run("EnumOK", func(t *testing.T) {
Expand Down
Loading