From 2c45208f97ce2d90d79140aaae3ce730d82b8887 Mon Sep 17 00:00:00 2001 From: rsteube Date: Sun, 29 Jan 2023 21:37:38 +0100 Subject: [PATCH] nargs: keep repeatable behaviour - parts only consists of args related to current occurrence --- example-nonposix/cmd/root.go | 2 +- internal/pflagfork/flag.go | 15 --------------- internalActions.go | 5 ----- traverse.go | 2 +- 4 files changed, 2 insertions(+), 22 deletions(-) diff --git a/example-nonposix/cmd/root.go b/example-nonposix/cmd/root.go index 264942696..b75b9d992 100644 --- a/example-nonposix/cmd/root.go +++ b/example-nonposix/cmd/root.go @@ -43,7 +43,7 @@ func init() { "delim-colon": carapace.ActionValues("d1", "d2", "d3"), "delim-slash": carapace.ActionValues("d1", "d2", "d3"), "nargs-any": carapace.ActionCallback(func(c carapace.Context) carapace.Action { - return carapace.ActionValues("na1", "na2", "na3").Invoke(c).Filter(c.Parts).ToA() + return carapace.ActionValues("na1", "na2", "na3").Invoke(c).Filter(c.Parts).ToA() // only filters current occurrence }), "nargs-two": carapace.ActionCallback(func(c carapace.Context) carapace.Action { switch len(c.Parts) { diff --git a/internal/pflagfork/flag.go b/internal/pflagfork/flag.go index ceee72c39..0b215f106 100644 --- a/internal/pflagfork/flag.go +++ b/internal/pflagfork/flag.go @@ -28,21 +28,6 @@ func (f Flag) Nargs() int { return 0 } -func (f Flag) Parts() []string { - if t := f.Value.Type(); !strings.HasSuffix(t, "Array") && !strings.HasSuffix(t, "Slice") { - return []string{} - } - - toParse := f.Value.String() - toParse = strings.TrimPrefix(toParse, "[") - toParse = strings.TrimSuffix(toParse, "]") - if parts, err := readAsCSV(toParse); err == nil { - // TODO fails with StringArray (works with StringSlice) - return parts - } - return []string{} -} - func readAsCSV(val string) ([]string, error) { if val == "" { return []string{}, nil diff --git a/internalActions.go b/internalActions.go index 7f1c2fec4..15bedae07 100644 --- a/internalActions.go +++ b/internalActions.go @@ -87,11 +87,6 @@ func actionFlags(cmd *cobra.Command) Action { return // skip flag of group already set } - if f.Changed && f.Nargs() > 0 && len(f.Parts()) >= f.Nargs() { // TODO optimize - // TODO only works with StringSlice at the moment - return // don't repeat nargs flag that has consumed all its required arguments - } - s := style.Carapace.FlagNoArg if f.TakesValue() { if f.IsOptarg() { diff --git a/traverse.go b/traverse.go index d275f22cb..4e0278056 100644 --- a/traverse.go +++ b/traverse.go @@ -152,7 +152,7 @@ loop: // flag argument case inFlag != nil && inFlag.Consumes(context.CallbackValue): logger.Printf("completing flag argument of %#v for arg %#v\n", inFlag.Name, context.CallbackValue) - context.Parts = inFlag.Parts() // TODO check for various flag types (bool,int,...) + context.Parts = inFlag.Args return storage.getFlag(c, inFlag.Name), context // flag