diff --git a/compat.go b/compat.go index c83a2861c..a9086b65a 100644 --- a/compat.go +++ b/compat.go @@ -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...) }) diff --git a/example/cmd/compat.go b/example/cmd/compat.go index 837dd5834..9cc853f25 100644 --- a/example/cmd/compat.go +++ b/example/cmd/compat.go @@ -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 + } + } } diff --git a/storage.go b/storage.go index 3b9687a92..00b2be74d 100644 --- a/storage.go +++ b/storage.go @@ -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)