Skip to content
Myr

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

3 files+98 added−28 removed

modified:cobra_test.go+51 added-3 removed
8 unmodified lines
910var _ = fmt.Println1112var tp, te, tt, t1 []string13var flagb1, flagb2, flagb3 bool13var flagb1, flagb2, flagb3, flagbr bool14var flags1, flags2, flags3 string15var flagi1, flagi2, flagi3 int15var flagi1, flagi2, flagi3, flagir int16var globalFlag1 bool17var flagEcho bool17var flagEcho, rootcalled bool1819var cmdPrint = &Command{20 Use: "print [string to print]",21 Short: "Print anything to the screen",
18 unmodified lines
40 Long: `an slightly useless command for testing.`,41 Run: timesRunner,42}4344var 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}5253func timesRunner(cmd *Command, args []string) {54 tt = args55}5657func 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 lines
84 commandInit()85 return c86}8788func 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}9899func TestSingleCommand(t *testing.T) {100 c := initialize()101 c.AddCommand(cmdPrint, cmdEcho)102 c.SetArgs(strings.Split("print one two", " "))
209 unmodified lines
312 if !strings.Contains(buf.String(), cmdTimes.Long) {313 t.Errorf("Wrong error message displayed, \n %s", buf.String())314 }315}316317func TestCommandToCommander(t *testing.T) {318 c := initializeWithRootCmd()319 c.AddCommand(cmdPrint, cmdEcho)320 c.SetArgs([]string(nil))321 c.Execute()322323 if rootcalled != true {324 t.Errorf("Root Function was not called")325 }326}327328func TestRootFlags(t *testing.T) {329 c := initializeWithRootCmd()330 c.AddCommand(cmdPrint, cmdEcho)331 c.SetArgs(strings.Split("-i 17 -b", " "))332 c.Execute()333334 if flagbr != true {335 t.Errorf("flag value should be true, %v given", flagbr)336 }337338 if flagir != 17 {339 t.Errorf("flag value should be 17, %d given", flagir)340 }341342}
modified:command.go+17 added-15 removed
102 unmodified lines
103 commandFound, a := innerfind(c, arrs)104105 // 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 }109110 return commandFound, a, nil111}
25 unmodified lines
137 return c.Commander().out()138}139140// execute the command determined by args and the command tree141func (c *Command) execute(args []string) (err error) {142 err = fmt.Errorf("unknown subcommand %q\nRun 'help' for usage.\n", args[0])141func (c *Command) findAndExecute(args []string) (err error) {142143 cmd, a, e := c.Find(args)144 if e != nil {145 return e146 }147 return cmd.execute(a)148}149150func (c *Command) execute(a []string) (err error) {151 if c == nil {152 return fmt.Errorf("Called Execute() on a nil Command")153 }154148 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}164165// Used for testing166func (c *Command) ResetCommands() {
261 unmodified lines
modified:commander.go+30 added-10 removed
76 unmodified lines
77func (c *Commander) Execute() (err error) {78 // initialize help as the last point possible to allow for user79 // overriding80 c.initHelp()81 var args []string8283 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 }8889 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 }99100 // 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 }115116 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 }121122 return123}124125func (c *Commander) out() io.Writer {
63 unmodified lines