Fix InitDefaultHelpCmd when custom help command is set

This commit is contained in:
Albert Nigmatzianov 2017-06-29 12:52:34 +02:00
parent 4d647c8944
commit 8c6fa02d22
2 changed files with 52 additions and 23 deletions

View File

@ -767,17 +767,16 @@ func (c *Command) InitDefaultHelpFlag() {
// It is called automatically by executing the c or by calling help and usage. // It is called automatically by executing the c or by calling help and usage.
// If c already has help command or c has no subcommands, it will do nothing. // If c already has help command or c has no subcommands, it will do nothing.
func (c *Command) InitDefaultHelpCmd() { func (c *Command) InitDefaultHelpCmd() {
if c.helpCommand != nil || !c.HasSubCommands() { if !c.HasSubCommands() {
return return
} }
if c.helpCommand == nil {
c.helpCommand = &Command{ c.helpCommand = &Command{
Use: "help [command]", Use: "help [command]",
Short: "Help about any command", Short: "Help about any command",
Long: `Help provides help for any command in the application. Long: `Help provides help for any command in the application.
Simply type ` + c.Name() + ` help [path to command] for full details.`, Simply type ` + c.Name() + ` help [path to command] for full details.`,
PersistentPreRun: func(cmd *Command, args []string) {},
PersistentPostRun: func(cmd *Command, args []string) {},
Run: func(c *Command, args []string) { Run: func(c *Command, args []string) {
cmd, _, e := c.Root().Find(args) cmd, _, e := c.Root().Find(args)
@ -790,6 +789,7 @@ func (c *Command) InitDefaultHelpCmd() {
} }
}, },
} }
}
c.RemoveCommand(c.helpCommand) c.RemoveCommand(c.helpCommand)
c.AddCommand(c.helpCommand) c.AddCommand(c.helpCommand)
} }

View File

@ -120,7 +120,6 @@ func TestStripFlags(t *testing.T) {
} }
func TestDisableFlagParsing(t *testing.T) { func TestDisableFlagParsing(t *testing.T) {
as := []string{"-v", "-race", "-file", "foo.go"}
targs := []string{} targs := []string{}
cmdPrint := &Command{ cmdPrint := &Command{
DisableFlagParsing: true, DisableFlagParsing: true,
@ -128,14 +127,14 @@ func TestDisableFlagParsing(t *testing.T) {
targs = args targs = args
}, },
} }
osargs := []string{"cmd"} args := []string{"cmd", "-v", "-race", "-file", "foo.go"}
os.Args = append(osargs, as...) cmdPrint.SetArgs(args)
err := cmdPrint.Execute() err := cmdPrint.Execute()
if err != nil { if err != nil {
t.Error(err) t.Error(err)
} }
if !reflect.DeepEqual(as, targs) { if !reflect.DeepEqual(args, targs) {
t.Errorf("expected: %v, got: %v", as, targs) t.Errorf("expected: %v, got: %v", args, targs)
} }
} }
@ -316,5 +315,35 @@ func TestUseDeprecatedFlags(t *testing.T) {
if !strings.Contains(output.String(), "This flag is deprecated") { if !strings.Contains(output.String(), "This flag is deprecated") {
t.Errorf("Expected to contain deprecated message, but got %q", output.String()) t.Errorf("Expected to contain deprecated message, but got %q", output.String())
} }
}
// TestSetHelpCommand checks, if SetHelpCommand works correctly.
func TestSetHelpCommand(t *testing.T) {
c := &Command{Use: "c", Run: func(*Command, []string) {}}
output := new(bytes.Buffer)
c.SetOutput(output)
c.SetArgs([]string{"help"})
// Help will not be shown, if c has no subcommands.
c.AddCommand(&Command{
Use: "empty",
Run: func(cmd *Command, args []string) {},
})
correctMessage := "WORKS"
c.SetHelpCommand(&Command{
Use: "help [command]",
Short: "Help about any command",
Long: `Help provides help for any command in the application.
Simply type ` + c.Name() + ` help [path to command] for full details.`,
Run: func(c *Command, args []string) { c.Print(correctMessage) },
})
if err := c.Execute(); err != nil {
t.Error("Unexpected error:", err)
}
if output.String() != correctMessage {
t.Errorf("Expected to contain %q message, but got %q", correctMessage, output.String())
}
} }