From e5bcd6cde351e21a89fb9b2d1abca76d1a26f9ae Mon Sep 17 00:00:00 2001 From: rsteube Date: Sun, 2 Apr 2023 09:45:12 +0200 Subject: [PATCH] flags: support optarg completion for bool flags as well - keeping the style though (flag without argument) --- example/cmd/root_test.go | 6 ++++++ traverse.go | 9 ++++++++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/example/cmd/root_test.go b/example/cmd/root_test.go index 8942a6d30..a94f42b5d 100644 --- a/example/cmd/root_test.go +++ b/example/cmd/root_test.go @@ -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=")) }) } diff --git a/traverse.go b/traverse.go index faaa1387b..beb9970e3 100644 --- a/traverse.go +++ b/traverse.go @@ -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" ) @@ -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