mirror of https://github.com/spf13/cobra.git
Merge pull request #88 from eparis/subhelptest
Add tests about additional help topics
This commit is contained in:
commit
b78326bb16
|
@ -72,6 +72,12 @@ var cmdRootWithRun = &Command{
|
|||
},
|
||||
}
|
||||
|
||||
var cmdSubNoRun = &Command{
|
||||
Use: "subnorun",
|
||||
Short: "A subcommand without a Run function",
|
||||
Long: "A long output about a subcommand without a Run function",
|
||||
}
|
||||
|
||||
var cmdVersion1 = &Command{
|
||||
Use: "version",
|
||||
Short: "Print the version number",
|
||||
|
@ -97,6 +103,7 @@ func flagInit() {
|
|||
cmdRootNoRun.ResetFlags()
|
||||
cmdRootSameName.ResetFlags()
|
||||
cmdRootWithRun.ResetFlags()
|
||||
cmdSubNoRun.ResetFlags()
|
||||
cmdRootNoRun.PersistentFlags().StringVarP(&flags2a, "strtwo", "t", "two", strtwoParentHelp)
|
||||
cmdEcho.Flags().IntVarP(&flagi1, "intone", "i", 123, "help message for flag intone")
|
||||
cmdTimes.Flags().IntVarP(&flagi2, "inttwo", "j", 234, "help message for flag inttwo")
|
||||
|
@ -119,6 +126,7 @@ func commandInit() {
|
|||
cmdRootNoRun.ResetCommands()
|
||||
cmdRootSameName.ResetCommands()
|
||||
cmdRootWithRun.ResetCommands()
|
||||
cmdSubNoRun.ResetCommands()
|
||||
}
|
||||
|
||||
func initialize() *Command {
|
||||
|
@ -188,7 +196,7 @@ func fullTester(c *Command, input string) resulter {
|
|||
// Testing flag with invalid input
|
||||
c.SetOutput(buf)
|
||||
cmdEcho.AddCommand(cmdTimes)
|
||||
c.AddCommand(cmdPrint, cmdEcho)
|
||||
c.AddCommand(cmdPrint, cmdEcho, cmdSubNoRun)
|
||||
c.SetArgs(strings.Split(input, " "))
|
||||
|
||||
err := c.Execute()
|
||||
|
@ -500,11 +508,14 @@ func TestPersistentFlags(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestHelpCommand(t *testing.T) {
|
||||
c := fullSetupTest("help echo")
|
||||
checkResultContains(t, c, cmdEcho.Long)
|
||||
x := fullSetupTest("help")
|
||||
checkResultContains(t, x, cmdRootWithRun.Long)
|
||||
|
||||
r := fullSetupTest("help echo times")
|
||||
checkResultContains(t, r, cmdTimes.Long)
|
||||
x = fullSetupTest("help echo")
|
||||
checkResultContains(t, x, cmdEcho.Long)
|
||||
|
||||
x = fullSetupTest("help echo times")
|
||||
checkResultContains(t, x, cmdTimes.Long)
|
||||
}
|
||||
|
||||
func TestChildCommandHelp(t *testing.T) {
|
||||
|
@ -514,6 +525,11 @@ func TestChildCommandHelp(t *testing.T) {
|
|||
checkResultContains(t, r, strtwoChildHelp)
|
||||
}
|
||||
|
||||
func TestNonRunChildHelp(t *testing.T) {
|
||||
x := noRRSetupTest("subnorun")
|
||||
checkResultContains(t, x, cmdSubNoRun.Long)
|
||||
}
|
||||
|
||||
func TestRunnableRootCommand(t *testing.T) {
|
||||
fullSetupTest("")
|
||||
|
||||
|
|
|
@ -234,7 +234,8 @@ Available Commands: {{range .Commands}}{{if .Runnable}}
|
|||
{{.LocalFlags.FlagUsages}}{{end}}
|
||||
{{ if .HasInheritedFlags}}Global Flags:
|
||||
{{.InheritedFlags.FlagUsages}}{{end}}{{if or (.HasHelpSubCommands) (.HasRunnableSiblings)}}
|
||||
Additional help topics: {{if .HasHelpSubCommands}}{{range .Commands}}{{if not .Runnable}} {{rpad .CommandPath .CommandPathPadding}} {{.Short}}{{end}}{{end}}{{end}}{{if .HasRunnableSiblings }}{{range .Parent.Commands}}{{if .Runnable}}{{if not (eq .Name $cmd.Name) }}
|
||||
Additional help topics:
|
||||
{{if .HasHelpSubCommands}}{{range .Commands}}{{if not .Runnable}} {{rpad .CommandPath .CommandPathPadding}} {{.Short}}{{end}}{{end}}{{end}}{{if .HasRunnableSiblings }}{{range .Parent.Commands}}{{if .Runnable}}{{if not (eq .Name $cmd.Name) }}
|
||||
{{rpad .CommandPath .CommandPathPadding}} {{.Short}}{{end}}{{end}}{{end}}{{end}}
|
||||
{{end}}{{ if .HasSubCommands }}
|
||||
Use "{{.Root.Name}} help [command]" for more information about a command.
|
||||
|
|
Loading…
Reference in New Issue