Skip to content

Commit

Permalink
tmp
Browse files Browse the repository at this point in the history
  • Loading branch information
rsteube committed Sep 25, 2023
1 parent 6800db4 commit bfe4842
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 0 deletions.
4 changes: 4 additions & 0 deletions compat.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ func cobraDirectiveFor(action InvokedAction) cobra.ShellCompDirective {

func actionCobra(cmd *cobra.Command, f func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective)) Action {
return ActionCallback(func(c Context) Action {
if f == nil {
return ActionValues()
}

values, directive := f(cmd, c.Args, c.Value)
return compDirective(directive).ToA(values...)
})
Expand Down
11 changes: 11 additions & 0 deletions example/cmd/compat.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,15 @@ func init() {
compatCmd.RegisterFlagCompletionFunc("default", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return nil, cobra.ShellCompDirectiveDefault
})

compatCmd.ValidArgsFunction = func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
switch len(args) {
case 0:
return []string{"p1", "positional1"}, cobra.ShellCompDirectiveDefault
case 1:
return nil, cobra.ShellCompDirectiveDefault
default:
return nil, cobra.ShellCompDirectiveNoFileComp
}
}
}
4 changes: 4 additions & 0 deletions storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,12 +163,16 @@ func (s _storage) getPositional(cmd *cobra.Command, index int) Action {
case !isDash:
if entry.positionalAny != nil {
a = *entry.positionalAny
} else {
a = actionCobra(cmd, cmd.ValidArgsFunction)
}
case len(entry.dash) > index:
a = entry.dash[index]
default:
if entry.dashAny != nil {
a = *entry.dashAny
} else {
a = actionCobra(cmd, cmd.ValidArgsFunction)
}
}
a = s.preinvoke(cmd, nil, a)
Expand Down

0 comments on commit bfe4842

Please sign in to comment.