OSDN Git Service

new repo
[bytom/vapor.git] / vendor / github.com / spf13 / cobra / command.go
1 // Copyright © 2013 Steve Francia <spf@spf13.com>.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 // http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13
14 // Package cobra is a commander providing a simple interface to create powerful modern CLI interfaces.
15 // In addition to providing an interface, Cobra simultaneously provides a controller to organize your application code.
16 package cobra
17
18 import (
19         "bytes"
20         "fmt"
21         "io"
22         "os"
23         "path/filepath"
24         "sort"
25         "strings"
26
27         flag "github.com/spf13/pflag"
28 )
29
30 // Command is just that, a command for your application.
31 // E.g.  'go run ...' - 'run' is the command. Cobra requires
32 // you to define the usage and description as part of your command
33 // definition to ensure usability.
34 type Command struct {
35         // Use is the one-line usage message.
36         Use string
37
38         // Aliases is an array of aliases that can be used instead of the first word in Use.
39         Aliases []string
40
41         // SuggestFor is an array of command names for which this command will be suggested -
42         // similar to aliases but only suggests.
43         SuggestFor []string
44
45         // Short is the short description shown in the 'help' output.
46         Short string
47
48         // Long is the long message shown in the 'help <this-command>' output.
49         Long string
50
51         // Example is examples of how to use the command.
52         Example string
53
54         // ValidArgs is list of all valid non-flag arguments that are accepted in bash completions
55         ValidArgs []string
56
57         // Expected arguments
58         Args PositionalArgs
59
60         // ArgAliases is List of aliases for ValidArgs.
61         // These are not suggested to the user in the bash completion,
62         // but accepted if entered manually.
63         ArgAliases []string
64
65         // BashCompletionFunction is custom functions used by the bash autocompletion generator.
66         BashCompletionFunction string
67
68         // Deprecated defines, if this command is deprecated and should print this string when used.
69         Deprecated string
70
71         // Hidden defines, if this command is hidden and should NOT show up in the list of available commands.
72         Hidden bool
73
74         // Annotations are key/value pairs that can be used by applications to identify or
75         // group commands.
76         Annotations map[string]string
77
78         // The *Run functions are executed in the following order:
79         //   * PersistentPreRun()
80         //   * PreRun()
81         //   * Run()
82         //   * PostRun()
83         //   * PersistentPostRun()
84         // All functions get the same args, the arguments after the command name.
85         //
86         // PersistentPreRun: children of this command will inherit and execute.
87         PersistentPreRun func(cmd *Command, args []string)
88         // PersistentPreRunE: PersistentPreRun but returns an error.
89         PersistentPreRunE func(cmd *Command, args []string) error
90         // PreRun: children of this command will not inherit.
91         PreRun func(cmd *Command, args []string)
92         // PreRunE: PreRun but returns an error.
93         PreRunE func(cmd *Command, args []string) error
94         // Run: Typically the actual work function. Most commands will only implement this.
95         Run func(cmd *Command, args []string)
96         // RunE: Run but returns an error.
97         RunE func(cmd *Command, args []string) error
98         // PostRun: run after the Run command.
99         PostRun func(cmd *Command, args []string)
100         // PostRunE: PostRun but returns an error.
101         PostRunE func(cmd *Command, args []string) error
102         // PersistentPostRun: children of this command will inherit and execute after PostRun.
103         PersistentPostRun func(cmd *Command, args []string)
104         // PersistentPostRunE: PersistentPostRun but returns an error.
105         PersistentPostRunE func(cmd *Command, args []string) error
106
107         // SilenceErrors is an option to quiet errors down stream.
108         SilenceErrors bool
109
110         // SilenceUsage is an option to silence usage when an error occurs.
111         SilenceUsage bool
112
113         // DisableFlagParsing disables the flag parsing.
114         // If this is true all flags will be passed to the command as arguments.
115         DisableFlagParsing bool
116
117         // DisableAutoGenTag defines, if gen tag ("Auto generated by spf13/cobra...")
118         // will be printed by generating docs for this command.
119         DisableAutoGenTag bool
120
121         // DisableSuggestions disables the suggestions based on Levenshtein distance
122         // that go along with 'unknown command' messages.
123         DisableSuggestions bool
124         // SuggestionsMinimumDistance defines minimum levenshtein distance to display suggestions.
125         // Must be > 0.
126         SuggestionsMinimumDistance int
127
128         // TraverseChildren parses flags on all parents before executing child command.
129         TraverseChildren bool
130
131         // commands is the list of commands supported by this program.
132         commands []*Command
133         // parent is a parent command for this command.
134         parent *Command
135         // Max lengths of commands' string lengths for use in padding.
136         commandsMaxUseLen         int
137         commandsMaxCommandPathLen int
138         commandsMaxNameLen        int
139         // commandsAreSorted defines, if command slice are sorted or not.
140         commandsAreSorted bool
141
142         // args is actual args parsed from flags.
143         args []string
144         // flagErrorBuf contains all error messages from pflag.
145         flagErrorBuf *bytes.Buffer
146         // flags is full set of flags.
147         flags *flag.FlagSet
148         // pflags contains persistent flags.
149         pflags *flag.FlagSet
150         // lflags contains local flags.
151         lflags *flag.FlagSet
152         // iflags contains inherited flags.
153         iflags *flag.FlagSet
154         // parentsPflags is all persistent flags of cmd's parents.
155         parentsPflags *flag.FlagSet
156         // globNormFunc is the global normalization function
157         // that we can use on every pflag set and children commands
158         globNormFunc func(f *flag.FlagSet, name string) flag.NormalizedName
159
160         // output is an output writer defined by user.
161         output io.Writer
162         // usageFunc is usage func defined by user.
163         usageFunc func(*Command) error
164         // usageTemplate is usage template defined by user.
165         usageTemplate string
166         // flagErrorFunc is func defined by user and it's called when the parsing of
167         // flags returns an error.
168         flagErrorFunc func(*Command, error) error
169         // helpTemplate is help template defined by user.
170         helpTemplate string
171         // helpFunc is help func defined by user.
172         helpFunc func(*Command, []string)
173         // helpCommand is command with usage 'help'. If it's not defined by user,
174         // cobra uses default help command.
175         helpCommand *Command
176 }
177
178 // SetArgs sets arguments for the command. It is set to os.Args[1:] by default, if desired, can be overridden
179 // particularly useful when testing.
180 func (c *Command) SetArgs(a []string) {
181         c.args = a
182 }
183
184 // SetOutput sets the destination for usage and error messages.
185 // If output is nil, os.Stderr is used.
186 func (c *Command) SetOutput(output io.Writer) {
187         c.output = output
188 }
189
190 // SetUsageFunc sets usage function. Usage can be defined by application.
191 func (c *Command) SetUsageFunc(f func(*Command) error) {
192         c.usageFunc = f
193 }
194
195 // SetUsageTemplate sets usage template. Can be defined by Application.
196 func (c *Command) SetUsageTemplate(s string) {
197         c.usageTemplate = s
198 }
199
200 // SetFlagErrorFunc sets a function to generate an error when flag parsing
201 // fails.
202 func (c *Command) SetFlagErrorFunc(f func(*Command, error) error) {
203         c.flagErrorFunc = f
204 }
205
206 // SetHelpFunc sets help function. Can be defined by Application.
207 func (c *Command) SetHelpFunc(f func(*Command, []string)) {
208         c.helpFunc = f
209 }
210
211 // SetHelpCommand sets help command.
212 func (c *Command) SetHelpCommand(cmd *Command) {
213         c.helpCommand = cmd
214 }
215
216 // SetHelpTemplate sets help template to be used. Application can use it to set custom template.
217 func (c *Command) SetHelpTemplate(s string) {
218         c.helpTemplate = s
219 }
220
221 // SetGlobalNormalizationFunc sets a normalization function to all flag sets and also to child commands.
222 // The user should not have a cyclic dependency on commands.
223 func (c *Command) SetGlobalNormalizationFunc(n func(f *flag.FlagSet, name string) flag.NormalizedName) {
224         c.Flags().SetNormalizeFunc(n)
225         c.PersistentFlags().SetNormalizeFunc(n)
226         c.globNormFunc = n
227
228         for _, command := range c.commands {
229                 command.SetGlobalNormalizationFunc(n)
230         }
231 }
232
233 // OutOrStdout returns output to stdout.
234 func (c *Command) OutOrStdout() io.Writer {
235         return c.getOut(os.Stdout)
236 }
237
238 // OutOrStderr returns output to stderr
239 func (c *Command) OutOrStderr() io.Writer {
240         return c.getOut(os.Stderr)
241 }
242
243 func (c *Command) getOut(def io.Writer) io.Writer {
244         if c.output != nil {
245                 return c.output
246         }
247         if c.HasParent() {
248                 return c.parent.getOut(def)
249         }
250         return def
251 }
252
253 // UsageFunc returns either the function set by SetUsageFunc for this command
254 // or a parent, or it returns a default usage function.
255 func (c *Command) UsageFunc() (f func(*Command) error) {
256         if c.usageFunc != nil {
257                 return c.usageFunc
258         }
259         if c.HasParent() {
260                 return c.Parent().UsageFunc()
261         }
262         return func(c *Command) error {
263                 c.mergePersistentFlags()
264                 err := tmpl(c.OutOrStderr(), c.UsageTemplate(), c)
265                 if err != nil {
266                         c.Println(err)
267                 }
268                 return err
269         }
270 }
271
272 // Usage puts out the usage for the command.
273 // Used when a user provides invalid input.
274 // Can be defined by user by overriding UsageFunc.
275 func (c *Command) Usage() error {
276         return c.UsageFunc()(c)
277 }
278
279 // HelpFunc returns either the function set by SetHelpFunc for this command
280 // or a parent, or it returns a function with default help behavior.
281 func (c *Command) HelpFunc() func(*Command, []string) {
282         if c.helpFunc != nil {
283                 return c.helpFunc
284         }
285         if c.HasParent() {
286                 return c.Parent().HelpFunc()
287         }
288         return func(c *Command, a []string) {
289                 c.mergePersistentFlags()
290                 err := tmpl(c.OutOrStdout(), c.HelpTemplate(), c)
291                 if err != nil {
292                         c.Println(err)
293                 }
294         }
295 }
296
297 // Help puts out the help for the command.
298 // Used when a user calls help [command].
299 // Can be defined by user by overriding HelpFunc.
300 func (c *Command) Help() error {
301         c.HelpFunc()(c, []string{})
302         return nil
303 }
304
305 // UsageString return usage string.
306 func (c *Command) UsageString() string {
307         tmpOutput := c.output
308         bb := new(bytes.Buffer)
309         c.SetOutput(bb)
310         c.Usage()
311         c.output = tmpOutput
312         return bb.String()
313 }
314
315 // FlagErrorFunc returns either the function set by SetFlagErrorFunc for this
316 // command or a parent, or it returns a function which returns the original
317 // error.
318 func (c *Command) FlagErrorFunc() (f func(*Command, error) error) {
319         if c.flagErrorFunc != nil {
320                 return c.flagErrorFunc
321         }
322
323         if c.HasParent() {
324                 return c.parent.FlagErrorFunc()
325         }
326         return func(c *Command, err error) error {
327                 return err
328         }
329 }
330
331 var minUsagePadding = 25
332
333 // UsagePadding return padding for the usage.
334 func (c *Command) UsagePadding() int {
335         if c.parent == nil || minUsagePadding > c.parent.commandsMaxUseLen {
336                 return minUsagePadding
337         }
338         return c.parent.commandsMaxUseLen
339 }
340
341 var minCommandPathPadding = 11
342
343 // CommandPathPadding return padding for the command path.
344 func (c *Command) CommandPathPadding() int {
345         if c.parent == nil || minCommandPathPadding > c.parent.commandsMaxCommandPathLen {
346                 return minCommandPathPadding
347         }
348         return c.parent.commandsMaxCommandPathLen
349 }
350
351 var minNamePadding = 11
352
353 // NamePadding returns padding for the name.
354 func (c *Command) NamePadding() int {
355         if c.parent == nil || minNamePadding > c.parent.commandsMaxNameLen {
356                 return minNamePadding
357         }
358         return c.parent.commandsMaxNameLen
359 }
360
361 // UsageTemplate returns usage template for the command.
362 func (c *Command) UsageTemplate() string {
363         if c.usageTemplate != "" {
364                 return c.usageTemplate
365         }
366
367         if c.HasParent() {
368                 return c.parent.UsageTemplate()
369         }
370         return `Usage:{{if .Runnable}}
371   {{.UseLine}}{{end}}{{if .HasAvailableSubCommands}}
372   {{.CommandPath}} [command]{{end}}{{if gt (len .Aliases) 0}}
373
374 Aliases:
375   {{.NameAndAliases}}{{end}}{{if .HasExample}}
376
377 Examples:
378 {{.Example}}{{end}}{{if .HasAvailableSubCommands}}
379
380 Available Commands:{{range .Commands}}{{if (or .IsAvailableCommand (eq .Name "help"))}}
381   {{rpad .Name .NamePadding }} {{.Short}}{{end}}{{end}}{{end}}{{if .HasAvailableLocalFlags}}
382
383 Flags:
384 {{.LocalFlags.FlagUsages | trimTrailingWhitespaces}}{{end}}{{if .HasAvailableInheritedFlags}}
385
386 Global Flags:
387 {{.InheritedFlags.FlagUsages | trimTrailingWhitespaces}}{{end}}{{if .HasHelpSubCommands}}
388
389 Additional help topics:{{range .Commands}}{{if .IsAdditionalHelpTopicCommand}}
390   {{rpad .CommandPath .CommandPathPadding}} {{.Short}}{{end}}{{end}}{{end}}{{if .HasAvailableSubCommands}}
391
392 Use "{{.CommandPath}} [command] --help" for more information about a command.{{end}}
393 `
394 }
395
396 // HelpTemplate return help template for the command.
397 func (c *Command) HelpTemplate() string {
398         if c.helpTemplate != "" {
399                 return c.helpTemplate
400         }
401
402         if c.HasParent() {
403                 return c.parent.HelpTemplate()
404         }
405         return `{{with (or .Long .Short)}}{{. | trimTrailingWhitespaces}}
406
407 {{end}}{{if or .Runnable .HasSubCommands}}{{.UsageString}}{{end}}`
408 }
409
410 func hasNoOptDefVal(name string, fs *flag.FlagSet) bool {
411         flag := fs.Lookup(name)
412         if flag == nil {
413                 return false
414         }
415         return flag.NoOptDefVal != ""
416 }
417
418 func shortHasNoOptDefVal(name string, fs *flag.FlagSet) bool {
419         if len(name) == 0 {
420                 return false
421         }
422
423         flag := fs.ShorthandLookup(name[:1])
424         if flag == nil {
425                 return false
426         }
427         return flag.NoOptDefVal != ""
428 }
429
430 func stripFlags(args []string, c *Command) []string {
431         if len(args) == 0 {
432                 return args
433         }
434         c.mergePersistentFlags()
435
436         commands := []string{}
437         flags := c.Flags()
438
439 Loop:
440         for len(args) > 0 {
441                 s := args[0]
442                 args = args[1:]
443                 switch {
444                 case strings.HasPrefix(s, "--") && !strings.Contains(s, "=") && !hasNoOptDefVal(s[2:], flags):
445                         // If '--flag arg' then
446                         // delete arg from args.
447                         fallthrough // (do the same as below)
448                 case strings.HasPrefix(s, "-") && !strings.Contains(s, "=") && len(s) == 2 && !shortHasNoOptDefVal(s[1:], flags):
449                         // If '-f arg' then
450                         // delete 'arg' from args or break the loop if len(args) <= 1.
451                         if len(args) <= 1 {
452                                 break Loop
453                         } else {
454                                 args = args[1:]
455                                 continue
456                         }
457                 case s != "" && !strings.HasPrefix(s, "-"):
458                         commands = append(commands, s)
459                 }
460         }
461
462         return commands
463 }
464
465 // argsMinusFirstX removes only the first x from args.  Otherwise, commands that look like
466 // openshift admin policy add-role-to-user admin my-user, lose the admin argument (arg[4]).
467 func argsMinusFirstX(args []string, x string) []string {
468         for i, y := range args {
469                 if x == y {
470                         ret := []string{}
471                         ret = append(ret, args[:i]...)
472                         ret = append(ret, args[i+1:]...)
473                         return ret
474                 }
475         }
476         return args
477 }
478
479 func isFlagArg(arg string) bool {
480         return ((len(arg) >= 3 && arg[1] == '-') ||
481                 (len(arg) >= 2 && arg[0] == '-' && arg[1] != '-'))
482 }
483
484 // Find the target command given the args and command tree
485 // Meant to be run on the highest node. Only searches down.
486 func (c *Command) Find(args []string) (*Command, []string, error) {
487         var innerfind func(*Command, []string) (*Command, []string)
488
489         innerfind = func(c *Command, innerArgs []string) (*Command, []string) {
490                 argsWOflags := stripFlags(innerArgs, c)
491                 if len(argsWOflags) == 0 {
492                         return c, innerArgs
493                 }
494                 nextSubCmd := argsWOflags[0]
495
496                 cmd := c.findNext(nextSubCmd)
497                 if cmd != nil {
498                         return innerfind(cmd, argsMinusFirstX(innerArgs, nextSubCmd))
499                 }
500                 return c, innerArgs
501         }
502
503         commandFound, a := innerfind(c, args)
504         if commandFound.Args == nil {
505                 return commandFound, a, legacyArgs(commandFound, stripFlags(a, commandFound))
506         }
507         return commandFound, a, nil
508 }
509
510 func (c *Command) findSuggestions(arg string) string {
511         if c.DisableSuggestions {
512                 return ""
513         }
514         if c.SuggestionsMinimumDistance <= 0 {
515                 c.SuggestionsMinimumDistance = 2
516         }
517         suggestionsString := ""
518         if suggestions := c.SuggestionsFor(arg); len(suggestions) > 0 {
519                 suggestionsString += "\n\nDid you mean this?\n"
520                 for _, s := range suggestions {
521                         suggestionsString += fmt.Sprintf("\t%v\n", s)
522                 }
523         }
524         return suggestionsString
525 }
526
527 func (c *Command) findNext(next string) *Command {
528         matches := make([]*Command, 0)
529         for _, cmd := range c.commands {
530                 if cmd.Name() == next || cmd.HasAlias(next) {
531                         return cmd
532                 }
533                 if EnablePrefixMatching && cmd.hasNameOrAliasPrefix(next) {
534                         matches = append(matches, cmd)
535                 }
536         }
537
538         if len(matches) == 1 {
539                 return matches[0]
540         }
541         return nil
542 }
543
544 // Traverse the command tree to find the command, and parse args for
545 // each parent.
546 func (c *Command) Traverse(args []string) (*Command, []string, error) {
547         flags := []string{}
548         inFlag := false
549
550         for i, arg := range args {
551                 switch {
552                 // A long flag with a space separated value
553                 case strings.HasPrefix(arg, "--") && !strings.Contains(arg, "="):
554                         // TODO: this isn't quite right, we should really check ahead for 'true' or 'false'
555                         inFlag = !hasNoOptDefVal(arg[2:], c.Flags())
556                         flags = append(flags, arg)
557                         continue
558                 // A short flag with a space separated value
559                 case strings.HasPrefix(arg, "-") && !strings.Contains(arg, "=") && len(arg) == 2 && !shortHasNoOptDefVal(arg[1:], c.Flags()):
560                         inFlag = true
561                         flags = append(flags, arg)
562                         continue
563                 // The value for a flag
564                 case inFlag:
565                         inFlag = false
566                         flags = append(flags, arg)
567                         continue
568                 // A flag without a value, or with an `=` separated value
569                 case isFlagArg(arg):
570                         flags = append(flags, arg)
571                         continue
572                 }
573
574                 cmd := c.findNext(arg)
575                 if cmd == nil {
576                         return c, args, nil
577                 }
578
579                 if err := c.ParseFlags(flags); err != nil {
580                         return nil, args, err
581                 }
582                 return cmd.Traverse(args[i+1:])
583         }
584         return c, args, nil
585 }
586
587 // SuggestionsFor provides suggestions for the typedName.
588 func (c *Command) SuggestionsFor(typedName string) []string {
589         suggestions := []string{}
590         for _, cmd := range c.commands {
591                 if cmd.IsAvailableCommand() {
592                         levenshteinDistance := ld(typedName, cmd.Name(), true)
593                         suggestByLevenshtein := levenshteinDistance <= c.SuggestionsMinimumDistance
594                         suggestByPrefix := strings.HasPrefix(strings.ToLower(cmd.Name()), strings.ToLower(typedName))
595                         if suggestByLevenshtein || suggestByPrefix {
596                                 suggestions = append(suggestions, cmd.Name())
597                         }
598                         for _, explicitSuggestion := range cmd.SuggestFor {
599                                 if strings.EqualFold(typedName, explicitSuggestion) {
600                                         suggestions = append(suggestions, cmd.Name())
601                                 }
602                         }
603                 }
604         }
605         return suggestions
606 }
607
608 // VisitParents visits all parents of the command and invokes fn on each parent.
609 func (c *Command) VisitParents(fn func(*Command)) {
610         if c.HasParent() {
611                 fn(c.Parent())
612                 c.Parent().VisitParents(fn)
613         }
614 }
615
616 // Root finds root command.
617 func (c *Command) Root() *Command {
618         if c.HasParent() {
619                 return c.Parent().Root()
620         }
621         return c
622 }
623
624 // ArgsLenAtDash will return the length of f.Args at the moment when a -- was
625 // found during arg parsing. This allows your program to know which args were
626 // before the -- and which came after. (Description from
627 // https://godoc.org/github.com/spf13/pflag#FlagSet.ArgsLenAtDash).
628 func (c *Command) ArgsLenAtDash() int {
629         return c.Flags().ArgsLenAtDash()
630 }
631
632 func (c *Command) execute(a []string) (err error) {
633         if c == nil {
634                 return fmt.Errorf("Called Execute() on a nil Command")
635         }
636
637         if len(c.Deprecated) > 0 {
638                 c.Printf("Command %q is deprecated, %s\n", c.Name(), c.Deprecated)
639         }
640
641         // initialize help flag as the last point possible to allow for user
642         // overriding
643         c.InitDefaultHelpFlag()
644
645         err = c.ParseFlags(a)
646         if err != nil {
647                 return c.FlagErrorFunc()(c, err)
648         }
649
650         // If help is called, regardless of other flags, return we want help.
651         // Also say we need help if the command isn't runnable.
652         helpVal, err := c.Flags().GetBool("help")
653         if err != nil {
654                 // should be impossible to get here as we always declare a help
655                 // flag in InitDefaultHelpFlag()
656                 c.Println("\"help\" flag declared as non-bool. Please correct your code")
657                 return err
658         }
659
660         if helpVal || !c.Runnable() {
661                 return flag.ErrHelp
662         }
663
664         c.preRun()
665
666         argWoFlags := c.Flags().Args()
667         if c.DisableFlagParsing {
668                 argWoFlags = a
669         }
670
671         if err := c.ValidateArgs(argWoFlags); err != nil {
672                 return err
673         }
674
675         for p := c; p != nil; p = p.Parent() {
676                 if p.PersistentPreRunE != nil {
677                         if err := p.PersistentPreRunE(c, argWoFlags); err != nil {
678                                 return err
679                         }
680                         break
681                 } else if p.PersistentPreRun != nil {
682                         p.PersistentPreRun(c, argWoFlags)
683                         break
684                 }
685         }
686         if c.PreRunE != nil {
687                 if err := c.PreRunE(c, argWoFlags); err != nil {
688                         return err
689                 }
690         } else if c.PreRun != nil {
691                 c.PreRun(c, argWoFlags)
692         }
693
694         if err := c.validateRequiredFlags(); err != nil {
695                 return err
696         }
697         if c.RunE != nil {
698                 if err := c.RunE(c, argWoFlags); err != nil {
699                         return err
700                 }
701         } else {
702                 c.Run(c, argWoFlags)
703         }
704         if c.PostRunE != nil {
705                 if err := c.PostRunE(c, argWoFlags); err != nil {
706                         return err
707                 }
708         } else if c.PostRun != nil {
709                 c.PostRun(c, argWoFlags)
710         }
711         for p := c; p != nil; p = p.Parent() {
712                 if p.PersistentPostRunE != nil {
713                         if err := p.PersistentPostRunE(c, argWoFlags); err != nil {
714                                 return err
715                         }
716                         break
717                 } else if p.PersistentPostRun != nil {
718                         p.PersistentPostRun(c, argWoFlags)
719                         break
720                 }
721         }
722
723         return nil
724 }
725
726 func (c *Command) preRun() {
727         for _, x := range initializers {
728                 x()
729         }
730 }
731
732 // Execute uses the args (os.Args[1:] by default)
733 // and run through the command tree finding appropriate matches
734 // for commands and then corresponding flags.
735 func (c *Command) Execute() error {
736         _, err := c.ExecuteC()
737         return err
738 }
739
740 // ExecuteC executes the command.
741 func (c *Command) ExecuteC() (cmd *Command, err error) {
742         // Regardless of what command execute is called on, run on Root only
743         if c.HasParent() {
744                 return c.Root().ExecuteC()
745         }
746
747         // windows hook
748         if preExecHookFn != nil {
749                 preExecHookFn(c)
750         }
751
752         // initialize help as the last point possible to allow for user
753         // overriding
754         c.InitDefaultHelpCmd()
755
756         var args []string
757
758         // Workaround FAIL with "go test -v" or "cobra.test -test.v", see #155
759         if c.args == nil && filepath.Base(os.Args[0]) != "cobra.test" {
760                 args = os.Args[1:]
761         } else {
762                 args = c.args
763         }
764
765         var flags []string
766         if c.TraverseChildren {
767                 cmd, flags, err = c.Traverse(args)
768         } else {
769                 cmd, flags, err = c.Find(args)
770         }
771         if err != nil {
772                 // If found parse to a subcommand and then failed, talk about the subcommand
773                 if cmd != nil {
774                         c = cmd
775                 }
776                 if !c.SilenceErrors {
777                         c.Println("Error:", err.Error())
778                         c.Printf("Run '%v --help' for usage.\n", c.CommandPath())
779                 }
780                 return c, err
781         }
782
783         err = cmd.execute(flags)
784         if err != nil {
785                 // Always show help if requested, even if SilenceErrors is in
786                 // effect
787                 if err == flag.ErrHelp {
788                         cmd.HelpFunc()(cmd, args)
789                         return cmd, nil
790                 }
791
792                 // If root command has SilentErrors flagged,
793                 // all subcommands should respect it
794                 if !cmd.SilenceErrors && !c.SilenceErrors {
795                         c.Println("Error:", err.Error())
796                 }
797
798                 // If root command has SilentUsage flagged,
799                 // all subcommands should respect it
800                 if !cmd.SilenceUsage && !c.SilenceUsage {
801                         c.Println(cmd.UsageString())
802                 }
803         }
804         return cmd, err
805 }
806
807 func (c *Command) ValidateArgs(args []string) error {
808         if c.Args == nil {
809                 return nil
810         }
811         return c.Args(c, args)
812 }
813
814 func (c *Command) validateRequiredFlags() error {
815         flags := c.Flags()
816         missingFlagNames := []string{}
817         flags.VisitAll(func(pflag *flag.Flag) {
818                 requiredAnnotation, found := pflag.Annotations[BashCompOneRequiredFlag]
819                 if !found {
820                         return
821                 }
822                 if (requiredAnnotation[0] == "true") && !pflag.Changed {
823                         missingFlagNames = append(missingFlagNames, pflag.Name)
824                 }
825         })
826
827         if len(missingFlagNames) > 0 {
828                 return fmt.Errorf(`Required flag(s) "%s" have/has not been set`, strings.Join(missingFlagNames, `", "`))
829         }
830         return nil
831 }
832
833 // InitDefaultHelpFlag adds default help flag to c.
834 // It is called automatically by executing the c or by calling help and usage.
835 // If c already has help flag, it will do nothing.
836 func (c *Command) InitDefaultHelpFlag() {
837         c.mergePersistentFlags()
838         if c.Flags().Lookup("help") == nil {
839                 usage := "help for "
840                 if c.Name() == "" {
841                         usage += "this command"
842                 } else {
843                         usage += c.Name()
844                 }
845                 c.Flags().BoolP("help", "h", false, usage)
846         }
847 }
848
849 // InitDefaultHelpCmd adds default help command to c.
850 // It is called automatically by executing the c or by calling help and usage.
851 // If c already has help command or c has no subcommands, it will do nothing.
852 func (c *Command) InitDefaultHelpCmd() {
853         if !c.HasSubCommands() {
854                 return
855         }
856
857         if c.helpCommand == nil {
858                 c.helpCommand = &Command{
859                         Use:   "help [command]",
860                         Short: "Help about any command",
861                         Long: `Help provides help for any command in the application.
862 Simply type ` + c.Name() + ` help [path to command] for full details.`,
863
864                         Run: func(c *Command, args []string) {
865                                 cmd, _, e := c.Root().Find(args)
866                                 if cmd == nil || e != nil {
867                                         c.Printf("Unknown help topic %#q\n", args)
868                                         c.Root().Usage()
869                                 } else {
870                                         cmd.InitDefaultHelpFlag() // make possible 'help' flag to be shown
871                                         cmd.Help()
872                                 }
873                         },
874                 }
875         }
876         c.RemoveCommand(c.helpCommand)
877         c.AddCommand(c.helpCommand)
878 }
879
880 // ResetCommands used for testing.
881 func (c *Command) ResetCommands() {
882         c.parent = nil
883         c.commands = nil
884         c.helpCommand = nil
885         c.parentsPflags = nil
886 }
887
888 // Sorts commands by their names.
889 type commandSorterByName []*Command
890
891 func (c commandSorterByName) Len() int           { return len(c) }
892 func (c commandSorterByName) Swap(i, j int)      { c[i], c[j] = c[j], c[i] }
893 func (c commandSorterByName) Less(i, j int) bool { return c[i].Name() < c[j].Name() }
894
895 // Commands returns a sorted slice of child commands.
896 func (c *Command) Commands() []*Command {
897         // do not sort commands if it already sorted or sorting was disabled
898         if EnableCommandSorting && !c.commandsAreSorted {
899                 sort.Sort(commandSorterByName(c.commands))
900                 c.commandsAreSorted = true
901         }
902         return c.commands
903 }
904
905 // AddCommand adds one or more commands to this parent command.
906 func (c *Command) AddCommand(cmds ...*Command) {
907         for i, x := range cmds {
908                 if cmds[i] == c {
909                         panic("Command can't be a child of itself")
910                 }
911                 cmds[i].parent = c
912                 // update max lengths
913                 usageLen := len(x.Use)
914                 if usageLen > c.commandsMaxUseLen {
915                         c.commandsMaxUseLen = usageLen
916                 }
917                 commandPathLen := len(x.CommandPath())
918                 if commandPathLen > c.commandsMaxCommandPathLen {
919                         c.commandsMaxCommandPathLen = commandPathLen
920                 }
921                 nameLen := len(x.Name())
922                 if nameLen > c.commandsMaxNameLen {
923                         c.commandsMaxNameLen = nameLen
924                 }
925                 // If global normalization function exists, update all children
926                 if c.globNormFunc != nil {
927                         x.SetGlobalNormalizationFunc(c.globNormFunc)
928                 }
929                 c.commands = append(c.commands, x)
930                 c.commandsAreSorted = false
931         }
932 }
933
934 // RemoveCommand removes one or more commands from a parent command.
935 func (c *Command) RemoveCommand(cmds ...*Command) {
936         commands := []*Command{}
937 main:
938         for _, command := range c.commands {
939                 for _, cmd := range cmds {
940                         if command == cmd {
941                                 command.parent = nil
942                                 continue main
943                         }
944                 }
945                 commands = append(commands, command)
946         }
947         c.commands = commands
948         // recompute all lengths
949         c.commandsMaxUseLen = 0
950         c.commandsMaxCommandPathLen = 0
951         c.commandsMaxNameLen = 0
952         for _, command := range c.commands {
953                 usageLen := len(command.Use)
954                 if usageLen > c.commandsMaxUseLen {
955                         c.commandsMaxUseLen = usageLen
956                 }
957                 commandPathLen := len(command.CommandPath())
958                 if commandPathLen > c.commandsMaxCommandPathLen {
959                         c.commandsMaxCommandPathLen = commandPathLen
960                 }
961                 nameLen := len(command.Name())
962                 if nameLen > c.commandsMaxNameLen {
963                         c.commandsMaxNameLen = nameLen
964                 }
965         }
966 }
967
968 // Print is a convenience method to Print to the defined output, fallback to Stderr if not set.
969 func (c *Command) Print(i ...interface{}) {
970         fmt.Fprint(c.OutOrStderr(), i...)
971 }
972
973 // Println is a convenience method to Println to the defined output, fallback to Stderr if not set.
974 func (c *Command) Println(i ...interface{}) {
975         c.Print(fmt.Sprintln(i...))
976 }
977
978 // Printf is a convenience method to Printf to the defined output, fallback to Stderr if not set.
979 func (c *Command) Printf(format string, i ...interface{}) {
980         c.Print(fmt.Sprintf(format, i...))
981 }
982
983 // CommandPath returns the full path to this command.
984 func (c *Command) CommandPath() string {
985         if c.HasParent() {
986                 return c.Parent().CommandPath() + " " + c.Name()
987         }
988         return c.Name()
989 }
990
991 // UseLine puts out the full usage for a given command (including parents).
992 func (c *Command) UseLine() string {
993         var useline string
994         if c.HasParent() {
995                 useline = c.parent.CommandPath() + " " + c.Use
996         } else {
997                 useline = c.Use
998         }
999         if c.HasAvailableFlags() && !strings.Contains(useline, "[flags]") {
1000                 useline += " [flags]"
1001         }
1002         return useline
1003 }
1004
1005 // DebugFlags used to determine which flags have been assigned to which commands
1006 // and which persist.
1007 func (c *Command) DebugFlags() {
1008         c.Println("DebugFlags called on", c.Name())
1009         var debugflags func(*Command)
1010
1011         debugflags = func(x *Command) {
1012                 if x.HasFlags() || x.HasPersistentFlags() {
1013                         c.Println(x.Name())
1014                 }
1015                 if x.HasFlags() {
1016                         x.flags.VisitAll(func(f *flag.Flag) {
1017                                 if x.HasPersistentFlags() && x.persistentFlag(f.Name) != nil {
1018                                         c.Println("  -"+f.Shorthand+",", "--"+f.Name, "["+f.DefValue+"]", "", f.Value, "  [LP]")
1019                                 } else {
1020                                         c.Println("  -"+f.Shorthand+",", "--"+f.Name, "["+f.DefValue+"]", "", f.Value, "  [L]")
1021                                 }
1022                         })
1023                 }
1024                 if x.HasPersistentFlags() {
1025                         x.pflags.VisitAll(func(f *flag.Flag) {
1026                                 if x.HasFlags() {
1027                                         if x.flags.Lookup(f.Name) == nil {
1028                                                 c.Println("  -"+f.Shorthand+",", "--"+f.Name, "["+f.DefValue+"]", "", f.Value, "  [P]")
1029                                         }
1030                                 } else {
1031                                         c.Println("  -"+f.Shorthand+",", "--"+f.Name, "["+f.DefValue+"]", "", f.Value, "  [P]")
1032                                 }
1033                         })
1034                 }
1035                 c.Println(x.flagErrorBuf)
1036                 if x.HasSubCommands() {
1037                         for _, y := range x.commands {
1038                                 debugflags(y)
1039                         }
1040                 }
1041         }
1042
1043         debugflags(c)
1044 }
1045
1046 // Name returns the command's name: the first word in the use line.
1047 func (c *Command) Name() string {
1048         name := c.Use
1049         i := strings.Index(name, " ")
1050         if i >= 0 {
1051                 name = name[:i]
1052         }
1053         return name
1054 }
1055
1056 // HasAlias determines if a given string is an alias of the command.
1057 func (c *Command) HasAlias(s string) bool {
1058         for _, a := range c.Aliases {
1059                 if a == s {
1060                         return true
1061                 }
1062         }
1063         return false
1064 }
1065
1066 // hasNameOrAliasPrefix returns true if the Name or any of aliases start
1067 // with prefix
1068 func (c *Command) hasNameOrAliasPrefix(prefix string) bool {
1069         if strings.HasPrefix(c.Name(), prefix) {
1070                 return true
1071         }
1072         for _, alias := range c.Aliases {
1073                 if strings.HasPrefix(alias, prefix) {
1074                         return true
1075                 }
1076         }
1077         return false
1078 }
1079
1080 // NameAndAliases returns a list of the command name and all aliases
1081 func (c *Command) NameAndAliases() string {
1082         return strings.Join(append([]string{c.Name()}, c.Aliases...), ", ")
1083 }
1084
1085 // HasExample determines if the command has example.
1086 func (c *Command) HasExample() bool {
1087         return len(c.Example) > 0
1088 }
1089
1090 // Runnable determines if the command is itself runnable.
1091 func (c *Command) Runnable() bool {
1092         return c.Run != nil || c.RunE != nil
1093 }
1094
1095 // HasSubCommands determines if the command has children commands.
1096 func (c *Command) HasSubCommands() bool {
1097         return len(c.commands) > 0
1098 }
1099
1100 // IsAvailableCommand determines if a command is available as a non-help command
1101 // (this includes all non deprecated/hidden commands).
1102 func (c *Command) IsAvailableCommand() bool {
1103         if len(c.Deprecated) != 0 || c.Hidden {
1104                 return false
1105         }
1106
1107         if c.HasParent() && c.Parent().helpCommand == c {
1108                 return false
1109         }
1110
1111         if c.Runnable() || c.HasAvailableSubCommands() {
1112                 return true
1113         }
1114
1115         return false
1116 }
1117
1118 // IsAdditionalHelpTopicCommand determines if a command is an additional
1119 // help topic command; additional help topic command is determined by the
1120 // fact that it is NOT runnable/hidden/deprecated, and has no sub commands that
1121 // are runnable/hidden/deprecated.
1122 // Concrete example: https://github.com/spf13/cobra/issues/393#issuecomment-282741924.
1123 func (c *Command) IsAdditionalHelpTopicCommand() bool {
1124         // if a command is runnable, deprecated, or hidden it is not a 'help' command
1125         if c.Runnable() || len(c.Deprecated) != 0 || c.Hidden {
1126                 return false
1127         }
1128
1129         // if any non-help sub commands are found, the command is not a 'help' command
1130         for _, sub := range c.commands {
1131                 if !sub.IsAdditionalHelpTopicCommand() {
1132                         return false
1133                 }
1134         }
1135
1136         // the command either has no sub commands, or no non-help sub commands
1137         return true
1138 }
1139
1140 // HasHelpSubCommands determines if a command has any available 'help' sub commands
1141 // that need to be shown in the usage/help default template under 'additional help
1142 // topics'.
1143 func (c *Command) HasHelpSubCommands() bool {
1144         // return true on the first found available 'help' sub command
1145         for _, sub := range c.commands {
1146                 if sub.IsAdditionalHelpTopicCommand() {
1147                         return true
1148                 }
1149         }
1150
1151         // the command either has no sub commands, or no available 'help' sub commands
1152         return false
1153 }
1154
1155 // HasAvailableSubCommands determines if a command has available sub commands that
1156 // need to be shown in the usage/help default template under 'available commands'.
1157 func (c *Command) HasAvailableSubCommands() bool {
1158         // return true on the first found available (non deprecated/help/hidden)
1159         // sub command
1160         for _, sub := range c.commands {
1161                 if sub.IsAvailableCommand() {
1162                         return true
1163                 }
1164         }
1165
1166         // the command either has no sub comamnds, or no available (non deprecated/help/hidden)
1167         // sub commands
1168         return false
1169 }
1170
1171 // HasParent determines if the command is a child command.
1172 func (c *Command) HasParent() bool {
1173         return c.parent != nil
1174 }
1175
1176 // GlobalNormalizationFunc returns the global normalization function or nil if doesn't exists.
1177 func (c *Command) GlobalNormalizationFunc() func(f *flag.FlagSet, name string) flag.NormalizedName {
1178         return c.globNormFunc
1179 }
1180
1181 // Flags returns the complete FlagSet that applies
1182 // to this command (local and persistent declared here and by all parents).
1183 func (c *Command) Flags() *flag.FlagSet {
1184         if c.flags == nil {
1185                 c.flags = flag.NewFlagSet(c.Name(), flag.ContinueOnError)
1186                 if c.flagErrorBuf == nil {
1187                         c.flagErrorBuf = new(bytes.Buffer)
1188                 }
1189                 c.flags.SetOutput(c.flagErrorBuf)
1190         }
1191
1192         return c.flags
1193 }
1194
1195 // LocalNonPersistentFlags are flags specific to this command which will NOT persist to subcommands.
1196 func (c *Command) LocalNonPersistentFlags() *flag.FlagSet {
1197         persistentFlags := c.PersistentFlags()
1198
1199         out := flag.NewFlagSet(c.Name(), flag.ContinueOnError)
1200         c.LocalFlags().VisitAll(func(f *flag.Flag) {
1201                 if persistentFlags.Lookup(f.Name) == nil {
1202                         out.AddFlag(f)
1203                 }
1204         })
1205         return out
1206 }
1207
1208 // LocalFlags returns the local FlagSet specifically set in the current command.
1209 func (c *Command) LocalFlags() *flag.FlagSet {
1210         c.mergePersistentFlags()
1211
1212         if c.lflags == nil {
1213                 c.lflags = flag.NewFlagSet(c.Name(), flag.ContinueOnError)
1214                 if c.flagErrorBuf == nil {
1215                         c.flagErrorBuf = new(bytes.Buffer)
1216                 }
1217                 c.lflags.SetOutput(c.flagErrorBuf)
1218         }
1219         c.lflags.SortFlags = c.Flags().SortFlags
1220         if c.globNormFunc != nil {
1221                 c.lflags.SetNormalizeFunc(c.globNormFunc)
1222         }
1223
1224         addToLocal := func(f *flag.Flag) {
1225                 if c.lflags.Lookup(f.Name) == nil && c.parentsPflags.Lookup(f.Name) == nil {
1226                         c.lflags.AddFlag(f)
1227                 }
1228         }
1229         c.Flags().VisitAll(addToLocal)
1230         c.PersistentFlags().VisitAll(addToLocal)
1231         return c.lflags
1232 }
1233
1234 // InheritedFlags returns all flags which were inherited from parents commands.
1235 func (c *Command) InheritedFlags() *flag.FlagSet {
1236         c.mergePersistentFlags()
1237
1238         if c.iflags == nil {
1239                 c.iflags = flag.NewFlagSet(c.Name(), flag.ContinueOnError)
1240                 if c.flagErrorBuf == nil {
1241                         c.flagErrorBuf = new(bytes.Buffer)
1242                 }
1243                 c.iflags.SetOutput(c.flagErrorBuf)
1244         }
1245
1246         local := c.LocalFlags()
1247         if c.globNormFunc != nil {
1248                 c.iflags.SetNormalizeFunc(c.globNormFunc)
1249         }
1250
1251         c.parentsPflags.VisitAll(func(f *flag.Flag) {
1252                 if c.iflags.Lookup(f.Name) == nil && local.Lookup(f.Name) == nil {
1253                         c.iflags.AddFlag(f)
1254                 }
1255         })
1256         return c.iflags
1257 }
1258
1259 // NonInheritedFlags returns all flags which were not inherited from parent commands.
1260 func (c *Command) NonInheritedFlags() *flag.FlagSet {
1261         return c.LocalFlags()
1262 }
1263
1264 // PersistentFlags returns the persistent FlagSet specifically set in the current command.
1265 func (c *Command) PersistentFlags() *flag.FlagSet {
1266         if c.pflags == nil {
1267                 c.pflags = flag.NewFlagSet(c.Name(), flag.ContinueOnError)
1268                 if c.flagErrorBuf == nil {
1269                         c.flagErrorBuf = new(bytes.Buffer)
1270                 }
1271                 c.pflags.SetOutput(c.flagErrorBuf)
1272         }
1273         return c.pflags
1274 }
1275
1276 // ResetFlags is used in testing.
1277 func (c *Command) ResetFlags() {
1278         c.flagErrorBuf = new(bytes.Buffer)
1279         c.flagErrorBuf.Reset()
1280         c.flags = flag.NewFlagSet(c.Name(), flag.ContinueOnError)
1281         c.flags.SetOutput(c.flagErrorBuf)
1282         c.pflags = flag.NewFlagSet(c.Name(), flag.ContinueOnError)
1283         c.pflags.SetOutput(c.flagErrorBuf)
1284
1285         c.lflags = nil
1286         c.iflags = nil
1287         c.parentsPflags = nil
1288 }
1289
1290 // HasFlags checks if the command contains any flags (local plus persistent from the entire structure).
1291 func (c *Command) HasFlags() bool {
1292         return c.Flags().HasFlags()
1293 }
1294
1295 // HasPersistentFlags checks if the command contains persistent flags.
1296 func (c *Command) HasPersistentFlags() bool {
1297         return c.PersistentFlags().HasFlags()
1298 }
1299
1300 // HasLocalFlags checks if the command has flags specifically declared locally.
1301 func (c *Command) HasLocalFlags() bool {
1302         return c.LocalFlags().HasFlags()
1303 }
1304
1305 // HasInheritedFlags checks if the command has flags inherited from its parent command.
1306 func (c *Command) HasInheritedFlags() bool {
1307         return c.InheritedFlags().HasFlags()
1308 }
1309
1310 // HasAvailableFlags checks if the command contains any flags (local plus persistent from the entire
1311 // structure) which are not hidden or deprecated.
1312 func (c *Command) HasAvailableFlags() bool {
1313         return c.Flags().HasAvailableFlags()
1314 }
1315
1316 // HasAvailablePersistentFlags checks if the command contains persistent flags which are not hidden or deprecated.
1317 func (c *Command) HasAvailablePersistentFlags() bool {
1318         return c.PersistentFlags().HasAvailableFlags()
1319 }
1320
1321 // HasAvailableLocalFlags checks if the command has flags specifically declared locally which are not hidden
1322 // or deprecated.
1323 func (c *Command) HasAvailableLocalFlags() bool {
1324         return c.LocalFlags().HasAvailableFlags()
1325 }
1326
1327 // HasAvailableInheritedFlags checks if the command has flags inherited from its parent command which are
1328 // not hidden or deprecated.
1329 func (c *Command) HasAvailableInheritedFlags() bool {
1330         return c.InheritedFlags().HasAvailableFlags()
1331 }
1332
1333 // Flag climbs up the command tree looking for matching flag.
1334 func (c *Command) Flag(name string) (flag *flag.Flag) {
1335         flag = c.Flags().Lookup(name)
1336
1337         if flag == nil {
1338                 flag = c.persistentFlag(name)
1339         }
1340
1341         return
1342 }
1343
1344 // Recursively find matching persistent flag.
1345 func (c *Command) persistentFlag(name string) (flag *flag.Flag) {
1346         if c.HasPersistentFlags() {
1347                 flag = c.PersistentFlags().Lookup(name)
1348         }
1349
1350         if flag == nil {
1351                 c.updateParentsPflags()
1352                 flag = c.parentsPflags.Lookup(name)
1353         }
1354         return
1355 }
1356
1357 // ParseFlags parses persistent flag tree and local flags.
1358 func (c *Command) ParseFlags(args []string) error {
1359         if c.DisableFlagParsing {
1360                 return nil
1361         }
1362
1363         if c.flagErrorBuf == nil {
1364                 c.flagErrorBuf = new(bytes.Buffer)
1365         }
1366         beforeErrorBufLen := c.flagErrorBuf.Len()
1367         c.mergePersistentFlags()
1368         err := c.Flags().Parse(args)
1369         // Print warnings if they occurred (e.g. deprecated flag messages).
1370         if c.flagErrorBuf.Len()-beforeErrorBufLen > 0 && err == nil {
1371                 c.Print(c.flagErrorBuf.String())
1372         }
1373
1374         return err
1375 }
1376
1377 // Parent returns a commands parent command.
1378 func (c *Command) Parent() *Command {
1379         return c.parent
1380 }
1381
1382 // mergePersistentFlags merges c.PersistentFlags() to c.Flags()
1383 // and adds missing persistent flags of all parents.
1384 func (c *Command) mergePersistentFlags() {
1385         c.updateParentsPflags()
1386         c.Flags().AddFlagSet(c.PersistentFlags())
1387         c.Flags().AddFlagSet(c.parentsPflags)
1388 }
1389
1390 // updateParentsPflags updates c.parentsPflags by adding
1391 // new persistent flags of all parents.
1392 // If c.parentsPflags == nil, it makes new.
1393 func (c *Command) updateParentsPflags() {
1394         if c.parentsPflags == nil {
1395                 c.parentsPflags = flag.NewFlagSet(c.Name(), flag.ContinueOnError)
1396                 c.parentsPflags.SetOutput(c.flagErrorBuf)
1397                 c.parentsPflags.SortFlags = false
1398         }
1399
1400         if c.globNormFunc != nil {
1401                 c.parentsPflags.SetNormalizeFunc(c.globNormFunc)
1402         }
1403
1404         c.Root().PersistentFlags().AddFlagSet(flag.CommandLine)
1405
1406         c.VisitParents(func(parent *Command) {
1407                 c.parentsPflags.AddFlagSet(parent.PersistentFlags())
1408         })
1409 }