From 2272dad83ef28967ad46bf27470b56be95840ada Mon Sep 17 00:00:00 2001 From: Jesse Szwedko Date: Tue, 2 Jun 2015 20:16:44 -0700 Subject: [PATCH] Version and help check should look for local flags too Now that Global looks up the chain of contexts, the top level should access the flags without the prefix (i.e. Bool rather than GlobalBool). --- app_test.go | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++++ help.go | 4 ++-- 2 files changed, 63 insertions(+), 2 deletions(-) diff --git a/app_test.go b/app_test.go index fb8111d..3d5678b 100644 --- a/app_test.go +++ b/app_test.go @@ -717,3 +717,64 @@ func TestApp_Run_CommandWithSubcommandHasHelpTopic(t *testing.T) { } } } + +func TestApp_Run_Help(t *testing.T) { + var helpArguments = [][]string{{"boom", "--help"}, {"boom", "-h"}, {"boom", "help"}} + + for _, args := range helpArguments { + buf := new(bytes.Buffer) + + t.Logf("==> checking with arguments %v", args) + + app := cli.NewApp() + app.Name = "boom" + app.Usage = "make an explosive entrance" + app.Writer = buf + app.Action = func(c *cli.Context) { + buf.WriteString("boom I say!") + } + + err := app.Run(args) + if err != nil { + t.Error(err) + } + + output := buf.String() + t.Logf("output: %q\n", buf.Bytes()) + + if !strings.Contains(output, "boom - make an explosive entrance") { + t.Errorf("want help to contain %q, did not: \n%q", "boom - make an explosive entrance", output) + } + } +} + +func TestApp_Run_Version(t *testing.T) { + var versionArguments = [][]string{{"boom", "--version"}, {"boom", "-v"}} + + for _, args := range versionArguments { + buf := new(bytes.Buffer) + + t.Logf("==> checking with arguments %v", args) + + app := cli.NewApp() + app.Name = "boom" + app.Usage = "make an explosive entrance" + app.Version = "0.1.0" + app.Writer = buf + app.Action = func(c *cli.Context) { + buf.WriteString("boom I say!") + } + + err := app.Run(args) + if err != nil { + t.Error(err) + } + + output := buf.String() + t.Logf("output: %q\n", buf.Bytes()) + + if !strings.Contains(output, "0.1.0") { + t.Errorf("want version to contain %q, did not: \n%q", "0.1.0", output) + } + } +} diff --git a/help.go b/help.go index 1117945..9b7b9b9 100644 --- a/help.go +++ b/help.go @@ -181,7 +181,7 @@ func printHelp(out io.Writer, templ string, data interface{}) { } func checkVersion(c *Context) bool { - if c.GlobalBool("version") { + if c.GlobalBool("version") || c.GlobalBool("v") || c.Bool("version") || c.Bool("v") { ShowVersion(c) return true } @@ -190,7 +190,7 @@ func checkVersion(c *Context) bool { } func checkHelp(c *Context) bool { - if c.GlobalBool("h") || c.GlobalBool("help") { + if c.GlobalBool("h") || c.GlobalBool("help") || c.Bool("h") || c.Bool("help") { ShowAppHelp(c) return true }