@@ -188,24 +188,23 @@ func (e example) String() string {
188188//
189189// If the flags fail to parse, an error will be returned and the Run function
190190// will not be called.
191- func (c * Command ) Execute () error {
192- if c == nil {
191+ func (cmd * Command ) Execute () error {
192+ if cmd == nil {
193193 return errors .New ("Execute called on a nil Command" )
194194 }
195195
196196 // Regardless of where we call execute, run it only from the root command, this is to ensure
197197 // that when we use the arguments to go and find the subcommand to run (if needed), then we
198198 // at the root of the command tree.
199- if c .parent != nil {
200- return fmt .Errorf ("Execute must be called on the root of the command tree, was called on %s" , c .name )
199+ if cmd .parent != nil {
200+ return fmt .Errorf ("Execute must be called on the root of the command tree, was called on %s" , cmd .name )
201201 }
202202
203203 // Use the raw arguments and the command tree to determine which subcommand (if any)
204- // we should be invoking. If it turns out we want to invoke the root command, then
205- // cmd here will be c.
206- cmd , args := findRequestedCommand (c , c .args )
207-
208- // Below this point, use cmd not c!
204+ // we should be invoking and swap that into 'cmd'.
205+ //
206+ // Slightly magical trick but it simplifies a lot of stuff below.
207+ cmd , args := findRequestedCommand (cmd , cmd .args )
209208
210209 if err := cmd .flagSet ().Parse (args ); err != nil {
211210 return fmt .Errorf ("failed to parse command flags: %w" , err )
@@ -240,7 +239,7 @@ func (c *Command) Execute() error {
240239 return errors .New ("versionFunc was nil" )
241240 }
242241
243- if err := cmd .versionFunc (c ); err != nil {
242+ if err := cmd .versionFunc (cmd ); err != nil {
244243 return fmt .Errorf ("version function returned an error: %w" , err )
245244 }
246245
@@ -293,33 +292,33 @@ func (c *Command) Execute() error {
293292}
294293
295294// Flags returns the set of flags for the command.
296- func (c * Command ) flagSet () * flag.Set {
297- if c == nil {
295+ func (cmd * Command ) flagSet () * flag.Set {
296+ if cmd == nil {
298297 // Only thing to do really, slightly more helpful than a generic
299298 // nil pointer dereference
300299 panic ("flagSet called on a nil Command" )
301300 }
302301
303- if c .flags == nil {
302+ if cmd .flags == nil {
304303 return flag .NewSet ()
305304 }
306305
307- return c .flags
306+ return cmd .flags
308307}
309308
310309// Stdout returns the configured Stdout for the Command.
311- func (c * Command ) Stdout () io.Writer {
312- return c .root ().stdout
310+ func (cmd * Command ) Stdout () io.Writer {
311+ return cmd .root ().stdout
313312}
314313
315314// Stderr returns the configured Stderr for the Command.
316- func (c * Command ) Stderr () io.Writer {
317- return c .root ().stderr
315+ func (cmd * Command ) Stderr () io.Writer {
316+ return cmd .root ().stderr
318317}
319318
320319// Stdin returns the configured Stdin for the Command.
321- func (c * Command ) Stdin () io.Reader {
322- return c .root ().stdin
320+ func (cmd * Command ) Stdin () io.Reader {
321+ return cmd .root ().stdin
323322}
324323
325324// Arg looks up a named positional argument by name.
@@ -328,8 +327,8 @@ func (c *Command) Stdin() io.Reader {
328327// then the value returned will be the default value.
329328//
330329// If no named argument exists with the given name, it will return "".
331- func (c * Command ) Arg (name string ) string {
332- for _ , arg := range c .positionalArgs {
330+ func (cmd * Command ) Arg (name string ) string {
331+ for _ , arg := range cmd .positionalArgs {
333332 if arg .name == name {
334333 // arg.value will have been set to the default already during command line parsing
335334 // if the arg was not provided
@@ -345,8 +344,8 @@ func (c *Command) Arg(name string) string {
345344// pass through in your commands.
346345//
347346// If there were no extra arguments, it will return nil, false.
348- func (c * Command ) ExtraArgs () (args []string , ok bool ) {
349- extra := c .flagSet ().ExtraArgs ()
347+ func (cmd * Command ) ExtraArgs () (args []string , ok bool ) {
348+ extra := cmd .flagSet ().ExtraArgs ()
350349 if len (extra ) > 0 {
351350 return extra , true
352351 }
@@ -355,17 +354,17 @@ func (c *Command) ExtraArgs() (args []string, ok bool) {
355354}
356355
357356// root returns the root of the command tree.
358- func (c * Command ) root () * Command {
359- if c .parent != nil {
360- return c .parent .root ()
357+ func (cmd * Command ) root () * Command {
358+ if cmd .parent != nil {
359+ return cmd .parent .root ()
361360 }
362361
363- return c
362+ return cmd
364363}
365364
366365// hasFlag returns whether the command has a flag of the given name defined.
367- func (c * Command ) hasFlag (name string ) bool {
368- flag , ok := c .flagSet ().Get (name )
366+ func (cmd * Command ) hasFlag (name string ) bool {
367+ flag , ok := cmd .flagSet ().Get (name )
369368 if ! ok {
370369 return false
371370 }
@@ -374,14 +373,14 @@ func (c *Command) hasFlag(name string) bool {
374373}
375374
376375// hasShortFlag returns whether the command has a shorthand flag of the given name defined.
377- func (c * Command ) hasShortFlag (name string ) bool {
376+ func (cmd * Command ) hasShortFlag (name string ) bool {
378377 if name == "" {
379378 return false
380379 }
381380
382381 char , _ := utf8 .DecodeRuneInString (name )
383382
384- flag , ok := c .flagSet ().GetShort (char )
383+ flag , ok := cmd .flagSet ().GetShort (char )
385384 if ! ok {
386385 return false
387386 }
@@ -390,9 +389,9 @@ func (c *Command) hasShortFlag(name string) bool {
390389}
391390
392391// subcommandNames returns a list of all the names of the current command's registered subcommands.
393- func (c * Command ) subcommandNames () []string {
394- names := make ([]string , 0 , len (c .subcommands ))
395- for _ , sub := range c .subcommands {
392+ func (cmd * Command ) subcommandNames () []string {
393+ names := make ([]string , 0 , len (cmd .subcommands ))
394+ for _ , sub := range cmd .subcommands {
396395 names = append (names , sub .name )
397396 }
398397
0 commit comments