Skip to content

Commit

Permalink
Merge pull request #750 from rsteube/flag-bool-optarg
Browse files Browse the repository at this point in the history
flag bool optarg
  • Loading branch information
rsteube authored Apr 2, 2023
2 parents 97b4f77 + e5bcd6c commit bb21a1d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
6 changes: 6 additions & 0 deletions example/cmd/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,5 +128,11 @@ func TestRoot(t *testing.T) {
Expect(carapace.ActionStyledValuesDescribed(
"--array", "multiflag", style.Blue,
).NoSpace('.').Tag("flags"))

s.Run("--toggle=").
Expect(carapace.ActionStyledValues(
"false", style.Red,
"true", style.Green,
).Prefix("--toggle="))
})
}
9 changes: 8 additions & 1 deletion traverse.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/rsteube/carapace/internal/common"
"github.com/rsteube/carapace/internal/config"
"github.com/rsteube/carapace/internal/pflagfork"
"github.com/rsteube/carapace/pkg/style"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -161,7 +162,13 @@ loop:
LOG.Printf("completing optional flag argument for arg %#v\n", context.CallbackValue)
prefix, optarg := f.Split(context.CallbackValue)
context.CallbackValue = optarg
return storage.getFlag(c, f.Name).Prefix(prefix), context

switch f.Value.Type() {
case "bool":
return ActionValues("true", "false").StyleF(style.ForKeyword).Prefix(prefix), context
default:
return storage.getFlag(c, f.Name).Prefix(prefix), context
}
}
LOG.Printf("completing flags for arg %#v\n", context.CallbackValue)
return actionFlags(c), context
Expand Down

0 comments on commit bb21a1d

Please sign in to comment.