This commit is contained in:
spf13 2013-09-30 21:54:46 -04:00
parent fb5077acbe
commit a2845e7f7a
3 changed files with 98 additions and 28 deletions

View file

@ -10,11 +10,11 @@ import (
var _ = fmt.Println var _ = fmt.Println
var tp, te, tt, t1 []string var tp, te, tt, t1 []string
var flagb1, flagb2, flagb3 bool var flagb1, flagb2, flagb3, flagbr bool
var flags1, flags2, flags3 string var flags1, flags2, flags3 string
var flagi1, flagi2, flagi3 int var flagi1, flagi2, flagi3, flagir int
var globalFlag1 bool var globalFlag1 bool
var flagEcho bool var flagEcho, rootcalled bool
var cmdPrint = &Command{ var cmdPrint = &Command{
Use: "print [string to print]", Use: "print [string to print]",
@ -41,6 +41,15 @@ var cmdTimes = &Command{
Run: timesRunner, Run: timesRunner,
} }
var cmdRoot = &Command{
Use: "cobra-test",
Short: "The root can run it's own function",
Long: "The root description for help",
Run: func(cmd *Command, args []string) {
rootcalled = true
},
}
func timesRunner(cmd *Command, args []string) { func timesRunner(cmd *Command, args []string) {
tt = args tt = args
} }
@ -49,6 +58,7 @@ func flagInit() {
cmdEcho.ResetFlags() cmdEcho.ResetFlags()
cmdPrint.ResetFlags() cmdPrint.ResetFlags()
cmdTimes.ResetFlags() cmdTimes.ResetFlags()
cmdRoot.ResetFlags()
cmdEcho.Flags().IntVarP(&flagi1, "intone", "i", 123, "help message for flag intone") cmdEcho.Flags().IntVarP(&flagi1, "intone", "i", 123, "help message for flag intone")
cmdTimes.Flags().IntVarP(&flagi2, "inttwo", "j", 234, "help message for flag inttwo") cmdTimes.Flags().IntVarP(&flagi2, "inttwo", "j", 234, "help message for flag inttwo")
cmdPrint.Flags().IntVarP(&flagi3, "intthree", "i", 345, "help message for flag intthree") cmdPrint.Flags().IntVarP(&flagi3, "intthree", "i", 345, "help message for flag intthree")
@ -75,6 +85,17 @@ func initialize() *Commander {
return c return c
} }
func initializeWithRootCmd() *Commander {
cmdRoot.ResetCommands()
tt, tp, te, rootcalled = nil, nil, nil, false
cmdRoot.Flags().BoolVarP(&flagbr, "boolroot", "b", false, "help message for flag boolroot")
cmdRoot.Flags().IntVarP(&flagir, "introot", "i", 321, "help message for flag intthree")
var c = cmdRoot.ToCommander()
flagInit()
commandInit()
return c
}
func TestSingleCommand(t *testing.T) { func TestSingleCommand(t *testing.T) {
c := initialize() c := initialize()
c.AddCommand(cmdPrint, cmdEcho) c.AddCommand(cmdPrint, cmdEcho)
@ -292,3 +313,30 @@ func TestHelpCommand(t *testing.T) {
t.Errorf("Wrong error message displayed, \n %s", buf.String()) t.Errorf("Wrong error message displayed, \n %s", buf.String())
} }
} }
func TestCommandToCommander(t *testing.T) {
c := initializeWithRootCmd()
c.AddCommand(cmdPrint, cmdEcho)
c.SetArgs([]string(nil))
c.Execute()
if rootcalled != true {
t.Errorf("Root Function was not called")
}
}
func TestRootFlags(t *testing.T) {
c := initializeWithRootCmd()
c.AddCommand(cmdPrint, cmdEcho)
c.SetArgs(strings.Split("-i 17 -b", " "))
c.Execute()
if flagbr != true {
t.Errorf("flag value should be true, %v given", flagbr)
}
if flagir != 17 {
t.Errorf("flag value should be 17, %d given", flagir)
}
}

View file

@ -104,7 +104,7 @@ func (c *Command) Find(arrs []string) (*Command, []string, error) {
// if commander returned and not appropriately matched return nil & error // if commander returned and not appropriately matched return nil & error
if commandFound.Name() == c.Name() && commandFound.Name() != arrs[0] { if commandFound.Name() == c.Name() && commandFound.Name() != arrs[0] {
return nil, a, fmt.Errorf("Command not found") return nil, a, fmt.Errorf("unknown command %q\nRun 'help' for usage.\n", a[0])
} }
return commandFound, a, nil return commandFound, a, nil
@ -138,26 +138,28 @@ func (c *Command) Out() io.Writer {
} }
// execute the command determined by args and the command tree // execute the command determined by args and the command tree
func (c *Command) execute(args []string) (err error) { func (c *Command) findAndExecute(args []string) (err error) {
err = fmt.Errorf("unknown subcommand %q\nRun 'help' for usage.\n", args[0])
cmd, a, e := c.Find(args)
if e != nil {
return e
}
return cmd.execute(a)
}
func (c *Command) execute(a []string) (err error) {
if c == nil { if c == nil {
return fmt.Errorf("Called Execute() on a nil Command") return fmt.Errorf("Called Execute() on a nil Command")
} }
cmd, a, e := c.Find(args) err = c.ParseFlags(a)
if e == nil {
err = cmd.ParseFlags(a)
if err != nil { if err != nil {
return err return err
} else { } else {
argWoFlags := cmd.Flags().Args() argWoFlags := c.Flags().Args()
cmd.Run(cmd, argWoFlags) c.Run(c, argWoFlags)
return nil return nil
} }
}
err = e
return err
} }
// Used for testing // Used for testing

View file

@ -78,20 +78,39 @@ func (c *Commander) Execute() (err error) {
// initialize help as the last point possible to allow for user // initialize help as the last point possible to allow for user
// overriding // overriding
c.initHelp() c.initHelp()
var args []string
if len(c.args) == 0 { if len(c.args) == 0 {
if len(os.Args) == 1 { args = os.Args[1:]
// If only the executable is called and the root is runnable, run it } else {
args = c.args
}
if len(args) == 0 {
// Only the executable is called and the root is runnable, run it
if c.Runnable() { if c.Runnable() {
argWoFlags := c.Flags().Args() err = c.execute([]string(nil))
c.Run(c.cmd, argWoFlags)
} else { } else {
c.Usage() c.Usage()
} }
} else { } else {
err = c.execute(os.Args[1:]) err = c.findAndExecute(args)
} }
// Now handle the case where the root is runnable and only flags are provided
if err != nil && c.Runnable() {
e := c.ParseFlags(args)
if e != nil {
return e
} else { } else {
err = c.execute(c.args) argWoFlags := c.Flags().Args()
if len(argWoFlags) > 0 {
c.Usage()
} else {
c.Run(c.cmd, argWoFlags)
err = nil
}
}
} }
if err != nil { if err != nil {
@ -99,6 +118,7 @@ func (c *Commander) Execute() (err error) {
c.Printf("%v: invalid command %#q\n", c.Root().Name(), os.Args[1:]) c.Printf("%v: invalid command %#q\n", c.Root().Name(), os.Args[1:])
c.Printf("Run '%v help' for usage\n", c.Root().Name()) c.Printf("Run '%v help' for usage\n", c.Root().Name())
} }
return return
} }