...

Source file src/github.com/spf13/cobra/command.go

Documentation: github.com/spf13/cobra

     1  // Copyright 2013-2023 The Cobra Authors
     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  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  // Package cobra is a commander providing a simple interface to create powerful modern CLI interfaces.
    16  // In addition to providing an interface, Cobra simultaneously provides a controller to organize your application code.
    17  package cobra
    18  
    19  import (
    20  	"bytes"
    21  	"context"
    22  	"errors"
    23  	"fmt"
    24  	"io"
    25  	"os"
    26  	"path/filepath"
    27  	"sort"
    28  	"strings"
    29  
    30  	flag "github.com/spf13/pflag"
    31  )
    32  
    33  const (
    34  	FlagSetByCobraAnnotation     = "cobra_annotation_flag_set_by_cobra"
    35  	CommandDisplayNameAnnotation = "cobra_annotation_command_display_name"
    36  )
    37  
    38  // FParseErrWhitelist configures Flag parse errors to be ignored
    39  type FParseErrWhitelist flag.ParseErrorsWhitelist
    40  
    41  // Group Structure to manage groups for commands
    42  type Group struct {
    43  	ID    string
    44  	Title string
    45  }
    46  
    47  // Command is just that, a command for your application.
    48  // E.g.  'go run ...' - 'run' is the command. Cobra requires
    49  // you to define the usage and description as part of your command
    50  // definition to ensure usability.
    51  type Command struct {
    52  	// Use is the one-line usage message.
    53  	// Recommended syntax is as follows:
    54  	//   [ ] identifies an optional argument. Arguments that are not enclosed in brackets are required.
    55  	//   ... indicates that you can specify multiple values for the previous argument.
    56  	//   |   indicates mutually exclusive information. You can use the argument to the left of the separator or the
    57  	//       argument to the right of the separator. You cannot use both arguments in a single use of the command.
    58  	//   { } delimits a set of mutually exclusive arguments when one of the arguments is required. If the arguments are
    59  	//       optional, they are enclosed in brackets ([ ]).
    60  	// Example: add [-F file | -D dir]... [-f format] profile
    61  	Use string
    62  
    63  	// Aliases is an array of aliases that can be used instead of the first word in Use.
    64  	Aliases []string
    65  
    66  	// SuggestFor is an array of command names for which this command will be suggested -
    67  	// similar to aliases but only suggests.
    68  	SuggestFor []string
    69  
    70  	// Short is the short description shown in the 'help' output.
    71  	Short string
    72  
    73  	// The group id under which this subcommand is grouped in the 'help' output of its parent.
    74  	GroupID string
    75  
    76  	// Long is the long message shown in the 'help <this-command>' output.
    77  	Long string
    78  
    79  	// Example is examples of how to use the command.
    80  	Example string
    81  
    82  	// ValidArgs is list of all valid non-flag arguments that are accepted in shell completions
    83  	ValidArgs []string
    84  	// ValidArgsFunction is an optional function that provides valid non-flag arguments for shell completion.
    85  	// It is a dynamic version of using ValidArgs.
    86  	// Only one of ValidArgs and ValidArgsFunction can be used for a command.
    87  	ValidArgsFunction func(cmd *Command, args []string, toComplete string) ([]string, ShellCompDirective)
    88  
    89  	// Expected arguments
    90  	Args PositionalArgs
    91  
    92  	// ArgAliases is List of aliases for ValidArgs.
    93  	// These are not suggested to the user in the shell completion,
    94  	// but accepted if entered manually.
    95  	ArgAliases []string
    96  
    97  	// BashCompletionFunction is custom bash functions used by the legacy bash autocompletion generator.
    98  	// For portability with other shells, it is recommended to instead use ValidArgsFunction
    99  	BashCompletionFunction string
   100  
   101  	// Deprecated defines, if this command is deprecated and should print this string when used.
   102  	Deprecated string
   103  
   104  	// Annotations are key/value pairs that can be used by applications to identify or
   105  	// group commands or set special options.
   106  	Annotations map[string]string
   107  
   108  	// Version defines the version for this command. If this value is non-empty and the command does not
   109  	// define a "version" flag, a "version" boolean flag will be added to the command and, if specified,
   110  	// will print content of the "Version" variable. A shorthand "v" flag will also be added if the
   111  	// command does not define one.
   112  	Version string
   113  
   114  	// The *Run functions are executed in the following order:
   115  	//   * PersistentPreRun()
   116  	//   * PreRun()
   117  	//   * Run()
   118  	//   * PostRun()
   119  	//   * PersistentPostRun()
   120  	// All functions get the same args, the arguments after the command name.
   121  	// The *PreRun and *PostRun functions will only be executed if the Run function of the current
   122  	// command has been declared.
   123  	//
   124  	// PersistentPreRun: children of this command will inherit and execute.
   125  	PersistentPreRun func(cmd *Command, args []string)
   126  	// PersistentPreRunE: PersistentPreRun but returns an error.
   127  	PersistentPreRunE func(cmd *Command, args []string) error
   128  	// PreRun: children of this command will not inherit.
   129  	PreRun func(cmd *Command, args []string)
   130  	// PreRunE: PreRun but returns an error.
   131  	PreRunE func(cmd *Command, args []string) error
   132  	// Run: Typically the actual work function. Most commands will only implement this.
   133  	Run func(cmd *Command, args []string)
   134  	// RunE: Run but returns an error.
   135  	RunE func(cmd *Command, args []string) error
   136  	// PostRun: run after the Run command.
   137  	PostRun func(cmd *Command, args []string)
   138  	// PostRunE: PostRun but returns an error.
   139  	PostRunE func(cmd *Command, args []string) error
   140  	// PersistentPostRun: children of this command will inherit and execute after PostRun.
   141  	PersistentPostRun func(cmd *Command, args []string)
   142  	// PersistentPostRunE: PersistentPostRun but returns an error.
   143  	PersistentPostRunE func(cmd *Command, args []string) error
   144  
   145  	// groups for subcommands
   146  	commandgroups []*Group
   147  
   148  	// args is actual args parsed from flags.
   149  	args []string
   150  	// flagErrorBuf contains all error messages from pflag.
   151  	flagErrorBuf *bytes.Buffer
   152  	// flags is full set of flags.
   153  	flags *flag.FlagSet
   154  	// pflags contains persistent flags.
   155  	pflags *flag.FlagSet
   156  	// lflags contains local flags.
   157  	lflags *flag.FlagSet
   158  	// iflags contains inherited flags.
   159  	iflags *flag.FlagSet
   160  	// parentsPflags is all persistent flags of cmd's parents.
   161  	parentsPflags *flag.FlagSet
   162  	// globNormFunc is the global normalization function
   163  	// that we can use on every pflag set and children commands
   164  	globNormFunc func(f *flag.FlagSet, name string) flag.NormalizedName
   165  
   166  	// usageFunc is usage func defined by user.
   167  	usageFunc func(*Command) error
   168  	// usageTemplate is usage template defined by user.
   169  	usageTemplate string
   170  	// flagErrorFunc is func defined by user and it's called when the parsing of
   171  	// flags returns an error.
   172  	flagErrorFunc func(*Command, error) error
   173  	// helpTemplate is help template defined by user.
   174  	helpTemplate string
   175  	// helpFunc is help func defined by user.
   176  	helpFunc func(*Command, []string)
   177  	// helpCommand is command with usage 'help'. If it's not defined by user,
   178  	// cobra uses default help command.
   179  	helpCommand *Command
   180  	// helpCommandGroupID is the group id for the helpCommand
   181  	helpCommandGroupID string
   182  
   183  	// completionCommandGroupID is the group id for the completion command
   184  	completionCommandGroupID string
   185  
   186  	// versionTemplate is the version template defined by user.
   187  	versionTemplate string
   188  
   189  	// errPrefix is the error message prefix defined by user.
   190  	errPrefix string
   191  
   192  	// inReader is a reader defined by the user that replaces stdin
   193  	inReader io.Reader
   194  	// outWriter is a writer defined by the user that replaces stdout
   195  	outWriter io.Writer
   196  	// errWriter is a writer defined by the user that replaces stderr
   197  	errWriter io.Writer
   198  
   199  	// FParseErrWhitelist flag parse errors to be ignored
   200  	FParseErrWhitelist FParseErrWhitelist
   201  
   202  	// CompletionOptions is a set of options to control the handling of shell completion
   203  	CompletionOptions CompletionOptions
   204  
   205  	// commandsAreSorted defines, if command slice are sorted or not.
   206  	commandsAreSorted bool
   207  	// commandCalledAs is the name or alias value used to call this command.
   208  	commandCalledAs struct {
   209  		name   string
   210  		called bool
   211  	}
   212  
   213  	ctx context.Context
   214  
   215  	// commands is the list of commands supported by this program.
   216  	commands []*Command
   217  	// parent is a parent command for this command.
   218  	parent *Command
   219  	// Max lengths of commands' string lengths for use in padding.
   220  	commandsMaxUseLen         int
   221  	commandsMaxCommandPathLen int
   222  	commandsMaxNameLen        int
   223  
   224  	// TraverseChildren parses flags on all parents before executing child command.
   225  	TraverseChildren bool
   226  
   227  	// Hidden defines, if this command is hidden and should NOT show up in the list of available commands.
   228  	Hidden bool
   229  
   230  	// SilenceErrors is an option to quiet errors down stream.
   231  	SilenceErrors bool
   232  
   233  	// SilenceUsage is an option to silence usage when an error occurs.
   234  	SilenceUsage bool
   235  
   236  	// DisableFlagParsing disables the flag parsing.
   237  	// If this is true all flags will be passed to the command as arguments.
   238  	DisableFlagParsing bool
   239  
   240  	// DisableAutoGenTag defines, if gen tag ("Auto generated by spf13/cobra...")
   241  	// will be printed by generating docs for this command.
   242  	DisableAutoGenTag bool
   243  
   244  	// DisableFlagsInUseLine will disable the addition of [flags] to the usage
   245  	// line of a command when printing help or generating docs
   246  	DisableFlagsInUseLine bool
   247  
   248  	// DisableSuggestions disables the suggestions based on Levenshtein distance
   249  	// that go along with 'unknown command' messages.
   250  	DisableSuggestions bool
   251  
   252  	// SuggestionsMinimumDistance defines minimum levenshtein distance to display suggestions.
   253  	// Must be > 0.
   254  	SuggestionsMinimumDistance int
   255  }
   256  
   257  // Context returns underlying command context. If command was executed
   258  // with ExecuteContext or the context was set with SetContext, the
   259  // previously set context will be returned. Otherwise, nil is returned.
   260  //
   261  // Notice that a call to Execute and ExecuteC will replace a nil context of
   262  // a command with a context.Background, so a background context will be
   263  // returned by Context after one of these functions has been called.
   264  func (c *Command) Context() context.Context {
   265  	return c.ctx
   266  }
   267  
   268  // SetContext sets context for the command. This context will be overwritten by
   269  // Command.ExecuteContext or Command.ExecuteContextC.
   270  func (c *Command) SetContext(ctx context.Context) {
   271  	c.ctx = ctx
   272  }
   273  
   274  // SetArgs sets arguments for the command. It is set to os.Args[1:] by default, if desired, can be overridden
   275  // particularly useful when testing.
   276  func (c *Command) SetArgs(a []string) {
   277  	c.args = a
   278  }
   279  
   280  // SetOutput sets the destination for usage and error messages.
   281  // If output is nil, os.Stderr is used.
   282  // Deprecated: Use SetOut and/or SetErr instead
   283  func (c *Command) SetOutput(output io.Writer) {
   284  	c.outWriter = output
   285  	c.errWriter = output
   286  }
   287  
   288  // SetOut sets the destination for usage messages.
   289  // If newOut is nil, os.Stdout is used.
   290  func (c *Command) SetOut(newOut io.Writer) {
   291  	c.outWriter = newOut
   292  }
   293  
   294  // SetErr sets the destination for error messages.
   295  // If newErr is nil, os.Stderr is used.
   296  func (c *Command) SetErr(newErr io.Writer) {
   297  	c.errWriter = newErr
   298  }
   299  
   300  // SetIn sets the source for input data
   301  // If newIn is nil, os.Stdin is used.
   302  func (c *Command) SetIn(newIn io.Reader) {
   303  	c.inReader = newIn
   304  }
   305  
   306  // SetUsageFunc sets usage function. Usage can be defined by application.
   307  func (c *Command) SetUsageFunc(f func(*Command) error) {
   308  	c.usageFunc = f
   309  }
   310  
   311  // SetUsageTemplate sets usage template. Can be defined by Application.
   312  func (c *Command) SetUsageTemplate(s string) {
   313  	c.usageTemplate = s
   314  }
   315  
   316  // SetFlagErrorFunc sets a function to generate an error when flag parsing
   317  // fails.
   318  func (c *Command) SetFlagErrorFunc(f func(*Command, error) error) {
   319  	c.flagErrorFunc = f
   320  }
   321  
   322  // SetHelpFunc sets help function. Can be defined by Application.
   323  func (c *Command) SetHelpFunc(f func(*Command, []string)) {
   324  	c.helpFunc = f
   325  }
   326  
   327  // SetHelpCommand sets help command.
   328  func (c *Command) SetHelpCommand(cmd *Command) {
   329  	c.helpCommand = cmd
   330  }
   331  
   332  // SetHelpCommandGroupID sets the group id of the help command.
   333  func (c *Command) SetHelpCommandGroupID(groupID string) {
   334  	if c.helpCommand != nil {
   335  		c.helpCommand.GroupID = groupID
   336  	}
   337  	// helpCommandGroupID is used if no helpCommand is defined by the user
   338  	c.helpCommandGroupID = groupID
   339  }
   340  
   341  // SetCompletionCommandGroupID sets the group id of the completion command.
   342  func (c *Command) SetCompletionCommandGroupID(groupID string) {
   343  	// completionCommandGroupID is used if no completion command is defined by the user
   344  	c.Root().completionCommandGroupID = groupID
   345  }
   346  
   347  // SetHelpTemplate sets help template to be used. Application can use it to set custom template.
   348  func (c *Command) SetHelpTemplate(s string) {
   349  	c.helpTemplate = s
   350  }
   351  
   352  // SetVersionTemplate sets version template to be used. Application can use it to set custom template.
   353  func (c *Command) SetVersionTemplate(s string) {
   354  	c.versionTemplate = s
   355  }
   356  
   357  // SetErrPrefix sets error message prefix to be used. Application can use it to set custom prefix.
   358  func (c *Command) SetErrPrefix(s string) {
   359  	c.errPrefix = s
   360  }
   361  
   362  // SetGlobalNormalizationFunc sets a normalization function to all flag sets and also to child commands.
   363  // The user should not have a cyclic dependency on commands.
   364  func (c *Command) SetGlobalNormalizationFunc(n func(f *flag.FlagSet, name string) flag.NormalizedName) {
   365  	c.Flags().SetNormalizeFunc(n)
   366  	c.PersistentFlags().SetNormalizeFunc(n)
   367  	c.globNormFunc = n
   368  
   369  	for _, command := range c.commands {
   370  		command.SetGlobalNormalizationFunc(n)
   371  	}
   372  }
   373  
   374  // OutOrStdout returns output to stdout.
   375  func (c *Command) OutOrStdout() io.Writer {
   376  	return c.getOut(os.Stdout)
   377  }
   378  
   379  // OutOrStderr returns output to stderr
   380  func (c *Command) OutOrStderr() io.Writer {
   381  	return c.getOut(os.Stderr)
   382  }
   383  
   384  // ErrOrStderr returns output to stderr
   385  func (c *Command) ErrOrStderr() io.Writer {
   386  	return c.getErr(os.Stderr)
   387  }
   388  
   389  // InOrStdin returns input to stdin
   390  func (c *Command) InOrStdin() io.Reader {
   391  	return c.getIn(os.Stdin)
   392  }
   393  
   394  func (c *Command) getOut(def io.Writer) io.Writer {
   395  	if c.outWriter != nil {
   396  		return c.outWriter
   397  	}
   398  	if c.HasParent() {
   399  		return c.parent.getOut(def)
   400  	}
   401  	return def
   402  }
   403  
   404  func (c *Command) getErr(def io.Writer) io.Writer {
   405  	if c.errWriter != nil {
   406  		return c.errWriter
   407  	}
   408  	if c.HasParent() {
   409  		return c.parent.getErr(def)
   410  	}
   411  	return def
   412  }
   413  
   414  func (c *Command) getIn(def io.Reader) io.Reader {
   415  	if c.inReader != nil {
   416  		return c.inReader
   417  	}
   418  	if c.HasParent() {
   419  		return c.parent.getIn(def)
   420  	}
   421  	return def
   422  }
   423  
   424  // UsageFunc returns either the function set by SetUsageFunc for this command
   425  // or a parent, or it returns a default usage function.
   426  func (c *Command) UsageFunc() (f func(*Command) error) {
   427  	if c.usageFunc != nil {
   428  		return c.usageFunc
   429  	}
   430  	if c.HasParent() {
   431  		return c.Parent().UsageFunc()
   432  	}
   433  	return func(c *Command) error {
   434  		c.mergePersistentFlags()
   435  		err := tmpl(c.OutOrStderr(), c.UsageTemplate(), c)
   436  		if err != nil {
   437  			c.PrintErrln(err)
   438  		}
   439  		return err
   440  	}
   441  }
   442  
   443  // Usage puts out the usage for the command.
   444  // Used when a user provides invalid input.
   445  // Can be defined by user by overriding UsageFunc.
   446  func (c *Command) Usage() error {
   447  	return c.UsageFunc()(c)
   448  }
   449  
   450  // HelpFunc returns either the function set by SetHelpFunc for this command
   451  // or a parent, or it returns a function with default help behavior.
   452  func (c *Command) HelpFunc() func(*Command, []string) {
   453  	if c.helpFunc != nil {
   454  		return c.helpFunc
   455  	}
   456  	if c.HasParent() {
   457  		return c.Parent().HelpFunc()
   458  	}
   459  	return func(c *Command, a []string) {
   460  		c.mergePersistentFlags()
   461  		// The help should be sent to stdout
   462  		// See https://github.com/spf13/cobra/issues/1002
   463  		err := tmpl(c.OutOrStdout(), c.HelpTemplate(), c)
   464  		if err != nil {
   465  			c.PrintErrln(err)
   466  		}
   467  	}
   468  }
   469  
   470  // Help puts out the help for the command.
   471  // Used when a user calls help [command].
   472  // Can be defined by user by overriding HelpFunc.
   473  func (c *Command) Help() error {
   474  	c.HelpFunc()(c, []string{})
   475  	return nil
   476  }
   477  
   478  // UsageString returns usage string.
   479  func (c *Command) UsageString() string {
   480  	// Storing normal writers
   481  	tmpOutput := c.outWriter
   482  	tmpErr := c.errWriter
   483  
   484  	bb := new(bytes.Buffer)
   485  	c.outWriter = bb
   486  	c.errWriter = bb
   487  
   488  	CheckErr(c.Usage())
   489  
   490  	// Setting things back to normal
   491  	c.outWriter = tmpOutput
   492  	c.errWriter = tmpErr
   493  
   494  	return bb.String()
   495  }
   496  
   497  // FlagErrorFunc returns either the function set by SetFlagErrorFunc for this
   498  // command or a parent, or it returns a function which returns the original
   499  // error.
   500  func (c *Command) FlagErrorFunc() (f func(*Command, error) error) {
   501  	if c.flagErrorFunc != nil {
   502  		return c.flagErrorFunc
   503  	}
   504  
   505  	if c.HasParent() {
   506  		return c.parent.FlagErrorFunc()
   507  	}
   508  	return func(c *Command, err error) error {
   509  		return err
   510  	}
   511  }
   512  
   513  var minUsagePadding = 25
   514  
   515  // UsagePadding return padding for the usage.
   516  func (c *Command) UsagePadding() int {
   517  	if c.parent == nil || minUsagePadding > c.parent.commandsMaxUseLen {
   518  		return minUsagePadding
   519  	}
   520  	return c.parent.commandsMaxUseLen
   521  }
   522  
   523  var minCommandPathPadding = 11
   524  
   525  // CommandPathPadding return padding for the command path.
   526  func (c *Command) CommandPathPadding() int {
   527  	if c.parent == nil || minCommandPathPadding > c.parent.commandsMaxCommandPathLen {
   528  		return minCommandPathPadding
   529  	}
   530  	return c.parent.commandsMaxCommandPathLen
   531  }
   532  
   533  var minNamePadding = 11
   534  
   535  // NamePadding returns padding for the name.
   536  func (c *Command) NamePadding() int {
   537  	if c.parent == nil || minNamePadding > c.parent.commandsMaxNameLen {
   538  		return minNamePadding
   539  	}
   540  	return c.parent.commandsMaxNameLen
   541  }
   542  
   543  // UsageTemplate returns usage template for the command.
   544  func (c *Command) UsageTemplate() string {
   545  	if c.usageTemplate != "" {
   546  		return c.usageTemplate
   547  	}
   548  
   549  	if c.HasParent() {
   550  		return c.parent.UsageTemplate()
   551  	}
   552  	return `Usage:{{if .Runnable}}
   553    {{.UseLine}}{{end}}{{if .HasAvailableSubCommands}}
   554    {{.CommandPath}} [command]{{end}}{{if gt (len .Aliases) 0}}
   555  
   556  Aliases:
   557    {{.NameAndAliases}}{{end}}{{if .HasExample}}
   558  
   559  Examples:
   560  {{.Example}}{{end}}{{if .HasAvailableSubCommands}}{{$cmds := .Commands}}{{if eq (len .Groups) 0}}
   561  
   562  Available Commands:{{range $cmds}}{{if (or .IsAvailableCommand (eq .Name "help"))}}
   563    {{rpad .Name .NamePadding }} {{.Short}}{{end}}{{end}}{{else}}{{range $group := .Groups}}
   564  
   565  {{.Title}}{{range $cmds}}{{if (and (eq .GroupID $group.ID) (or .IsAvailableCommand (eq .Name "help")))}}
   566    {{rpad .Name .NamePadding }} {{.Short}}{{end}}{{end}}{{end}}{{if not .AllChildCommandsHaveGroup}}
   567  
   568  Additional Commands:{{range $cmds}}{{if (and (eq .GroupID "") (or .IsAvailableCommand (eq .Name "help")))}}
   569    {{rpad .Name .NamePadding }} {{.Short}}{{end}}{{end}}{{end}}{{end}}{{end}}{{if .HasAvailableLocalFlags}}
   570  
   571  Flags:
   572  {{.LocalFlags.FlagUsages | trimTrailingWhitespaces}}{{end}}{{if .HasAvailableInheritedFlags}}
   573  
   574  Global Flags:
   575  {{.InheritedFlags.FlagUsages | trimTrailingWhitespaces}}{{end}}{{if .HasHelpSubCommands}}
   576  
   577  Additional help topics:{{range .Commands}}{{if .IsAdditionalHelpTopicCommand}}
   578    {{rpad .CommandPath .CommandPathPadding}} {{.Short}}{{end}}{{end}}{{end}}{{if .HasAvailableSubCommands}}
   579  
   580  Use "{{.CommandPath}} [command] --help" for more information about a command.{{end}}
   581  `
   582  }
   583  
   584  // HelpTemplate return help template for the command.
   585  func (c *Command) HelpTemplate() string {
   586  	if c.helpTemplate != "" {
   587  		return c.helpTemplate
   588  	}
   589  
   590  	if c.HasParent() {
   591  		return c.parent.HelpTemplate()
   592  	}
   593  	return `{{with (or .Long .Short)}}{{. | trimTrailingWhitespaces}}
   594  
   595  {{end}}{{if or .Runnable .HasSubCommands}}{{.UsageString}}{{end}}`
   596  }
   597  
   598  // VersionTemplate return version template for the command.
   599  func (c *Command) VersionTemplate() string {
   600  	if c.versionTemplate != "" {
   601  		return c.versionTemplate
   602  	}
   603  
   604  	if c.HasParent() {
   605  		return c.parent.VersionTemplate()
   606  	}
   607  	return `{{with .Name}}{{printf "%s " .}}{{end}}{{printf "version %s" .Version}}
   608  `
   609  }
   610  
   611  // ErrPrefix return error message prefix for the command
   612  func (c *Command) ErrPrefix() string {
   613  	if c.errPrefix != "" {
   614  		return c.errPrefix
   615  	}
   616  
   617  	if c.HasParent() {
   618  		return c.parent.ErrPrefix()
   619  	}
   620  	return "Error:"
   621  }
   622  
   623  func hasNoOptDefVal(name string, fs *flag.FlagSet) bool {
   624  	flag := fs.Lookup(name)
   625  	if flag == nil {
   626  		return false
   627  	}
   628  	return flag.NoOptDefVal != ""
   629  }
   630  
   631  func shortHasNoOptDefVal(name string, fs *flag.FlagSet) bool {
   632  	if len(name) == 0 {
   633  		return false
   634  	}
   635  
   636  	flag := fs.ShorthandLookup(name[:1])
   637  	if flag == nil {
   638  		return false
   639  	}
   640  	return flag.NoOptDefVal != ""
   641  }
   642  
   643  func stripFlags(args []string, c *Command) []string {
   644  	if len(args) == 0 {
   645  		return args
   646  	}
   647  	c.mergePersistentFlags()
   648  
   649  	commands := []string{}
   650  	flags := c.Flags()
   651  
   652  Loop:
   653  	for len(args) > 0 {
   654  		s := args[0]
   655  		args = args[1:]
   656  		switch {
   657  		case s == "--":
   658  			// "--" terminates the flags
   659  			break Loop
   660  		case strings.HasPrefix(s, "--") && !strings.Contains(s, "=") && !hasNoOptDefVal(s[2:], flags):
   661  			// If '--flag arg' then
   662  			// delete arg from args.
   663  			fallthrough // (do the same as below)
   664  		case strings.HasPrefix(s, "-") && !strings.Contains(s, "=") && len(s) == 2 && !shortHasNoOptDefVal(s[1:], flags):
   665  			// If '-f arg' then
   666  			// delete 'arg' from args or break the loop if len(args) <= 1.
   667  			if len(args) <= 1 {
   668  				break Loop
   669  			} else {
   670  				args = args[1:]
   671  				continue
   672  			}
   673  		case s != "" && !strings.HasPrefix(s, "-"):
   674  			commands = append(commands, s)
   675  		}
   676  	}
   677  
   678  	return commands
   679  }
   680  
   681  // argsMinusFirstX removes only the first x from args.  Otherwise, commands that look like
   682  // openshift admin policy add-role-to-user admin my-user, lose the admin argument (arg[4]).
   683  // Special care needs to be taken not to remove a flag value.
   684  func (c *Command) argsMinusFirstX(args []string, x string) []string {
   685  	if len(args) == 0 {
   686  		return args
   687  	}
   688  	c.mergePersistentFlags()
   689  	flags := c.Flags()
   690  
   691  Loop:
   692  	for pos := 0; pos < len(args); pos++ {
   693  		s := args[pos]
   694  		switch {
   695  		case s == "--":
   696  			// -- means we have reached the end of the parseable args. Break out of the loop now.
   697  			break Loop
   698  		case strings.HasPrefix(s, "--") && !strings.Contains(s, "=") && !hasNoOptDefVal(s[2:], flags):
   699  			fallthrough
   700  		case strings.HasPrefix(s, "-") && !strings.Contains(s, "=") && len(s) == 2 && !shortHasNoOptDefVal(s[1:], flags):
   701  			// This is a flag without a default value, and an equal sign is not used. Increment pos in order to skip
   702  			// over the next arg, because that is the value of this flag.
   703  			pos++
   704  			continue
   705  		case !strings.HasPrefix(s, "-"):
   706  			// This is not a flag or a flag value. Check to see if it matches what we're looking for, and if so,
   707  			// return the args, excluding the one at this position.
   708  			if s == x {
   709  				ret := []string{}
   710  				ret = append(ret, args[:pos]...)
   711  				ret = append(ret, args[pos+1:]...)
   712  				return ret
   713  			}
   714  		}
   715  	}
   716  	return args
   717  }
   718  
   719  func isFlagArg(arg string) bool {
   720  	return ((len(arg) >= 3 && arg[0:2] == "--") ||
   721  		(len(arg) >= 2 && arg[0] == '-' && arg[1] != '-'))
   722  }
   723  
   724  // Find the target command given the args and command tree
   725  // Meant to be run on the highest node. Only searches down.
   726  func (c *Command) Find(args []string) (*Command, []string, error) {
   727  	var innerfind func(*Command, []string) (*Command, []string)
   728  
   729  	innerfind = func(c *Command, innerArgs []string) (*Command, []string) {
   730  		argsWOflags := stripFlags(innerArgs, c)
   731  		if len(argsWOflags) == 0 {
   732  			return c, innerArgs
   733  		}
   734  		nextSubCmd := argsWOflags[0]
   735  
   736  		cmd := c.findNext(nextSubCmd)
   737  		if cmd != nil {
   738  			return innerfind(cmd, c.argsMinusFirstX(innerArgs, nextSubCmd))
   739  		}
   740  		return c, innerArgs
   741  	}
   742  
   743  	commandFound, a := innerfind(c, args)
   744  	if commandFound.Args == nil {
   745  		return commandFound, a, legacyArgs(commandFound, stripFlags(a, commandFound))
   746  	}
   747  	return commandFound, a, nil
   748  }
   749  
   750  func (c *Command) findSuggestions(arg string) string {
   751  	if c.DisableSuggestions {
   752  		return ""
   753  	}
   754  	if c.SuggestionsMinimumDistance <= 0 {
   755  		c.SuggestionsMinimumDistance = 2
   756  	}
   757  	suggestionsString := ""
   758  	if suggestions := c.SuggestionsFor(arg); len(suggestions) > 0 {
   759  		suggestionsString += "\n\nDid you mean this?\n"
   760  		for _, s := range suggestions {
   761  			suggestionsString += fmt.Sprintf("\t%v\n", s)
   762  		}
   763  	}
   764  	return suggestionsString
   765  }
   766  
   767  func (c *Command) findNext(next string) *Command {
   768  	matches := make([]*Command, 0)
   769  	for _, cmd := range c.commands {
   770  		if commandNameMatches(cmd.Name(), next) || cmd.HasAlias(next) {
   771  			cmd.commandCalledAs.name = next
   772  			return cmd
   773  		}
   774  		if EnablePrefixMatching && cmd.hasNameOrAliasPrefix(next) {
   775  			matches = append(matches, cmd)
   776  		}
   777  	}
   778  
   779  	if len(matches) == 1 {
   780  		// Temporarily disable gosec G602, which produces a false positive.
   781  		// See https://github.com/securego/gosec/issues/1005.
   782  		return matches[0] // #nosec G602
   783  	}
   784  
   785  	return nil
   786  }
   787  
   788  // Traverse the command tree to find the command, and parse args for
   789  // each parent.
   790  func (c *Command) Traverse(args []string) (*Command, []string, error) {
   791  	flags := []string{}
   792  	inFlag := false
   793  
   794  	for i, arg := range args {
   795  		switch {
   796  		// A long flag with a space separated value
   797  		case strings.HasPrefix(arg, "--") && !strings.Contains(arg, "="):
   798  			// TODO: this isn't quite right, we should really check ahead for 'true' or 'false'
   799  			inFlag = !hasNoOptDefVal(arg[2:], c.Flags())
   800  			flags = append(flags, arg)
   801  			continue
   802  		// A short flag with a space separated value
   803  		case strings.HasPrefix(arg, "-") && !strings.Contains(arg, "=") && len(arg) == 2 && !shortHasNoOptDefVal(arg[1:], c.Flags()):
   804  			inFlag = true
   805  			flags = append(flags, arg)
   806  			continue
   807  		// The value for a flag
   808  		case inFlag:
   809  			inFlag = false
   810  			flags = append(flags, arg)
   811  			continue
   812  		// A flag without a value, or with an `=` separated value
   813  		case isFlagArg(arg):
   814  			flags = append(flags, arg)
   815  			continue
   816  		}
   817  
   818  		cmd := c.findNext(arg)
   819  		if cmd == nil {
   820  			return c, args, nil
   821  		}
   822  
   823  		if err := c.ParseFlags(flags); err != nil {
   824  			return nil, args, err
   825  		}
   826  		return cmd.Traverse(args[i+1:])
   827  	}
   828  	return c, args, nil
   829  }
   830  
   831  // SuggestionsFor provides suggestions for the typedName.
   832  func (c *Command) SuggestionsFor(typedName string) []string {
   833  	suggestions := []string{}
   834  	for _, cmd := range c.commands {
   835  		if cmd.IsAvailableCommand() {
   836  			levenshteinDistance := ld(typedName, cmd.Name(), true)
   837  			suggestByLevenshtein := levenshteinDistance <= c.SuggestionsMinimumDistance
   838  			suggestByPrefix := strings.HasPrefix(strings.ToLower(cmd.Name()), strings.ToLower(typedName))
   839  			if suggestByLevenshtein || suggestByPrefix {
   840  				suggestions = append(suggestions, cmd.Name())
   841  			}
   842  			for _, explicitSuggestion := range cmd.SuggestFor {
   843  				if strings.EqualFold(typedName, explicitSuggestion) {
   844  					suggestions = append(suggestions, cmd.Name())
   845  				}
   846  			}
   847  		}
   848  	}
   849  	return suggestions
   850  }
   851  
   852  // VisitParents visits all parents of the command and invokes fn on each parent.
   853  func (c *Command) VisitParents(fn func(*Command)) {
   854  	if c.HasParent() {
   855  		fn(c.Parent())
   856  		c.Parent().VisitParents(fn)
   857  	}
   858  }
   859  
   860  // Root finds root command.
   861  func (c *Command) Root() *Command {
   862  	if c.HasParent() {
   863  		return c.Parent().Root()
   864  	}
   865  	return c
   866  }
   867  
   868  // ArgsLenAtDash will return the length of c.Flags().Args at the moment
   869  // when a -- was found during args parsing.
   870  func (c *Command) ArgsLenAtDash() int {
   871  	return c.Flags().ArgsLenAtDash()
   872  }
   873  
   874  func (c *Command) execute(a []string) (err error) {
   875  	if c == nil {
   876  		return fmt.Errorf("Called Execute() on a nil Command")
   877  	}
   878  
   879  	if len(c.Deprecated) > 0 {
   880  		c.Printf("Command %q is deprecated, %s\n", c.Name(), c.Deprecated)
   881  	}
   882  
   883  	// initialize help and version flag at the last point possible to allow for user
   884  	// overriding
   885  	c.InitDefaultHelpFlag()
   886  	c.InitDefaultVersionFlag()
   887  
   888  	err = c.ParseFlags(a)
   889  	if err != nil {
   890  		return c.FlagErrorFunc()(c, err)
   891  	}
   892  
   893  	// If help is called, regardless of other flags, return we want help.
   894  	// Also say we need help if the command isn't runnable.
   895  	helpVal, err := c.Flags().GetBool("help")
   896  	if err != nil {
   897  		// should be impossible to get here as we always declare a help
   898  		// flag in InitDefaultHelpFlag()
   899  		c.Println("\"help\" flag declared as non-bool. Please correct your code")
   900  		return err
   901  	}
   902  
   903  	if helpVal {
   904  		return flag.ErrHelp
   905  	}
   906  
   907  	// for back-compat, only add version flag behavior if version is defined
   908  	if c.Version != "" {
   909  		versionVal, err := c.Flags().GetBool("version")
   910  		if err != nil {
   911  			c.Println("\"version\" flag declared as non-bool. Please correct your code")
   912  			return err
   913  		}
   914  		if versionVal {
   915  			err := tmpl(c.OutOrStdout(), c.VersionTemplate(), c)
   916  			if err != nil {
   917  				c.Println(err)
   918  			}
   919  			return err
   920  		}
   921  	}
   922  
   923  	if !c.Runnable() {
   924  		return flag.ErrHelp
   925  	}
   926  
   927  	c.preRun()
   928  
   929  	defer c.postRun()
   930  
   931  	argWoFlags := c.Flags().Args()
   932  	if c.DisableFlagParsing {
   933  		argWoFlags = a
   934  	}
   935  
   936  	if err := c.ValidateArgs(argWoFlags); err != nil {
   937  		return err
   938  	}
   939  
   940  	parents := make([]*Command, 0, 5)
   941  	for p := c; p != nil; p = p.Parent() {
   942  		if EnableTraverseRunHooks {
   943  			// When EnableTraverseRunHooks is set:
   944  			// - Execute all persistent pre-runs from the root parent till this command.
   945  			// - Execute all persistent post-runs from this command till the root parent.
   946  			parents = append([]*Command{p}, parents...)
   947  		} else {
   948  			// Otherwise, execute only the first found persistent hook.
   949  			parents = append(parents, p)
   950  		}
   951  	}
   952  	for _, p := range parents {
   953  		if p.PersistentPreRunE != nil {
   954  			if err := p.PersistentPreRunE(c, argWoFlags); err != nil {
   955  				return err
   956  			}
   957  			if !EnableTraverseRunHooks {
   958  				break
   959  			}
   960  		} else if p.PersistentPreRun != nil {
   961  			p.PersistentPreRun(c, argWoFlags)
   962  			if !EnableTraverseRunHooks {
   963  				break
   964  			}
   965  		}
   966  	}
   967  	if c.PreRunE != nil {
   968  		if err := c.PreRunE(c, argWoFlags); err != nil {
   969  			return err
   970  		}
   971  	} else if c.PreRun != nil {
   972  		c.PreRun(c, argWoFlags)
   973  	}
   974  
   975  	if err := c.ValidateRequiredFlags(); err != nil {
   976  		return err
   977  	}
   978  	if err := c.ValidateFlagGroups(); err != nil {
   979  		return err
   980  	}
   981  
   982  	if c.RunE != nil {
   983  		if err := c.RunE(c, argWoFlags); err != nil {
   984  			return err
   985  		}
   986  	} else {
   987  		c.Run(c, argWoFlags)
   988  	}
   989  	if c.PostRunE != nil {
   990  		if err := c.PostRunE(c, argWoFlags); err != nil {
   991  			return err
   992  		}
   993  	} else if c.PostRun != nil {
   994  		c.PostRun(c, argWoFlags)
   995  	}
   996  	for p := c; p != nil; p = p.Parent() {
   997  		if p.PersistentPostRunE != nil {
   998  			if err := p.PersistentPostRunE(c, argWoFlags); err != nil {
   999  				return err
  1000  			}
  1001  			if !EnableTraverseRunHooks {
  1002  				break
  1003  			}
  1004  		} else if p.PersistentPostRun != nil {
  1005  			p.PersistentPostRun(c, argWoFlags)
  1006  			if !EnableTraverseRunHooks {
  1007  				break
  1008  			}
  1009  		}
  1010  	}
  1011  
  1012  	return nil
  1013  }
  1014  
  1015  func (c *Command) preRun() {
  1016  	for _, x := range initializers {
  1017  		x()
  1018  	}
  1019  }
  1020  
  1021  func (c *Command) postRun() {
  1022  	for _, x := range finalizers {
  1023  		x()
  1024  	}
  1025  }
  1026  
  1027  // ExecuteContext is the same as Execute(), but sets the ctx on the command.
  1028  // Retrieve ctx by calling cmd.Context() inside your *Run lifecycle or ValidArgs
  1029  // functions.
  1030  func (c *Command) ExecuteContext(ctx context.Context) error {
  1031  	c.ctx = ctx
  1032  	return c.Execute()
  1033  }
  1034  
  1035  // Execute uses the args (os.Args[1:] by default)
  1036  // and run through the command tree finding appropriate matches
  1037  // for commands and then corresponding flags.
  1038  func (c *Command) Execute() error {
  1039  	_, err := c.ExecuteC()
  1040  	return err
  1041  }
  1042  
  1043  // ExecuteContextC is the same as ExecuteC(), but sets the ctx on the command.
  1044  // Retrieve ctx by calling cmd.Context() inside your *Run lifecycle or ValidArgs
  1045  // functions.
  1046  func (c *Command) ExecuteContextC(ctx context.Context) (*Command, error) {
  1047  	c.ctx = ctx
  1048  	return c.ExecuteC()
  1049  }
  1050  
  1051  // ExecuteC executes the command.
  1052  func (c *Command) ExecuteC() (cmd *Command, err error) {
  1053  	if c.ctx == nil {
  1054  		c.ctx = context.Background()
  1055  	}
  1056  
  1057  	// Regardless of what command execute is called on, run on Root only
  1058  	if c.HasParent() {
  1059  		return c.Root().ExecuteC()
  1060  	}
  1061  
  1062  	// windows hook
  1063  	if preExecHookFn != nil {
  1064  		preExecHookFn(c)
  1065  	}
  1066  
  1067  	// initialize help at the last point to allow for user overriding
  1068  	c.InitDefaultHelpCmd()
  1069  	// initialize completion at the last point to allow for user overriding
  1070  	c.InitDefaultCompletionCmd()
  1071  
  1072  	// Now that all commands have been created, let's make sure all groups
  1073  	// are properly created also
  1074  	c.checkCommandGroups()
  1075  
  1076  	args := c.args
  1077  
  1078  	// Workaround FAIL with "go test -v" or "cobra.test -test.v", see #155
  1079  	if c.args == nil && filepath.Base(os.Args[0]) != "cobra.test" {
  1080  		args = os.Args[1:]
  1081  	}
  1082  
  1083  	// initialize the hidden command to be used for shell completion
  1084  	c.initCompleteCmd(args)
  1085  
  1086  	var flags []string
  1087  	if c.TraverseChildren {
  1088  		cmd, flags, err = c.Traverse(args)
  1089  	} else {
  1090  		cmd, flags, err = c.Find(args)
  1091  	}
  1092  	if err != nil {
  1093  		// If found parse to a subcommand and then failed, talk about the subcommand
  1094  		if cmd != nil {
  1095  			c = cmd
  1096  		}
  1097  		if !c.SilenceErrors {
  1098  			c.PrintErrln(c.ErrPrefix(), err.Error())
  1099  			c.PrintErrf("Run '%v --help' for usage.\n", c.CommandPath())
  1100  		}
  1101  		return c, err
  1102  	}
  1103  
  1104  	cmd.commandCalledAs.called = true
  1105  	if cmd.commandCalledAs.name == "" {
  1106  		cmd.commandCalledAs.name = cmd.Name()
  1107  	}
  1108  
  1109  	// We have to pass global context to children command
  1110  	// if context is present on the parent command.
  1111  	if cmd.ctx == nil {
  1112  		cmd.ctx = c.ctx
  1113  	}
  1114  
  1115  	err = cmd.execute(flags)
  1116  	if err != nil {
  1117  		// Always show help if requested, even if SilenceErrors is in
  1118  		// effect
  1119  		if errors.Is(err, flag.ErrHelp) {
  1120  			cmd.HelpFunc()(cmd, args)
  1121  			return cmd, nil
  1122  		}
  1123  
  1124  		// If root command has SilenceErrors flagged,
  1125  		// all subcommands should respect it
  1126  		if !cmd.SilenceErrors && !c.SilenceErrors {
  1127  			c.PrintErrln(cmd.ErrPrefix(), err.Error())
  1128  		}
  1129  
  1130  		// If root command has SilenceUsage flagged,
  1131  		// all subcommands should respect it
  1132  		if !cmd.SilenceUsage && !c.SilenceUsage {
  1133  			c.Println(cmd.UsageString())
  1134  		}
  1135  	}
  1136  	return cmd, err
  1137  }
  1138  
  1139  func (c *Command) ValidateArgs(args []string) error {
  1140  	if c.Args == nil {
  1141  		return ArbitraryArgs(c, args)
  1142  	}
  1143  	return c.Args(c, args)
  1144  }
  1145  
  1146  // ValidateRequiredFlags validates all required flags are present and returns an error otherwise
  1147  func (c *Command) ValidateRequiredFlags() error {
  1148  	if c.DisableFlagParsing {
  1149  		return nil
  1150  	}
  1151  
  1152  	flags := c.Flags()
  1153  	missingFlagNames := []string{}
  1154  	flags.VisitAll(func(pflag *flag.Flag) {
  1155  		requiredAnnotation, found := pflag.Annotations[BashCompOneRequiredFlag]
  1156  		if !found {
  1157  			return
  1158  		}
  1159  		if (requiredAnnotation[0] == "true") && !pflag.Changed {
  1160  			missingFlagNames = append(missingFlagNames, pflag.Name)
  1161  		}
  1162  	})
  1163  
  1164  	if len(missingFlagNames) > 0 {
  1165  		return fmt.Errorf(`required flag(s) "%s" not set`, strings.Join(missingFlagNames, `", "`))
  1166  	}
  1167  	return nil
  1168  }
  1169  
  1170  // checkCommandGroups checks if a command has been added to a group that does not exists.
  1171  // If so, we panic because it indicates a coding error that should be corrected.
  1172  func (c *Command) checkCommandGroups() {
  1173  	for _, sub := range c.commands {
  1174  		// if Group is not defined let the developer know right away
  1175  		if sub.GroupID != "" && !c.ContainsGroup(sub.GroupID) {
  1176  			panic(fmt.Sprintf("group id '%s' is not defined for subcommand '%s'", sub.GroupID, sub.CommandPath()))
  1177  		}
  1178  
  1179  		sub.checkCommandGroups()
  1180  	}
  1181  }
  1182  
  1183  // InitDefaultHelpFlag adds default help flag to c.
  1184  // It is called automatically by executing the c or by calling help and usage.
  1185  // If c already has help flag, it will do nothing.
  1186  func (c *Command) InitDefaultHelpFlag() {
  1187  	c.mergePersistentFlags()
  1188  	if c.Flags().Lookup("help") == nil {
  1189  		usage := "help for "
  1190  		if c.Name() == "" {
  1191  			usage += "this command"
  1192  		} else {
  1193  			usage += c.Name()
  1194  		}
  1195  		c.Flags().BoolP("help", "h", false, usage)
  1196  		_ = c.Flags().SetAnnotation("help", FlagSetByCobraAnnotation, []string{"true"})
  1197  	}
  1198  }
  1199  
  1200  // InitDefaultVersionFlag adds default version flag to c.
  1201  // It is called automatically by executing the c.
  1202  // If c already has a version flag, it will do nothing.
  1203  // If c.Version is empty, it will do nothing.
  1204  func (c *Command) InitDefaultVersionFlag() {
  1205  	if c.Version == "" {
  1206  		return
  1207  	}
  1208  
  1209  	c.mergePersistentFlags()
  1210  	if c.Flags().Lookup("version") == nil {
  1211  		usage := "version for "
  1212  		if c.Name() == "" {
  1213  			usage += "this command"
  1214  		} else {
  1215  			usage += c.Name()
  1216  		}
  1217  		if c.Flags().ShorthandLookup("v") == nil {
  1218  			c.Flags().BoolP("version", "v", false, usage)
  1219  		} else {
  1220  			c.Flags().Bool("version", false, usage)
  1221  		}
  1222  		_ = c.Flags().SetAnnotation("version", FlagSetByCobraAnnotation, []string{"true"})
  1223  	}
  1224  }
  1225  
  1226  // InitDefaultHelpCmd adds default help command to c.
  1227  // It is called automatically by executing the c or by calling help and usage.
  1228  // If c already has help command or c has no subcommands, it will do nothing.
  1229  func (c *Command) InitDefaultHelpCmd() {
  1230  	if !c.HasSubCommands() {
  1231  		return
  1232  	}
  1233  
  1234  	if c.helpCommand == nil {
  1235  		c.helpCommand = &Command{
  1236  			Use:   "help [command]",
  1237  			Short: "Help about any command",
  1238  			Long: `Help provides help for any command in the application.
  1239  Simply type ` + c.Name() + ` help [path to command] for full details.`,
  1240  			ValidArgsFunction: func(c *Command, args []string, toComplete string) ([]string, ShellCompDirective) {
  1241  				var completions []string
  1242  				cmd, _, e := c.Root().Find(args)
  1243  				if e != nil {
  1244  					return nil, ShellCompDirectiveNoFileComp
  1245  				}
  1246  				if cmd == nil {
  1247  					// Root help command.
  1248  					cmd = c.Root()
  1249  				}
  1250  				for _, subCmd := range cmd.Commands() {
  1251  					if subCmd.IsAvailableCommand() || subCmd == cmd.helpCommand {
  1252  						if strings.HasPrefix(subCmd.Name(), toComplete) {
  1253  							completions = append(completions, fmt.Sprintf("%s\t%s", subCmd.Name(), subCmd.Short))
  1254  						}
  1255  					}
  1256  				}
  1257  				return completions, ShellCompDirectiveNoFileComp
  1258  			},
  1259  			Run: func(c *Command, args []string) {
  1260  				cmd, _, e := c.Root().Find(args)
  1261  				if cmd == nil || e != nil {
  1262  					c.Printf("Unknown help topic %#q\n", args)
  1263  					CheckErr(c.Root().Usage())
  1264  				} else {
  1265  					cmd.InitDefaultHelpFlag()    // make possible 'help' flag to be shown
  1266  					cmd.InitDefaultVersionFlag() // make possible 'version' flag to be shown
  1267  					CheckErr(cmd.Help())
  1268  				}
  1269  			},
  1270  			GroupID: c.helpCommandGroupID,
  1271  		}
  1272  	}
  1273  	c.RemoveCommand(c.helpCommand)
  1274  	c.AddCommand(c.helpCommand)
  1275  }
  1276  
  1277  // ResetCommands delete parent, subcommand and help command from c.
  1278  func (c *Command) ResetCommands() {
  1279  	c.parent = nil
  1280  	c.commands = nil
  1281  	c.helpCommand = nil
  1282  	c.parentsPflags = nil
  1283  }
  1284  
  1285  // Sorts commands by their names.
  1286  type commandSorterByName []*Command
  1287  
  1288  func (c commandSorterByName) Len() int           { return len(c) }
  1289  func (c commandSorterByName) Swap(i, j int)      { c[i], c[j] = c[j], c[i] }
  1290  func (c commandSorterByName) Less(i, j int) bool { return c[i].Name() < c[j].Name() }
  1291  
  1292  // Commands returns a sorted slice of child commands.
  1293  func (c *Command) Commands() []*Command {
  1294  	// do not sort commands if it already sorted or sorting was disabled
  1295  	if EnableCommandSorting && !c.commandsAreSorted {
  1296  		sort.Sort(commandSorterByName(c.commands))
  1297  		c.commandsAreSorted = true
  1298  	}
  1299  	return c.commands
  1300  }
  1301  
  1302  // AddCommand adds one or more commands to this parent command.
  1303  func (c *Command) AddCommand(cmds ...*Command) {
  1304  	for i, x := range cmds {
  1305  		if cmds[i] == c {
  1306  			panic("Command can't be a child of itself")
  1307  		}
  1308  		cmds[i].parent = c
  1309  		// update max lengths
  1310  		usageLen := len(x.Use)
  1311  		if usageLen > c.commandsMaxUseLen {
  1312  			c.commandsMaxUseLen = usageLen
  1313  		}
  1314  		commandPathLen := len(x.CommandPath())
  1315  		if commandPathLen > c.commandsMaxCommandPathLen {
  1316  			c.commandsMaxCommandPathLen = commandPathLen
  1317  		}
  1318  		nameLen := len(x.Name())
  1319  		if nameLen > c.commandsMaxNameLen {
  1320  			c.commandsMaxNameLen = nameLen
  1321  		}
  1322  		// If global normalization function exists, update all children
  1323  		if c.globNormFunc != nil {
  1324  			x.SetGlobalNormalizationFunc(c.globNormFunc)
  1325  		}
  1326  		c.commands = append(c.commands, x)
  1327  		c.commandsAreSorted = false
  1328  	}
  1329  }
  1330  
  1331  // Groups returns a slice of child command groups.
  1332  func (c *Command) Groups() []*Group {
  1333  	return c.commandgroups
  1334  }
  1335  
  1336  // AllChildCommandsHaveGroup returns if all subcommands are assigned to a group
  1337  func (c *Command) AllChildCommandsHaveGroup() bool {
  1338  	for _, sub := range c.commands {
  1339  		if (sub.IsAvailableCommand() || sub == c.helpCommand) && sub.GroupID == "" {
  1340  			return false
  1341  		}
  1342  	}
  1343  	return true
  1344  }
  1345  
  1346  // ContainsGroup return if groupID exists in the list of command groups.
  1347  func (c *Command) ContainsGroup(groupID string) bool {
  1348  	for _, x := range c.commandgroups {
  1349  		if x.ID == groupID {
  1350  			return true
  1351  		}
  1352  	}
  1353  	return false
  1354  }
  1355  
  1356  // AddGroup adds one or more command groups to this parent command.
  1357  func (c *Command) AddGroup(groups ...*Group) {
  1358  	c.commandgroups = append(c.commandgroups, groups...)
  1359  }
  1360  
  1361  // RemoveCommand removes one or more commands from a parent command.
  1362  func (c *Command) RemoveCommand(cmds ...*Command) {
  1363  	commands := []*Command{}
  1364  main:
  1365  	for _, command := range c.commands {
  1366  		for _, cmd := range cmds {
  1367  			if command == cmd {
  1368  				command.parent = nil
  1369  				continue main
  1370  			}
  1371  		}
  1372  		commands = append(commands, command)
  1373  	}
  1374  	c.commands = commands
  1375  	// recompute all lengths
  1376  	c.commandsMaxUseLen = 0
  1377  	c.commandsMaxCommandPathLen = 0
  1378  	c.commandsMaxNameLen = 0
  1379  	for _, command := range c.commands {
  1380  		usageLen := len(command.Use)
  1381  		if usageLen > c.commandsMaxUseLen {
  1382  			c.commandsMaxUseLen = usageLen
  1383  		}
  1384  		commandPathLen := len(command.CommandPath())
  1385  		if commandPathLen > c.commandsMaxCommandPathLen {
  1386  			c.commandsMaxCommandPathLen = commandPathLen
  1387  		}
  1388  		nameLen := len(command.Name())
  1389  		if nameLen > c.commandsMaxNameLen {
  1390  			c.commandsMaxNameLen = nameLen
  1391  		}
  1392  	}
  1393  }
  1394  
  1395  // Print is a convenience method to Print to the defined output, fallback to Stderr if not set.
  1396  func (c *Command) Print(i ...interface{}) {
  1397  	fmt.Fprint(c.OutOrStderr(), i...)
  1398  }
  1399  
  1400  // Println is a convenience method to Println to the defined output, fallback to Stderr if not set.
  1401  func (c *Command) Println(i ...interface{}) {
  1402  	c.Print(fmt.Sprintln(i...))
  1403  }
  1404  
  1405  // Printf is a convenience method to Printf to the defined output, fallback to Stderr if not set.
  1406  func (c *Command) Printf(format string, i ...interface{}) {
  1407  	c.Print(fmt.Sprintf(format, i...))
  1408  }
  1409  
  1410  // PrintErr is a convenience method to Print to the defined Err output, fallback to Stderr if not set.
  1411  func (c *Command) PrintErr(i ...interface{}) {
  1412  	fmt.Fprint(c.ErrOrStderr(), i...)
  1413  }
  1414  
  1415  // PrintErrln is a convenience method to Println to the defined Err output, fallback to Stderr if not set.
  1416  func (c *Command) PrintErrln(i ...interface{}) {
  1417  	c.PrintErr(fmt.Sprintln(i...))
  1418  }
  1419  
  1420  // PrintErrf is a convenience method to Printf to the defined Err output, fallback to Stderr if not set.
  1421  func (c *Command) PrintErrf(format string, i ...interface{}) {
  1422  	c.PrintErr(fmt.Sprintf(format, i...))
  1423  }
  1424  
  1425  // CommandPath returns the full path to this command.
  1426  func (c *Command) CommandPath() string {
  1427  	if c.HasParent() {
  1428  		return c.Parent().CommandPath() + " " + c.Name()
  1429  	}
  1430  	if displayName, ok := c.Annotations[CommandDisplayNameAnnotation]; ok {
  1431  		return displayName
  1432  	}
  1433  	return c.Name()
  1434  }
  1435  
  1436  // UseLine puts out the full usage for a given command (including parents).
  1437  func (c *Command) UseLine() string {
  1438  	var useline string
  1439  	if c.HasParent() {
  1440  		useline = c.parent.CommandPath() + " " + c.Use
  1441  	} else {
  1442  		useline = c.Use
  1443  	}
  1444  	if c.DisableFlagsInUseLine {
  1445  		return useline
  1446  	}
  1447  	if c.HasAvailableFlags() && !strings.Contains(useline, "[flags]") {
  1448  		useline += " [flags]"
  1449  	}
  1450  	return useline
  1451  }
  1452  
  1453  // DebugFlags used to determine which flags have been assigned to which commands
  1454  // and which persist.
  1455  // nolint:goconst
  1456  func (c *Command) DebugFlags() {
  1457  	c.Println("DebugFlags called on", c.Name())
  1458  	var debugflags func(*Command)
  1459  
  1460  	debugflags = func(x *Command) {
  1461  		if x.HasFlags() || x.HasPersistentFlags() {
  1462  			c.Println(x.Name())
  1463  		}
  1464  		if x.HasFlags() {
  1465  			x.flags.VisitAll(func(f *flag.Flag) {
  1466  				if x.HasPersistentFlags() && x.persistentFlag(f.Name) != nil {
  1467  					c.Println("  -"+f.Shorthand+",", "--"+f.Name, "["+f.DefValue+"]", "", f.Value, "  [LP]")
  1468  				} else {
  1469  					c.Println("  -"+f.Shorthand+",", "--"+f.Name, "["+f.DefValue+"]", "", f.Value, "  [L]")
  1470  				}
  1471  			})
  1472  		}
  1473  		if x.HasPersistentFlags() {
  1474  			x.pflags.VisitAll(func(f *flag.Flag) {
  1475  				if x.HasFlags() {
  1476  					if x.flags.Lookup(f.Name) == nil {
  1477  						c.Println("  -"+f.Shorthand+",", "--"+f.Name, "["+f.DefValue+"]", "", f.Value, "  [P]")
  1478  					}
  1479  				} else {
  1480  					c.Println("  -"+f.Shorthand+",", "--"+f.Name, "["+f.DefValue+"]", "", f.Value, "  [P]")
  1481  				}
  1482  			})
  1483  		}
  1484  		c.Println(x.flagErrorBuf)
  1485  		if x.HasSubCommands() {
  1486  			for _, y := range x.commands {
  1487  				debugflags(y)
  1488  			}
  1489  		}
  1490  	}
  1491  
  1492  	debugflags(c)
  1493  }
  1494  
  1495  // Name returns the command's name: the first word in the use line.
  1496  func (c *Command) Name() string {
  1497  	name := c.Use
  1498  	i := strings.Index(name, " ")
  1499  	if i >= 0 {
  1500  		name = name[:i]
  1501  	}
  1502  	return name
  1503  }
  1504  
  1505  // HasAlias determines if a given string is an alias of the command.
  1506  func (c *Command) HasAlias(s string) bool {
  1507  	for _, a := range c.Aliases {
  1508  		if commandNameMatches(a, s) {
  1509  			return true
  1510  		}
  1511  	}
  1512  	return false
  1513  }
  1514  
  1515  // CalledAs returns the command name or alias that was used to invoke
  1516  // this command or an empty string if the command has not been called.
  1517  func (c *Command) CalledAs() string {
  1518  	if c.commandCalledAs.called {
  1519  		return c.commandCalledAs.name
  1520  	}
  1521  	return ""
  1522  }
  1523  
  1524  // hasNameOrAliasPrefix returns true if the Name or any of aliases start
  1525  // with prefix
  1526  func (c *Command) hasNameOrAliasPrefix(prefix string) bool {
  1527  	if strings.HasPrefix(c.Name(), prefix) {
  1528  		c.commandCalledAs.name = c.Name()
  1529  		return true
  1530  	}
  1531  	for _, alias := range c.Aliases {
  1532  		if strings.HasPrefix(alias, prefix) {
  1533  			c.commandCalledAs.name = alias
  1534  			return true
  1535  		}
  1536  	}
  1537  	return false
  1538  }
  1539  
  1540  // NameAndAliases returns a list of the command name and all aliases
  1541  func (c *Command) NameAndAliases() string {
  1542  	return strings.Join(append([]string{c.Name()}, c.Aliases...), ", ")
  1543  }
  1544  
  1545  // HasExample determines if the command has example.
  1546  func (c *Command) HasExample() bool {
  1547  	return len(c.Example) > 0
  1548  }
  1549  
  1550  // Runnable determines if the command is itself runnable.
  1551  func (c *Command) Runnable() bool {
  1552  	return c.Run != nil || c.RunE != nil
  1553  }
  1554  
  1555  // HasSubCommands determines if the command has children commands.
  1556  func (c *Command) HasSubCommands() bool {
  1557  	return len(c.commands) > 0
  1558  }
  1559  
  1560  // IsAvailableCommand determines if a command is available as a non-help command
  1561  // (this includes all non deprecated/hidden commands).
  1562  func (c *Command) IsAvailableCommand() bool {
  1563  	if len(c.Deprecated) != 0 || c.Hidden {
  1564  		return false
  1565  	}
  1566  
  1567  	if c.HasParent() && c.Parent().helpCommand == c {
  1568  		return false
  1569  	}
  1570  
  1571  	if c.Runnable() || c.HasAvailableSubCommands() {
  1572  		return true
  1573  	}
  1574  
  1575  	return false
  1576  }
  1577  
  1578  // IsAdditionalHelpTopicCommand determines if a command is an additional
  1579  // help topic command; additional help topic command is determined by the
  1580  // fact that it is NOT runnable/hidden/deprecated, and has no sub commands that
  1581  // are runnable/hidden/deprecated.
  1582  // Concrete example: https://github.com/spf13/cobra/issues/393#issuecomment-282741924.
  1583  func (c *Command) IsAdditionalHelpTopicCommand() bool {
  1584  	// if a command is runnable, deprecated, or hidden it is not a 'help' command
  1585  	if c.Runnable() || len(c.Deprecated) != 0 || c.Hidden {
  1586  		return false
  1587  	}
  1588  
  1589  	// if any non-help sub commands are found, the command is not a 'help' command
  1590  	for _, sub := range c.commands {
  1591  		if !sub.IsAdditionalHelpTopicCommand() {
  1592  			return false
  1593  		}
  1594  	}
  1595  
  1596  	// the command either has no sub commands, or no non-help sub commands
  1597  	return true
  1598  }
  1599  
  1600  // HasHelpSubCommands determines if a command has any available 'help' sub commands
  1601  // that need to be shown in the usage/help default template under 'additional help
  1602  // topics'.
  1603  func (c *Command) HasHelpSubCommands() bool {
  1604  	// return true on the first found available 'help' sub command
  1605  	for _, sub := range c.commands {
  1606  		if sub.IsAdditionalHelpTopicCommand() {
  1607  			return true
  1608  		}
  1609  	}
  1610  
  1611  	// the command either has no sub commands, or no available 'help' sub commands
  1612  	return false
  1613  }
  1614  
  1615  // HasAvailableSubCommands determines if a command has available sub commands that
  1616  // need to be shown in the usage/help default template under 'available commands'.
  1617  func (c *Command) HasAvailableSubCommands() bool {
  1618  	// return true on the first found available (non deprecated/help/hidden)
  1619  	// sub command
  1620  	for _, sub := range c.commands {
  1621  		if sub.IsAvailableCommand() {
  1622  			return true
  1623  		}
  1624  	}
  1625  
  1626  	// the command either has no sub commands, or no available (non deprecated/help/hidden)
  1627  	// sub commands
  1628  	return false
  1629  }
  1630  
  1631  // HasParent determines if the command is a child command.
  1632  func (c *Command) HasParent() bool {
  1633  	return c.parent != nil
  1634  }
  1635  
  1636  // GlobalNormalizationFunc returns the global normalization function or nil if it doesn't exist.
  1637  func (c *Command) GlobalNormalizationFunc() func(f *flag.FlagSet, name string) flag.NormalizedName {
  1638  	return c.globNormFunc
  1639  }
  1640  
  1641  // Flags returns the complete FlagSet that applies
  1642  // to this command (local and persistent declared here and by all parents).
  1643  func (c *Command) Flags() *flag.FlagSet {
  1644  	if c.flags == nil {
  1645  		c.flags = flag.NewFlagSet(c.Name(), flag.ContinueOnError)
  1646  		if c.flagErrorBuf == nil {
  1647  			c.flagErrorBuf = new(bytes.Buffer)
  1648  		}
  1649  		c.flags.SetOutput(c.flagErrorBuf)
  1650  	}
  1651  
  1652  	return c.flags
  1653  }
  1654  
  1655  // LocalNonPersistentFlags are flags specific to this command which will NOT persist to subcommands.
  1656  func (c *Command) LocalNonPersistentFlags() *flag.FlagSet {
  1657  	persistentFlags := c.PersistentFlags()
  1658  
  1659  	out := flag.NewFlagSet(c.Name(), flag.ContinueOnError)
  1660  	c.LocalFlags().VisitAll(func(f *flag.Flag) {
  1661  		if persistentFlags.Lookup(f.Name) == nil {
  1662  			out.AddFlag(f)
  1663  		}
  1664  	})
  1665  	return out
  1666  }
  1667  
  1668  // LocalFlags returns the local FlagSet specifically set in the current command.
  1669  func (c *Command) LocalFlags() *flag.FlagSet {
  1670  	c.mergePersistentFlags()
  1671  
  1672  	if c.lflags == nil {
  1673  		c.lflags = flag.NewFlagSet(c.Name(), flag.ContinueOnError)
  1674  		if c.flagErrorBuf == nil {
  1675  			c.flagErrorBuf = new(bytes.Buffer)
  1676  		}
  1677  		c.lflags.SetOutput(c.flagErrorBuf)
  1678  	}
  1679  	c.lflags.SortFlags = c.Flags().SortFlags
  1680  	if c.globNormFunc != nil {
  1681  		c.lflags.SetNormalizeFunc(c.globNormFunc)
  1682  	}
  1683  
  1684  	addToLocal := func(f *flag.Flag) {
  1685  		// Add the flag if it is not a parent PFlag, or it shadows a parent PFlag
  1686  		if c.lflags.Lookup(f.Name) == nil && f != c.parentsPflags.Lookup(f.Name) {
  1687  			c.lflags.AddFlag(f)
  1688  		}
  1689  	}
  1690  	c.Flags().VisitAll(addToLocal)
  1691  	c.PersistentFlags().VisitAll(addToLocal)
  1692  	return c.lflags
  1693  }
  1694  
  1695  // InheritedFlags returns all flags which were inherited from parent commands.
  1696  func (c *Command) InheritedFlags() *flag.FlagSet {
  1697  	c.mergePersistentFlags()
  1698  
  1699  	if c.iflags == nil {
  1700  		c.iflags = flag.NewFlagSet(c.Name(), flag.ContinueOnError)
  1701  		if c.flagErrorBuf == nil {
  1702  			c.flagErrorBuf = new(bytes.Buffer)
  1703  		}
  1704  		c.iflags.SetOutput(c.flagErrorBuf)
  1705  	}
  1706  
  1707  	local := c.LocalFlags()
  1708  	if c.globNormFunc != nil {
  1709  		c.iflags.SetNormalizeFunc(c.globNormFunc)
  1710  	}
  1711  
  1712  	c.parentsPflags.VisitAll(func(f *flag.Flag) {
  1713  		if c.iflags.Lookup(f.Name) == nil && local.Lookup(f.Name) == nil {
  1714  			c.iflags.AddFlag(f)
  1715  		}
  1716  	})
  1717  	return c.iflags
  1718  }
  1719  
  1720  // NonInheritedFlags returns all flags which were not inherited from parent commands.
  1721  func (c *Command) NonInheritedFlags() *flag.FlagSet {
  1722  	return c.LocalFlags()
  1723  }
  1724  
  1725  // PersistentFlags returns the persistent FlagSet specifically set in the current command.
  1726  func (c *Command) PersistentFlags() *flag.FlagSet {
  1727  	if c.pflags == nil {
  1728  		c.pflags = flag.NewFlagSet(c.Name(), flag.ContinueOnError)
  1729  		if c.flagErrorBuf == nil {
  1730  			c.flagErrorBuf = new(bytes.Buffer)
  1731  		}
  1732  		c.pflags.SetOutput(c.flagErrorBuf)
  1733  	}
  1734  	return c.pflags
  1735  }
  1736  
  1737  // ResetFlags deletes all flags from command.
  1738  func (c *Command) ResetFlags() {
  1739  	c.flagErrorBuf = new(bytes.Buffer)
  1740  	c.flagErrorBuf.Reset()
  1741  	c.flags = flag.NewFlagSet(c.Name(), flag.ContinueOnError)
  1742  	c.flags.SetOutput(c.flagErrorBuf)
  1743  	c.pflags = flag.NewFlagSet(c.Name(), flag.ContinueOnError)
  1744  	c.pflags.SetOutput(c.flagErrorBuf)
  1745  
  1746  	c.lflags = nil
  1747  	c.iflags = nil
  1748  	c.parentsPflags = nil
  1749  }
  1750  
  1751  // HasFlags checks if the command contains any flags (local plus persistent from the entire structure).
  1752  func (c *Command) HasFlags() bool {
  1753  	return c.Flags().HasFlags()
  1754  }
  1755  
  1756  // HasPersistentFlags checks if the command contains persistent flags.
  1757  func (c *Command) HasPersistentFlags() bool {
  1758  	return c.PersistentFlags().HasFlags()
  1759  }
  1760  
  1761  // HasLocalFlags checks if the command has flags specifically declared locally.
  1762  func (c *Command) HasLocalFlags() bool {
  1763  	return c.LocalFlags().HasFlags()
  1764  }
  1765  
  1766  // HasInheritedFlags checks if the command has flags inherited from its parent command.
  1767  func (c *Command) HasInheritedFlags() bool {
  1768  	return c.InheritedFlags().HasFlags()
  1769  }
  1770  
  1771  // HasAvailableFlags checks if the command contains any flags (local plus persistent from the entire
  1772  // structure) which are not hidden or deprecated.
  1773  func (c *Command) HasAvailableFlags() bool {
  1774  	return c.Flags().HasAvailableFlags()
  1775  }
  1776  
  1777  // HasAvailablePersistentFlags checks if the command contains persistent flags which are not hidden or deprecated.
  1778  func (c *Command) HasAvailablePersistentFlags() bool {
  1779  	return c.PersistentFlags().HasAvailableFlags()
  1780  }
  1781  
  1782  // HasAvailableLocalFlags checks if the command has flags specifically declared locally which are not hidden
  1783  // or deprecated.
  1784  func (c *Command) HasAvailableLocalFlags() bool {
  1785  	return c.LocalFlags().HasAvailableFlags()
  1786  }
  1787  
  1788  // HasAvailableInheritedFlags checks if the command has flags inherited from its parent command which are
  1789  // not hidden or deprecated.
  1790  func (c *Command) HasAvailableInheritedFlags() bool {
  1791  	return c.InheritedFlags().HasAvailableFlags()
  1792  }
  1793  
  1794  // Flag climbs up the command tree looking for matching flag.
  1795  func (c *Command) Flag(name string) (flag *flag.Flag) {
  1796  	flag = c.Flags().Lookup(name)
  1797  
  1798  	if flag == nil {
  1799  		flag = c.persistentFlag(name)
  1800  	}
  1801  
  1802  	return
  1803  }
  1804  
  1805  // Recursively find matching persistent flag.
  1806  func (c *Command) persistentFlag(name string) (flag *flag.Flag) {
  1807  	if c.HasPersistentFlags() {
  1808  		flag = c.PersistentFlags().Lookup(name)
  1809  	}
  1810  
  1811  	if flag == nil {
  1812  		c.updateParentsPflags()
  1813  		flag = c.parentsPflags.Lookup(name)
  1814  	}
  1815  	return
  1816  }
  1817  
  1818  // ParseFlags parses persistent flag tree and local flags.
  1819  func (c *Command) ParseFlags(args []string) error {
  1820  	if c.DisableFlagParsing {
  1821  		return nil
  1822  	}
  1823  
  1824  	if c.flagErrorBuf == nil {
  1825  		c.flagErrorBuf = new(bytes.Buffer)
  1826  	}
  1827  	beforeErrorBufLen := c.flagErrorBuf.Len()
  1828  	c.mergePersistentFlags()
  1829  
  1830  	// do it here after merging all flags and just before parse
  1831  	c.Flags().ParseErrorsWhitelist = flag.ParseErrorsWhitelist(c.FParseErrWhitelist)
  1832  
  1833  	err := c.Flags().Parse(args)
  1834  	// Print warnings if they occurred (e.g. deprecated flag messages).
  1835  	if c.flagErrorBuf.Len()-beforeErrorBufLen > 0 && err == nil {
  1836  		c.Print(c.flagErrorBuf.String())
  1837  	}
  1838  
  1839  	return err
  1840  }
  1841  
  1842  // Parent returns a commands parent command.
  1843  func (c *Command) Parent() *Command {
  1844  	return c.parent
  1845  }
  1846  
  1847  // mergePersistentFlags merges c.PersistentFlags() to c.Flags()
  1848  // and adds missing persistent flags of all parents.
  1849  func (c *Command) mergePersistentFlags() {
  1850  	c.updateParentsPflags()
  1851  	c.Flags().AddFlagSet(c.PersistentFlags())
  1852  	c.Flags().AddFlagSet(c.parentsPflags)
  1853  }
  1854  
  1855  // updateParentsPflags updates c.parentsPflags by adding
  1856  // new persistent flags of all parents.
  1857  // If c.parentsPflags == nil, it makes new.
  1858  func (c *Command) updateParentsPflags() {
  1859  	if c.parentsPflags == nil {
  1860  		c.parentsPflags = flag.NewFlagSet(c.Name(), flag.ContinueOnError)
  1861  		c.parentsPflags.SetOutput(c.flagErrorBuf)
  1862  		c.parentsPflags.SortFlags = false
  1863  	}
  1864  
  1865  	if c.globNormFunc != nil {
  1866  		c.parentsPflags.SetNormalizeFunc(c.globNormFunc)
  1867  	}
  1868  
  1869  	c.Root().PersistentFlags().AddFlagSet(flag.CommandLine)
  1870  
  1871  	c.VisitParents(func(parent *Command) {
  1872  		c.parentsPflags.AddFlagSet(parent.PersistentFlags())
  1873  	})
  1874  }
  1875  
  1876  // commandNameMatches checks if two command names are equal
  1877  // taking into account case sensitivity according to
  1878  // EnableCaseInsensitive global configuration.
  1879  func commandNameMatches(s string, t string) bool {
  1880  	if EnableCaseInsensitive {
  1881  		return strings.EqualFold(s, t)
  1882  	}
  1883  
  1884  	return s == t
  1885  }
  1886  

View as plain text