10 "github.com/spf13/cobra"
13 var flagb1, flagb2, flagb3, flagbr, flagbp bool
14 var flags1, flags2a, flags2b, flags3 string
15 var flagi1, flagi2, flagi3, flagir int
17 const strtwoParentHelp = "help message for parent flag strtwo"
18 const strtwoChildHelp = "help message for child flag strtwo"
20 var cmdEcho = &cobra.Command{
21 Use: "echo [string to echo]",
22 Aliases: []string{"say"},
23 Short: "Echo anything to the screen",
24 Long: `an utterly useless command for testing.`,
25 Example: "Just run cobra-test echo",
28 var cmdEchoSub = &cobra.Command{
29 Use: "echosub [string to print]",
30 Short: "second sub command for echo",
31 Long: `an absolutely utterly useless command for testing gendocs!.`,
32 Run: func(cmd *cobra.Command, args []string) {},
35 var cmdDeprecated = &cobra.Command{
36 Use: "deprecated [can't do anything here]",
37 Short: "A command which is deprecated",
38 Long: `an absolutely utterly useless command for testing deprecation!.`,
39 Deprecated: "Please use echo instead",
42 var cmdTimes = &cobra.Command{
43 Use: "times [# times] [string to echo]",
44 SuggestFor: []string{"counts"},
45 Short: "Echo anything to the screen more times",
46 Long: `a slightly useless command for testing.`,
47 PersistentPreRun: func(cmd *cobra.Command, args []string) {},
48 Run: func(cmd *cobra.Command, args []string) {},
51 var cmdPrint = &cobra.Command{
52 Use: "print [string to print]",
53 Short: "Print anything to the screen",
54 Long: `an absolutely utterly useless command for testing.`,
57 var cmdRootNoRun = &cobra.Command{
59 Short: "The root can run its own function",
60 Long: "The root description for help",
63 var cmdRootSameName = &cobra.Command{
65 Short: "Root with the same name as a subcommand",
66 Long: "The root description for help",
69 var cmdRootWithRun = &cobra.Command{
71 Short: "The root can run its own function",
72 Long: "The root description for help",
75 var cmdSubNoRun = &cobra.Command{
77 Short: "A subcommand without a Run function",
78 Long: "A long output about a subcommand without a Run function",
81 var cmdVersion1 = &cobra.Command{
83 Short: "Print the version number",
84 Long: `First version of the version command`,
87 var cmdVersion2 = &cobra.Command{
89 Short: "Print the version number",
90 Long: `Second version of the version command`,
97 cmdRootNoRun.ResetFlags()
98 cmdRootSameName.ResetFlags()
99 cmdRootWithRun.ResetFlags()
100 cmdSubNoRun.ResetFlags()
101 cmdRootNoRun.PersistentFlags().StringVarP(&flags2a, "strtwo", "t", "two", strtwoParentHelp)
102 cmdEcho.Flags().IntVarP(&flagi1, "intone", "i", 123, "help message for flag intone")
103 cmdTimes.Flags().IntVarP(&flagi2, "inttwo", "j", 234, "help message for flag inttwo")
104 cmdPrint.Flags().IntVarP(&flagi3, "intthree", "i", 345, "help message for flag intthree")
105 cmdEcho.PersistentFlags().StringVarP(&flags1, "strone", "s", "one", "help message for flag strone")
106 cmdEcho.PersistentFlags().BoolVarP(&flagbp, "persistentbool", "p", false, "help message for flag persistentbool")
107 cmdTimes.PersistentFlags().StringVarP(&flags2b, "strtwo", "t", "2", strtwoChildHelp)
108 cmdPrint.PersistentFlags().StringVarP(&flags3, "strthree", "s", "three", "help message for flag strthree")
109 cmdEcho.Flags().BoolVarP(&flagb1, "boolone", "b", true, "help message for flag boolone")
110 cmdTimes.Flags().BoolVarP(&flagb2, "booltwo", "c", false, "help message for flag booltwo")
111 cmdPrint.Flags().BoolVarP(&flagb3, "boolthree", "b", true, "help message for flag boolthree")
112 cmdVersion1.ResetFlags()
113 cmdVersion2.ResetFlags()
116 func initializeWithRootCmd() *cobra.Command {
117 cmdRootWithRun.ResetCommands()
119 cmdRootWithRun.Flags().BoolVarP(&flagbr, "boolroot", "b", false, "help message for flag boolroot")
120 cmdRootWithRun.Flags().IntVarP(&flagir, "introot", "i", 321, "help message for flag introot")
121 return cmdRootWithRun
124 func checkStringContains(t *testing.T, found, expected string) {
125 if !strings.Contains(found, expected) {
126 logErr(t, found, expected)
130 func checkStringOmits(t *testing.T, found, expected string) {
131 if strings.Contains(found, expected) {
132 logErr(t, found, expected)
136 func logErr(t *testing.T, found, expected string) {
137 out := new(bytes.Buffer)
139 _, _, line, ok := runtime.Caller(2)
141 fmt.Fprintf(out, "Line: %d ", line)
143 fmt.Fprintf(out, "Unexpected response.\nExpecting to contain: \n %q\nGot:\n %q\n", expected, found)
144 t.Errorf(out.String())