@@ -3427,3 +3427,93 @@ Completion ended with directive: ShellCompDirectiveNoFileComp
34273427 })
34283428 }
34293429}
3430+
3431+ func TestGetFlagCompletion (t * testing.T ) {
3432+ rootCmd := & Command {Use : "root" , Run : emptyRun }
3433+
3434+ rootCmd .Flags ().String ("rootflag" , "" , "root flag" )
3435+ _ = rootCmd .RegisterFlagCompletionFunc ("rootflag" , func (cmd * Command , args []string , toComplete string ) ([]string , ShellCompDirective ) {
3436+ return []string {"rootvalue" }, ShellCompDirectiveKeepOrder
3437+ })
3438+
3439+ rootCmd .PersistentFlags ().String ("persistentflag" , "" , "persistent flag" )
3440+ _ = rootCmd .RegisterFlagCompletionFunc ("persistentflag" , func (cmd * Command , args []string , toComplete string ) ([]string , ShellCompDirective ) {
3441+ return []string {"persistentvalue" }, ShellCompDirectiveDefault
3442+ })
3443+
3444+ childCmd := & Command {Use : "child" , Run : emptyRun }
3445+
3446+ childCmd .Flags ().String ("childflag" , "" , "child flag" )
3447+ _ = childCmd .RegisterFlagCompletionFunc ("childflag" , func (cmd * Command , args []string , toComplete string ) ([]string , ShellCompDirective ) {
3448+ return []string {"childvalue" }, ShellCompDirectiveNoFileComp | ShellCompDirectiveNoSpace
3449+ })
3450+
3451+ rootCmd .AddCommand (childCmd )
3452+
3453+ testcases := []struct {
3454+ desc string
3455+ cmd * Command
3456+ flagName string
3457+ exists bool
3458+ comps []string
3459+ directive ShellCompDirective
3460+ }{
3461+ {
3462+ desc : "get flag completion function for command" ,
3463+ cmd : rootCmd ,
3464+ flagName : "rootflag" ,
3465+ exists : true ,
3466+ comps : []string {"rootvalue" },
3467+ directive : ShellCompDirectiveKeepOrder ,
3468+ },
3469+ {
3470+ desc : "get persistent flag completion function for command" ,
3471+ cmd : rootCmd ,
3472+ flagName : "persistentflag" ,
3473+ exists : true ,
3474+ comps : []string {"persistentvalue" },
3475+ directive : ShellCompDirectiveDefault ,
3476+ },
3477+ {
3478+ desc : "get flag completion function for child command" ,
3479+ cmd : childCmd ,
3480+ flagName : "childflag" ,
3481+ exists : true ,
3482+ comps : []string {"childvalue" },
3483+ directive : ShellCompDirectiveNoFileComp | ShellCompDirectiveNoSpace ,
3484+ },
3485+ {
3486+ desc : "get persistent flag completion function for child command" ,
3487+ cmd : childCmd ,
3488+ flagName : "persistentflag" ,
3489+ exists : true ,
3490+ comps : []string {"persistentvalue" },
3491+ directive : ShellCompDirectiveDefault ,
3492+ },
3493+ {
3494+ desc : "cannot get flag completion function for local parent flag" ,
3495+ cmd : childCmd ,
3496+ flagName : "rootflag" ,
3497+ exists : false ,
3498+ },
3499+ }
3500+
3501+ for _ , tc := range testcases {
3502+ t .Run (tc .desc , func (t * testing.T ) {
3503+ compFunc , exists := tc .cmd .GetFlagCompletionFunc (tc .flagName )
3504+ if tc .exists != exists {
3505+ t .Errorf ("Unexpected result looking for flag completion function" )
3506+ }
3507+
3508+ if exists {
3509+ comps , directive := compFunc (tc .cmd , []string {}, "" )
3510+ if strings .Join (tc .comps , " " ) != strings .Join (comps , " " ) {
3511+ t .Errorf ("Unexpected completions %q" , comps )
3512+ }
3513+ if tc .directive != directive {
3514+ t .Errorf ("Unexpected directive %q" , directive )
3515+ }
3516+ }
3517+ })
3518+ }
3519+ }
0 commit comments