mirror of
https://github.com/spf13/cobra
synced 2024-11-24 14:47:12 +00:00
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.
This commit is contained in:
parent
c156af3984
commit
ccaecb155a
2 changed files with 6 additions and 2 deletions
|
@ -685,7 +685,7 @@ func (c *Command) execute(a []string) (err error) {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if helpVal || !c.Runnable() {
|
if helpVal {
|
||||||
return flag.ErrHelp
|
return flag.ErrHelp
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -705,6 +705,10 @@ func (c *Command) execute(a []string) (err error) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if !c.Runnable() {
|
||||||
|
return flag.ErrHelp
|
||||||
|
}
|
||||||
|
|
||||||
c.preRun()
|
c.preRun()
|
||||||
|
|
||||||
argWoFlags := c.Flags().Args()
|
argWoFlags := c.Flags().Args()
|
||||||
|
|
|
@ -867,7 +867,7 @@ func TestVersionTemplate(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestVersionFlagExecutedOnSubcommand(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})
|
rootCmd.AddCommand(&Command{Use: "sub", Run: emptyRun})
|
||||||
|
|
||||||
output, err := executeCommand(rootCmd, "--version", "sub")
|
output, err := executeCommand(rootCmd, "--version", "sub")
|
||||||
|
|
Loading…
Reference in a new issue