ExecuteC executes the command.
()
| 1082 | |
| 1083 | // ExecuteC executes the command. |
| 1084 | func (c *Command) ExecuteC() (cmd *Command, err error) { |
| 1085 | if c.ctx == nil { |
| 1086 | c.ctx = context.Background() |
| 1087 | } |
| 1088 | |
| 1089 | // Regardless of what command execute is called on, run on Root only |
| 1090 | if c.HasParent() { |
| 1091 | return c.Root().ExecuteC() |
| 1092 | } |
| 1093 | |
| 1094 | // windows hook |
| 1095 | if preExecHookFn != nil { |
| 1096 | preExecHookFn(c) |
| 1097 | } |
| 1098 | |
| 1099 | // initialize help at the last point to allow for user overriding |
| 1100 | c.InitDefaultHelpCmd() |
| 1101 | |
| 1102 | args := c.args |
| 1103 | |
| 1104 | // Workaround FAIL with "go test -v" or "cobra.test -test.v", see #155 |
| 1105 | if c.args == nil && filepath.Base(os.Args[0]) != "cobra.test" { |
| 1106 | args = os.Args[1:] |
| 1107 | } |
| 1108 | |
| 1109 | // initialize the __complete command to be used for shell completion |
| 1110 | c.initCompleteCmd(args) |
| 1111 | |
| 1112 | // initialize the default completion command |
| 1113 | c.InitDefaultCompletionCmd(args...) |
| 1114 | |
| 1115 | // Now that all commands have been created, let's make sure all groups |
| 1116 | // are properly created also |
| 1117 | c.checkCommandGroups() |
| 1118 | |
| 1119 | var flags []string |
| 1120 | if c.TraverseChildren { |
| 1121 | cmd, flags, err = c.Traverse(args) |
| 1122 | } else { |
| 1123 | cmd, flags, err = c.Find(args) |
| 1124 | } |
| 1125 | if err != nil { |
| 1126 | // If found parse to a subcommand and then failed, talk about the subcommand |
| 1127 | if cmd != nil { |
| 1128 | c = cmd |
| 1129 | } |
| 1130 | if !c.SilenceErrors { |
| 1131 | c.PrintErrln(c.ErrPrefix(), err.Error()) |
| 1132 | c.PrintErrf("Run '%v --help' for usage.\n", c.CommandPath()) |
| 1133 | } |
| 1134 | return c, err |
| 1135 | } |
| 1136 | |
| 1137 | cmd.commandCalledAs.called = true |
| 1138 | if cmd.commandCalledAs.name == "" { |
| 1139 | cmd.commandCalledAs.name = cmd.Name() |
| 1140 | } |
| 1141 |