fixed spf13/cobra#1 spf13/hugo#88
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by spf13 · committed by spf13 · parent fb5077a · Browse files · 3 files·+98 added−28 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+98 added−28 removed
- other:+98 added−28 removed
Provenance
- Commit
a2845e7- Tree
e0b2e84- Author
spf13 <steve.francia@gmail.com>- Committer
spf13 <steve.francia@gmail.com>- Parent
fb5077a
3 files changed+98 added−28 removed
modified:cobra_test.go+51 added-3 removed
8 unmodified lines9 10 var _ = fmt.Println11 12 var tp, te, tt, t1 []string13-var flagb1, flagb2, flagb3 bool13+var flagb1, flagb2, flagb3, flagbr bool14 var flags1, flags2, flags3 string15-var flagi1, flagi2, flagi3 int15+var flagi1, flagi2, flagi3, flagir int16 var globalFlag1 bool17-var flagEcho bool17+var flagEcho, rootcalled bool18 19 var cmdPrint = &Command{20 Use: "print [string to print]",21 Short: "Print anything to the screen",18 unmodified lines40 Long: `an slightly useless command for testing.`,41 Run: timesRunner,42 }43 44+var cmdRoot = &Command{45+ Use: "cobra-test",46+ Short: "The root can run it's own function",47+ Long: "The root description for help",48+ Run: func(cmd *Command, args []string) {49+ rootcalled = true50+ },51+}52+53 func timesRunner(cmd *Command, args []string) {54 tt = args55 }56 57 func flagInit() {58 cmdEcho.ResetFlags()59 cmdPrint.ResetFlags()60 cmdTimes.ResetFlags()61+ cmdRoot.ResetFlags()62 cmdEcho.Flags().IntVarP(&flagi1, "intone", "i", 123, "help message for flag intone")63 cmdTimes.Flags().IntVarP(&flagi2, "inttwo", "j", 234, "help message for flag inttwo")64 cmdPrint.Flags().IntVarP(&flagi3, "intthree", "i", 345, "help message for flag intthree")65 cmdEcho.PersistentFlags().StringVarP(&flags1, "strone", "s", "one", "help message for flag strone")18 unmodified lines84 commandInit()85 return c86 }87 88+func initializeWithRootCmd() *Commander {89+ cmdRoot.ResetCommands()90+ tt, tp, te, rootcalled = nil, nil, nil, false91+ cmdRoot.Flags().BoolVarP(&flagbr, "boolroot", "b", false, "help message for flag boolroot")92+ cmdRoot.Flags().IntVarP(&flagir, "introot", "i", 321, "help message for flag intthree")93+ var c = cmdRoot.ToCommander()94+ flagInit()95+ commandInit()96+ return c97+}98+99 func TestSingleCommand(t *testing.T) {100 c := initialize()101 c.AddCommand(cmdPrint, cmdEcho)102 c.SetArgs(strings.Split("print one two", " "))209 unmodified lines312 if !strings.Contains(buf.String(), cmdTimes.Long) {313 t.Errorf("Wrong error message displayed, \n %s", buf.String())314 }315 }316+317+func TestCommandToCommander(t *testing.T) {318+ c := initializeWithRootCmd()319+ c.AddCommand(cmdPrint, cmdEcho)320+ c.SetArgs([]string(nil))321+ c.Execute()322+323+ if rootcalled != true {324+ t.Errorf("Root Function was not called")325+ }326+}327+328+func TestRootFlags(t *testing.T) {329+ c := initializeWithRootCmd()330+ c.AddCommand(cmdPrint, cmdEcho)331+ c.SetArgs(strings.Split("-i 17 -b", " "))332+ c.Execute()333+334+ if flagbr != true {335+ t.Errorf("flag value should be true, %v given", flagbr)336+ }337+338+ if flagir != 17 {339+ t.Errorf("flag value should be 17, %d given", flagir)340+ }341+342+}modified:command.go+17 added-15 removed
102 unmodified lines103 commandFound, a := innerfind(c, arrs)104 105 // if commander returned and not appropriately matched return nil & error106 if commandFound.Name() == c.Name() && commandFound.Name() != arrs[0] {107- return nil, a, fmt.Errorf("Command not found")107+ return nil, a, fmt.Errorf("unknown command %q\nRun 'help' for usage.\n", a[0])108 }109 110 return commandFound, a, nil111 }25 unmodified lines137 return c.Commander().out()138 }139 140 // execute the command determined by args and the command tree141-func (c *Command) execute(args []string) (err error) {142- err = fmt.Errorf("unknown subcommand %q\nRun 'help' for usage.\n", args[0])141+func (c *Command) findAndExecute(args []string) (err error) {142 143+ cmd, a, e := c.Find(args)144+ if e != nil {145+ return e146+ }147+ return cmd.execute(a)148+}149+150+func (c *Command) execute(a []string) (err error) {151 if c == nil {152 return fmt.Errorf("Called Execute() on a nil Command")153 }154 148- cmd, a, e := c.Find(args)149- if e == nil {150- err = cmd.ParseFlags(a)151- if err != nil {152- return err153- } else {154- argWoFlags := cmd.Flags().Args()155- cmd.Run(cmd, argWoFlags)156- return nil155+ err = c.ParseFlags(a)156+ if err != nil {157+ return err158+ } else {159+ argWoFlags := c.Flags().Args()160+ c.Run(c, argWoFlags)161+ return nil157- }162 }159- err = e160- return err163 }164 165 // Used for testing166 func (c *Command) ResetCommands() {261 unmodified linesmodified:commander.go+30 added-10 removed
76 unmodified lines77 func (c *Commander) Execute() (err error) {78 // initialize help as the last point possible to allow for user79 // overriding80 c.initHelp()81+ var args []string82+83 if len(c.args) == 0 {82- if len(os.Args) == 1 {83- // If only the executable is called and the root is runnable, run it84- if c.Runnable() {85- argWoFlags := c.Flags().Args()86- c.Run(c.cmd, argWoFlags)87- } else {88- c.Usage()89- }84+ args = os.Args[1:]85+ } else {86+ args = c.args87+ }88+89+ if len(args) == 0 {90+ // Only the executable is called and the root is runnable, run it91+ if c.Runnable() {92+ err = c.execute([]string(nil))93 } else {91- err = c.execute(os.Args[1:])94+ c.Usage()95 }96 } else {94- err = c.execute(c.args)97+ err = c.findAndExecute(args)98 }99 100+ // Now handle the case where the root is runnable and only flags are provided101+ if err != nil && c.Runnable() {102+ e := c.ParseFlags(args)103+ if e != nil {104+ return e105+ } else {106+ argWoFlags := c.Flags().Args()107+ if len(argWoFlags) > 0 {108+ c.Usage()109+ } else {110+ c.Run(c.cmd, argWoFlags)111+ err = nil112+ }113+ }114+ }115+116 if err != nil {117 c.Println("Error:", err.Error())118 c.Printf("%v: invalid command %#q\n", c.Root().Name(), os.Args[1:])119 c.Printf("Run '%v help' for usage\n", c.Root().Name())120 }121+122 return123 }124 125 func (c *Commander) out() io.Writer {63 unmodified lines