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