fixed spf13/cobra#1 spf13/hugo#88
This commit is contained in:
parent
fb5077acbe
commit
a2845e7f7a
@ -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)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
32
command.go
32
command.go
@ -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 {
|
if err != nil {
|
||||||
err = cmd.ParseFlags(a)
|
return err
|
||||||
if err != nil {
|
} else {
|
||||||
return err
|
argWoFlags := c.Flags().Args()
|
||||||
} else {
|
c.Run(c, argWoFlags)
|
||||||
argWoFlags := cmd.Flags().Args()
|
return nil
|
||||||
cmd.Run(cmd, argWoFlags)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
err = e
|
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Used for testing
|
// Used for testing
|
||||||
|
40
commander.go
40
commander.go
@ -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 {
|
||||||
if c.Runnable() {
|
args = c.args
|
||||||
argWoFlags := c.Flags().Args()
|
}
|
||||||
c.Run(c.cmd, argWoFlags)
|
|
||||||
} else {
|
if len(args) == 0 {
|
||||||
c.Usage()
|
// Only the executable is called and the root is runnable, run it
|
||||||
}
|
if c.Runnable() {
|
||||||
|
err = c.execute([]string(nil))
|
||||||
} else {
|
} else {
|
||||||
err = c.execute(os.Args[1:])
|
c.Usage()
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
err = c.execute(c.args)
|
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 {
|
||||||
|
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
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user