From ccaecb155a2177302cb56cae929251a256d0f646 Mon Sep 17 00:00:00 2001 From: Nick Miyake Date: Wed, 6 Dec 2017 23:49:35 -0800 Subject: [PATCH] Ensure that '--version' flag works properly for root command (#595) Make it so that, in the case that the root command is not runnable but has subcommands, specifying a '--version' flag will still run the "version" behavior. --- command.go | 6 +++++- command_test.go | 2 +- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/command.go b/command.go index 58980bc2..5fefb58d 100644 --- a/command.go +++ b/command.go @@ -685,7 +685,7 @@ func (c *Command) execute(a []string) (err error) { return err } - if helpVal || !c.Runnable() { + if helpVal { return flag.ErrHelp } @@ -705,6 +705,10 @@ func (c *Command) execute(a []string) (err error) { } } + if !c.Runnable() { + return flag.ErrHelp + } + c.preRun() argWoFlags := c.Flags().Args() diff --git a/command_test.go b/command_test.go index 142203f1..d3dde152 100644 --- a/command_test.go +++ b/command_test.go @@ -867,7 +867,7 @@ func TestVersionTemplate(t *testing.T) { } func TestVersionFlagExecutedOnSubcommand(t *testing.T) { - rootCmd := &Command{Use: "root", Version: "1.0.0", Run: emptyRun} + rootCmd := &Command{Use: "root", Version: "1.0.0"} rootCmd.AddCommand(&Command{Use: "sub", Run: emptyRun}) output, err := executeCommand(rootCmd, "--version", "sub")