2017-05-11 14:39:54 +00:00
|
|
|
package doc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
2018-01-16 13:20:59 +00:00
|
|
|
func emptyRun(*cobra.Command, []string) {}
|
2017-05-11 14:39:54 +00:00
|
|
|
|
2018-01-16 13:20:59 +00:00
|
|
|
func init() {
|
|
|
|
rootCmd.PersistentFlags().StringP("rootflag", "r", "two", "")
|
|
|
|
rootCmd.PersistentFlags().StringP("strtwo", "t", "two", "help message for parent flag strtwo")
|
2017-05-11 14:39:54 +00:00
|
|
|
|
2018-01-16 13:20:59 +00:00
|
|
|
echoCmd.PersistentFlags().StringP("strone", "s", "one", "help message for flag strone")
|
|
|
|
echoCmd.PersistentFlags().BoolP("persistentbool", "p", false, "help message for flag persistentbool")
|
|
|
|
echoCmd.Flags().IntP("intone", "i", 123, "help message for flag intone")
|
|
|
|
echoCmd.Flags().BoolP("boolone", "b", true, "help message for flag boolone")
|
|
|
|
|
|
|
|
timesCmd.PersistentFlags().StringP("strtwo", "t", "2", "help message for child flag strtwo")
|
|
|
|
timesCmd.Flags().IntP("inttwo", "j", 234, "help message for flag inttwo")
|
|
|
|
timesCmd.Flags().BoolP("booltwo", "c", false, "help message for flag booltwo")
|
|
|
|
|
|
|
|
printCmd.PersistentFlags().StringP("strthree", "s", "three", "help message for flag strthree")
|
|
|
|
printCmd.Flags().IntP("intthree", "i", 345, "help message for flag intthree")
|
|
|
|
printCmd.Flags().BoolP("boolthree", "b", true, "help message for flag boolthree")
|
|
|
|
|
|
|
|
echoCmd.AddCommand(timesCmd, echoSubCmd, deprecatedCmd)
|
|
|
|
rootCmd.AddCommand(printCmd, echoCmd)
|
|
|
|
}
|
|
|
|
|
|
|
|
var rootCmd = &cobra.Command{
|
|
|
|
Use: "root",
|
|
|
|
Short: "Root short description",
|
|
|
|
Long: "Root long description",
|
|
|
|
Run: emptyRun,
|
|
|
|
}
|
|
|
|
|
|
|
|
var echoCmd = &cobra.Command{
|
2017-05-11 14:39:54 +00:00
|
|
|
Use: "echo [string to echo]",
|
|
|
|
Aliases: []string{"say"},
|
|
|
|
Short: "Echo anything to the screen",
|
2018-01-16 13:20:59 +00:00
|
|
|
Long: "an utterly useless command for testing",
|
2017-05-11 14:39:54 +00:00
|
|
|
Example: "Just run cobra-test echo",
|
|
|
|
}
|
|
|
|
|
2018-01-16 13:20:59 +00:00
|
|
|
var echoSubCmd = &cobra.Command{
|
2017-05-11 14:39:54 +00:00
|
|
|
Use: "echosub [string to print]",
|
|
|
|
Short: "second sub command for echo",
|
2018-01-16 13:20:59 +00:00
|
|
|
Long: "an absolutely utterly useless command for testing gendocs!.",
|
|
|
|
Run: emptyRun,
|
|
|
|
}
|
|
|
|
|
|
|
|
var timesCmd = &cobra.Command{
|
|
|
|
Use: "times [# times] [string to echo]",
|
|
|
|
SuggestFor: []string{"counts"},
|
|
|
|
Short: "Echo anything to the screen more times",
|
|
|
|
Long: `a slightly useless command for testing.`,
|
|
|
|
Run: emptyRun,
|
2017-05-11 14:39:54 +00:00
|
|
|
}
|
|
|
|
|
2018-01-16 13:20:59 +00:00
|
|
|
var deprecatedCmd = &cobra.Command{
|
2017-05-11 14:39:54 +00:00
|
|
|
Use: "deprecated [can't do anything here]",
|
|
|
|
Short: "A command which is deprecated",
|
|
|
|
Long: `an absolutely utterly useless command for testing deprecation!.`,
|
|
|
|
Deprecated: "Please use echo instead",
|
|
|
|
}
|
|
|
|
|
2018-01-16 13:20:59 +00:00
|
|
|
var printCmd = &cobra.Command{
|
2017-05-11 14:39:54 +00:00
|
|
|
Use: "print [string to print]",
|
|
|
|
Short: "Print anything to the screen",
|
|
|
|
Long: `an absolutely utterly useless command for testing.`,
|
|
|
|
}
|
|
|
|
|
2018-01-16 13:20:59 +00:00
|
|
|
func checkStringContains(t *testing.T, got, expected string) {
|
|
|
|
if !strings.Contains(got, expected) {
|
|
|
|
t.Errorf("Expected to contain: \n %v\nGot:\n %v\n", expected, got)
|
2017-05-11 14:39:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-16 13:20:59 +00:00
|
|
|
func checkStringOmits(t *testing.T, got, expected string) {
|
|
|
|
if strings.Contains(got, expected) {
|
|
|
|
t.Errorf("Expected to not contain: \n %v\nGot: %v", expected, got)
|
2017-05-11 14:39:54 +00:00
|
|
|
}
|
|
|
|
}
|