···188188//
189189// If the flags fail to parse, an error will be returned and the Run function
190190// will not be called.
191191-func (c *Command) Execute() error {
192192- if c == nil {
191191+func (cmd *Command) Execute() error {
192192+ if cmd == nil {
193193 return errors.New("Execute called on a nil Command")
194194 }
195195196196 // 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.
199199- if c.parent != nil {
200200- return fmt.Errorf("Execute must be called on the root of the command tree, was called on %s", c.name)
199199+ if cmd.parent != nil {
200200+ return fmt.Errorf("Execute must be called on the root of the command tree, was called on %s", cmd.name)
201201 }
202202203203 // Use the raw arguments and the command tree to determine which subcommand (if any)
204204- // we should be invoking. If it turns out we want to invoke the root command, then
205205- // cmd here will be c.
206206- cmd, args := findRequestedCommand(c, c.args)
207207-208208- // Below this point, use cmd not c!
204204+ // we should be invoking and swap that into 'cmd'.
205205+ //
206206+ // Slightly magical trick but it simplifies a lot of stuff below.
207207+ cmd, args := findRequestedCommand(cmd, cmd.args)
209208210209 if err := cmd.flagSet().Parse(args); err != nil {
211210 return fmt.Errorf("failed to parse command flags: %w", err)
···240239 return errors.New("versionFunc was nil")
241240 }
242241243243- if err := cmd.versionFunc(c); err != nil {
242242+ if err := cmd.versionFunc(cmd); err != nil {
244243 return fmt.Errorf("version function returned an error: %w", err)
245244 }
246245···293292}
294293295294// Flags returns the set of flags for the command.
296296-func (c *Command) flagSet() *flag.Set {
297297- if c == nil {
295295+func (cmd *Command) flagSet() *flag.Set {
296296+ 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 }
302301303303- if c.flags == nil {
302302+ if cmd.flags == nil {
304303 return flag.NewSet()
305304 }
306305307307- return c.flags
306306+ return cmd.flags
308307}
309308310309// Stdout returns the configured Stdout for the Command.
311311-func (c *Command) Stdout() io.Writer {
312312- return c.root().stdout
310310+func (cmd *Command) Stdout() io.Writer {
311311+ return cmd.root().stdout
313312}
314313315314// Stderr returns the configured Stderr for the Command.
316316-func (c *Command) Stderr() io.Writer {
317317- return c.root().stderr
315315+func (cmd *Command) Stderr() io.Writer {
316316+ return cmd.root().stderr
318317}
319318320319// Stdin returns the configured Stdin for the Command.
321321-func (c *Command) Stdin() io.Reader {
322322- return c.root().stdin
320320+func (cmd *Command) Stdin() io.Reader {
321321+ return cmd.root().stdin
323322}
324323325324// Arg looks up a named positional argument by name.
···328327// then the value returned will be the default value.
329328//
330329// If no named argument exists with the given name, it will return "".
331331-func (c *Command) Arg(name string) string {
332332- for _, arg := range c.positionalArgs {
330330+func (cmd *Command) Arg(name string) string {
331331+ 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
···345344// pass through in your commands.
346345//
347346// If there were no extra arguments, it will return nil, false.
348348-func (c *Command) ExtraArgs() (args []string, ok bool) {
349349- extra := c.flagSet().ExtraArgs()
347347+func (cmd *Command) ExtraArgs() (args []string, ok bool) {
348348+ extra := cmd.flagSet().ExtraArgs()
350349 if len(extra) > 0 {
351350 return extra, true
352351 }
···355354}
356355357356// root returns the root of the command tree.
358358-func (c *Command) root() *Command {
359359- if c.parent != nil {
360360- return c.parent.root()
357357+func (cmd *Command) root() *Command {
358358+ if cmd.parent != nil {
359359+ return cmd.parent.root()
361360 }
362361363363- return c
362362+ return cmd
364363}
365364366365// hasFlag returns whether the command has a flag of the given name defined.
367367-func (c *Command) hasFlag(name string) bool {
368368- flag, ok := c.flagSet().Get(name)
366366+func (cmd *Command) hasFlag(name string) bool {
367367+ flag, ok := cmd.flagSet().Get(name)
369368 if !ok {
370369 return false
371370 }
···374373}
375374376375// hasShortFlag returns whether the command has a shorthand flag of the given name defined.
377377-func (c *Command) hasShortFlag(name string) bool {
376376+func (cmd *Command) hasShortFlag(name string) bool {
378377 if name == "" {
379378 return false
380379 }
381380382381 char, _ := utf8.DecodeRuneInString(name)
383382384384- flag, ok := c.flagSet().GetShort(char)
383383+ flag, ok := cmd.flagSet().GetShort(char)
385384 if !ok {
386385 return false
387386 }
···390389}
391390392391// subcommandNames returns a list of all the names of the current command's registered subcommands.
393393-func (c *Command) subcommandNames() []string {
394394- names := make([]string, 0, len(c.subcommands))
395395- for _, sub := range c.subcommands {
392392+func (cmd *Command) subcommandNames() []string {
393393+ names := make([]string, 0, len(cmd.subcommands))
394394+ for _, sub := range cmd.subcommands {
396395 names = append(names, sub.name)
397396 }
398397
+4-5
option.go
···1616const NoShortHand = flag.NoShortHand
17171818// Flaggable is a type constraint that defines any type capable of being parsed as a command line flag.
1919-//
2020-// It's worth noting that the complete set of supported types is wider than this constraint appears
2121-// as e.g. a [time.Duration] is actually just an int64 underneath, likewise a [net.IP] is actually just []byte.
2219type Flaggable flag.Flaggable
23202421// Note: this must be a type alias (FlagCount = flag.Count), not a newtype (FlagCount flag.Count)
···7572 versionCalled bool
7673}
77747878-// build builds an returns a Command from the config, applying validation
7979-// to the whole thing.
7575+// build builds an returns a Command from the config.
7676+//
7777+// The returned command is a completely standalone CLI program with no back-references
7878+// to the config, so is effectively immutable to the user.
8079func (c *config) build() *Command {
8180 cmd := &Command{
8281 stdin: c.stdin,