From 8c6fa02d2225de0f9bdcb7ca912556f68d172d8c Mon Sep 17 00:00:00 2001 From: Albert Nigmatzianov Date: Thu, 29 Jun 2017 12:52:34 +0200 Subject: [PATCH] Fix InitDefaultHelpCmd when custom help command is set --- command.go | 34 +++++++++++++++++----------------- command_test.go | 41 +++++++++++++++++++++++++++++++++++------ 2 files changed, 52 insertions(+), 23 deletions(-) diff --git a/command.go b/command.go index 2cd6ee80..0ae8be21 100644 --- a/command.go +++ b/command.go @@ -767,28 +767,28 @@ func (c *Command) InitDefaultHelpFlag() { // 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. func (c *Command) InitDefaultHelpCmd() { - if c.helpCommand != nil || !c.HasSubCommands() { + if !c.HasSubCommands() { return } - c.helpCommand = &Command{ - Use: "help [command]", - Short: "Help about any command", - Long: `Help provides help for any command in the application. + if c.helpCommand == nil { + c.helpCommand = &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.`, - PersistentPreRun: func(cmd *Command, args []string) {}, - PersistentPostRun: func(cmd *Command, args []string) {}, - Run: func(c *Command, args []string) { - cmd, _, e := c.Root().Find(args) - if cmd == nil || e != nil { - c.Printf("Unknown help topic %#q\n", args) - c.Root().Usage() - } else { - cmd.InitDefaultHelpFlag() // make possible 'help' flag to be shown - cmd.Help() - } - }, + Run: func(c *Command, args []string) { + cmd, _, e := c.Root().Find(args) + if cmd == nil || e != nil { + c.Printf("Unknown help topic %#q\n", args) + c.Root().Usage() + } else { + cmd.InitDefaultHelpFlag() // make possible 'help' flag to be shown + cmd.Help() + } + }, + } } c.RemoveCommand(c.helpCommand) c.AddCommand(c.helpCommand) diff --git a/command_test.go b/command_test.go index f4fe1464..aa6658f8 100644 --- a/command_test.go +++ b/command_test.go @@ -120,7 +120,6 @@ func TestStripFlags(t *testing.T) { } func TestDisableFlagParsing(t *testing.T) { - as := []string{"-v", "-race", "-file", "foo.go"} targs := []string{} cmdPrint := &Command{ DisableFlagParsing: true, @@ -128,14 +127,14 @@ func TestDisableFlagParsing(t *testing.T) { targs = args }, } - osargs := []string{"cmd"} - os.Args = append(osargs, as...) + args := []string{"cmd", "-v", "-race", "-file", "foo.go"} + cmdPrint.SetArgs(args) err := cmdPrint.Execute() if err != nil { t.Error(err) } - if !reflect.DeepEqual(as, targs) { - t.Errorf("expected: %v, got: %v", as, targs) + if !reflect.DeepEqual(args, 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") { 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()) + } }