MCPcopy
hub / github.com/spf13/cobra / TestGetFlagCompletion

Function TestGetFlagCompletion

completions_test.go:3647–3735  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

3645}
3646
3647func TestGetFlagCompletion(t *testing.T) {
3648 rootCmd := &Command{Use: "root", Run: emptyRun}
3649
3650 rootCmd.Flags().String("rootflag", "", "root flag")
3651 _ = rootCmd.RegisterFlagCompletionFunc("rootflag", func(cmd *Command, args []string, toComplete string) ([]string, ShellCompDirective) {
3652 return []string{"rootvalue"}, ShellCompDirectiveKeepOrder
3653 })
3654
3655 rootCmd.PersistentFlags().String("persistentflag", "", "persistent flag")
3656 _ = rootCmd.RegisterFlagCompletionFunc("persistentflag", func(cmd *Command, args []string, toComplete string) ([]string, ShellCompDirective) {
3657 return []string{"persistentvalue"}, ShellCompDirectiveDefault
3658 })
3659
3660 childCmd := &Command{Use: "child", Run: emptyRun}
3661
3662 childCmd.Flags().String("childflag", "", "child flag")
3663 _ = childCmd.RegisterFlagCompletionFunc("childflag", func(cmd *Command, args []string, toComplete string) ([]string, ShellCompDirective) {
3664 return []string{"childvalue"}, ShellCompDirectiveNoFileComp | ShellCompDirectiveNoSpace
3665 })
3666
3667 rootCmd.AddCommand(childCmd)
3668
3669 testcases := []struct {
3670 desc string
3671 cmd *Command
3672 flagName string
3673 exists bool
3674 comps []string
3675 directive ShellCompDirective
3676 }{
3677 {
3678 desc: "get flag completion function for command",
3679 cmd: rootCmd,
3680 flagName: "rootflag",
3681 exists: true,
3682 comps: []string{"rootvalue"},
3683 directive: ShellCompDirectiveKeepOrder,
3684 },
3685 {
3686 desc: "get persistent flag completion function for command",
3687 cmd: rootCmd,
3688 flagName: "persistentflag",
3689 exists: true,
3690 comps: []string{"persistentvalue"},
3691 directive: ShellCompDirectiveDefault,
3692 },
3693 {
3694 desc: "get flag completion function for child command",
3695 cmd: childCmd,
3696 flagName: "childflag",
3697 exists: true,
3698 comps: []string{"childvalue"},
3699 directive: ShellCompDirectiveNoFileComp | ShellCompDirectiveNoSpace,
3700 },
3701 {
3702 desc: "get persistent flag completion function for child command",
3703 cmd: childCmd,
3704 flagName: "persistentflag",

Callers

nothing calls this directly

Calls 6

FlagsMethod · 0.95
PersistentFlagsMethod · 0.95
AddCommandMethod · 0.95
StringMethod · 0.80
GetFlagCompletionFuncMethod · 0.80

Tested by

no test coverage detected