mirror of
https://github.com/spf13/cobra
synced 2024-12-28 23:37:09 +00:00
Cobra behavior is now more consistent. Invalid flags cause Usage to be printed.
This commit is contained in:
parent
7ab4fd75e9
commit
71bb1dfdcd
2 changed files with 26 additions and 13 deletions
|
@ -119,6 +119,16 @@ func initializeWithRootCmd() *Command {
|
|||
return cmdRootWithRun
|
||||
}
|
||||
|
||||
func checkOutputContains(t *testing.T, c *Command, check string) {
|
||||
buf := new(bytes.Buffer)
|
||||
c.SetOutput(buf)
|
||||
c.Execute()
|
||||
|
||||
if !strings.Contains(buf.String(), check) {
|
||||
t.Errorf("Unexpected response.\nExpecting to contain: \n %q\nGot:\n %q\n", check, buf.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestSingleCommand(t *testing.T) {
|
||||
c := initialize()
|
||||
c.AddCommand(cmdPrint, cmdEcho)
|
||||
|
@ -378,29 +388,19 @@ func TestPersistentFlags(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestHelpCommand(t *testing.T) {
|
||||
buf := new(bytes.Buffer)
|
||||
c := initialize()
|
||||
cmdEcho.AddCommand(cmdTimes)
|
||||
c.AddCommand(cmdPrint, cmdEcho)
|
||||
c.SetArgs(strings.Split("help echo", " "))
|
||||
c.SetOutput(buf)
|
||||
c.Execute()
|
||||
|
||||
if !strings.Contains(buf.String(), cmdEcho.Long) {
|
||||
t.Errorf("Wrong error message displayed, \n %s", buf.String())
|
||||
}
|
||||
checkOutputContains(t, c, cmdEcho.Long)
|
||||
|
||||
buf.Reset()
|
||||
c = initialize()
|
||||
cmdEcho.AddCommand(cmdTimes)
|
||||
c.AddCommand(cmdPrint, cmdEcho)
|
||||
c.SetArgs(strings.Split("help echo times", " "))
|
||||
c.SetOutput(buf)
|
||||
c.Execute()
|
||||
|
||||
if !strings.Contains(buf.String(), cmdTimes.Long) {
|
||||
t.Errorf("Wrong error message displayed, \n %s", buf.String())
|
||||
}
|
||||
checkOutputContains(t, c, cmdTimes.Long)
|
||||
}
|
||||
|
||||
func TestRunnableRootCommand(t *testing.T) {
|
||||
|
@ -427,5 +427,15 @@ func TestRootFlags(t *testing.T) {
|
|||
if flagir != 17 {
|
||||
t.Errorf("flag value should be 17, %d given", flagir)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestRootHelp(t *testing.T) {
|
||||
fmt.Println("testing root help")
|
||||
c := initializeWithRootCmd()
|
||||
c.AddCommand(cmdPrint, cmdEcho)
|
||||
c.SetArgs(strings.Split("--help", " "))
|
||||
e := c.Execute()
|
||||
fmt.Println(e)
|
||||
|
||||
checkOutputContains(t, c, "Available Commands:")
|
||||
}
|
||||
|
|
|
@ -344,6 +344,9 @@ func (c *Command) Execute() (err error) {
|
|||
if err != nil && c.Runnable() {
|
||||
e := c.ParseFlags(args)
|
||||
if e != nil {
|
||||
// Flags parsing had an error.
|
||||
fmt.Println(e)
|
||||
c.Usage()
|
||||
return e
|
||||
} else {
|
||||
argWoFlags := c.Flags().Args()
|
||||
|
|
Loading…
Reference in a new issue