Skip to content

Commit

Permalink
nargs: keep repeatable behaviour
Browse files Browse the repository at this point in the history
- parts only consists of args related to current occurrence
  • Loading branch information
rsteube committed Jan 29, 2023
1 parent 6d28267 commit 2c45208
Show file tree
Hide file tree
Showing 4 changed files with 2 additions and 22 deletions.
2 changes: 1 addition & 1 deletion example-nonposix/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
15 changes: 0 additions & 15 deletions internal/pflagfork/flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 0 additions & 5 deletions internalActions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
2 changes: 1 addition & 1 deletion traverse.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 2c45208

Please sign in to comment.