const FlagSetByCobraAnnotation … const CommandDisplayNameAnnotation … type FParseErrWhitelist … type Group … type Command … // Context returns underlying command context. If command was executed // with ExecuteContext or the context was set with SetContext, the // previously set context will be returned. Otherwise, nil is returned. // // Notice that a call to Execute and ExecuteC will replace a nil context of // a command with a context.Background, so a background context will be // returned by Context after one of these functions has been called. func (c *Command) Context() context.Context { … } // SetContext sets context for the command. This context will be overwritten by // Command.ExecuteContext or Command.ExecuteContextC. func (c *Command) SetContext(ctx context.Context) { … } // SetArgs sets arguments for the command. It is set to os.Args[1:] by default, if desired, can be overridden // particularly useful when testing. func (c *Command) SetArgs(a []string) { … } // SetOutput sets the destination for usage and error messages. // If output is nil, os.Stderr is used. // Deprecated: Use SetOut and/or SetErr instead func (c *Command) SetOutput(output io.Writer) { … } // SetOut sets the destination for usage messages. // If newOut is nil, os.Stdout is used. func (c *Command) SetOut(newOut io.Writer) { … } // SetErr sets the destination for error messages. // If newErr is nil, os.Stderr is used. func (c *Command) SetErr(newErr io.Writer) { … } // SetIn sets the source for input data // If newIn is nil, os.Stdin is used. func (c *Command) SetIn(newIn io.Reader) { … } // SetUsageFunc sets usage function. Usage can be defined by application. func (c *Command) SetUsageFunc(f func(*Command) error) { … } // SetUsageTemplate sets usage template. Can be defined by Application. func (c *Command) SetUsageTemplate(s string) { … } // SetFlagErrorFunc sets a function to generate an error when flag parsing // fails. func (c *Command) SetFlagErrorFunc(f func(*Command, error) error) { … } // SetHelpFunc sets help function. Can be defined by Application. func (c *Command) SetHelpFunc(f func(*Command, []string)) { … } // SetHelpCommand sets help command. func (c *Command) SetHelpCommand(cmd *Command) { … } // SetHelpCommandGroupID sets the group id of the help command. func (c *Command) SetHelpCommandGroupID(groupID string) { … } // SetCompletionCommandGroupID sets the group id of the completion command. func (c *Command) SetCompletionCommandGroupID(groupID string) { … } // SetHelpTemplate sets help template to be used. Application can use it to set custom template. func (c *Command) SetHelpTemplate(s string) { … } // SetVersionTemplate sets version template to be used. Application can use it to set custom template. func (c *Command) SetVersionTemplate(s string) { … } // SetErrPrefix sets error message prefix to be used. Application can use it to set custom prefix. func (c *Command) SetErrPrefix(s string) { … } // SetGlobalNormalizationFunc sets a normalization function to all flag sets and also to child commands. // The user should not have a cyclic dependency on commands. func (c *Command) SetGlobalNormalizationFunc(n func(f *flag.FlagSet, name string) flag.NormalizedName) { … } // OutOrStdout returns output to stdout. func (c *Command) OutOrStdout() io.Writer { … } // OutOrStderr returns output to stderr func (c *Command) OutOrStderr() io.Writer { … } // ErrOrStderr returns output to stderr func (c *Command) ErrOrStderr() io.Writer { … } // InOrStdin returns input to stdin func (c *Command) InOrStdin() io.Reader { … } func (c *Command) getOut(def io.Writer) io.Writer { … } func (c *Command) getErr(def io.Writer) io.Writer { … } func (c *Command) getIn(def io.Reader) io.Reader { … } // UsageFunc returns either the function set by SetUsageFunc for this command // or a parent, or it returns a default usage function. func (c *Command) UsageFunc() (f func(*Command) error) { … } // Usage puts out the usage for the command. // Used when a user provides invalid input. // Can be defined by user by overriding UsageFunc. func (c *Command) Usage() error { … } // HelpFunc returns either the function set by SetHelpFunc for this command // or a parent, or it returns a function with default help behavior. func (c *Command) HelpFunc() func(*Command, []string) { … } // Help puts out the help for the command. // Used when a user calls help [command]. // Can be defined by user by overriding HelpFunc. func (c *Command) Help() error { … } // UsageString returns usage string. func (c *Command) UsageString() string { … } // FlagErrorFunc returns either the function set by SetFlagErrorFunc for this // command or a parent, or it returns a function which returns the original // error. func (c *Command) FlagErrorFunc() (f func(*Command, error) error) { … } var minUsagePadding … // UsagePadding return padding for the usage. func (c *Command) UsagePadding() int { … } var minCommandPathPadding … // CommandPathPadding return padding for the command path. func (c *Command) CommandPathPadding() int { … } var minNamePadding … // NamePadding returns padding for the name. func (c *Command) NamePadding() int { … } // UsageTemplate returns usage template for the command. func (c *Command) UsageTemplate() string { … } // HelpTemplate return help template for the command. func (c *Command) HelpTemplate() string { … } // VersionTemplate return version template for the command. func (c *Command) VersionTemplate() string { … } // ErrPrefix return error message prefix for the command func (c *Command) ErrPrefix() string { … } func hasNoOptDefVal(name string, fs *flag.FlagSet) bool { … } func shortHasNoOptDefVal(name string, fs *flag.FlagSet) bool { … } func stripFlags(args []string, c *Command) []string { … } // argsMinusFirstX removes only the first x from args. Otherwise, commands that look like // openshift admin policy add-role-to-user admin my-user, lose the admin argument (arg[4]). // Special care needs to be taken not to remove a flag value. func (c *Command) argsMinusFirstX(args []string, x string) []string { … } func isFlagArg(arg string) bool { … } // Find the target command given the args and command tree // Meant to be run on the highest node. Only searches down. func (c *Command) Find(args []string) (*Command, []string, error) { … } func (c *Command) findSuggestions(arg string) string { … } func (c *Command) findNext(next string) *Command { … } // Traverse the command tree to find the command, and parse args for // each parent. func (c *Command) Traverse(args []string) (*Command, []string, error) { … } // SuggestionsFor provides suggestions for the typedName. func (c *Command) SuggestionsFor(typedName string) []string { … } // VisitParents visits all parents of the command and invokes fn on each parent. func (c *Command) VisitParents(fn func(*Command)) { … } // Root finds root command. func (c *Command) Root() *Command { … } // ArgsLenAtDash will return the length of c.Flags().Args at the moment // when a -- was found during args parsing. func (c *Command) ArgsLenAtDash() int { … } func (c *Command) execute(a []string) (err error) { … } func (c *Command) preRun() { … } func (c *Command) postRun() { … } // ExecuteContext is the same as Execute(), but sets the ctx on the command. // Retrieve ctx by calling cmd.Context() inside your *Run lifecycle or ValidArgs // functions. func (c *Command) ExecuteContext(ctx context.Context) error { … } // Execute uses the args (os.Args[1:] by default) // and run through the command tree finding appropriate matches // for commands and then corresponding flags. func (c *Command) Execute() error { … } // ExecuteContextC is the same as ExecuteC(), but sets the ctx on the command. // Retrieve ctx by calling cmd.Context() inside your *Run lifecycle or ValidArgs // functions. func (c *Command) ExecuteContextC(ctx context.Context) (*Command, error) { … } // ExecuteC executes the command. func (c *Command) ExecuteC() (cmd *Command, err error) { … } func (c *Command) ValidateArgs(args []string) error { … } // ValidateRequiredFlags validates all required flags are present and returns an error otherwise func (c *Command) ValidateRequiredFlags() error { … } // checkCommandGroups checks if a command has been added to a group that does not exists. // If so, we panic because it indicates a coding error that should be corrected. func (c *Command) checkCommandGroups() { … } // InitDefaultHelpFlag adds default help flag to c. // It is called automatically by executing the c or by calling help and usage. // If c already has help flag, it will do nothing. func (c *Command) InitDefaultHelpFlag() { … } // InitDefaultVersionFlag adds default version flag to c. // It is called automatically by executing the c. // If c already has a version flag, it will do nothing. // If c.Version is empty, it will do nothing. func (c *Command) InitDefaultVersionFlag() { … } // InitDefaultHelpCmd adds default help command to c. // It is called automatically by executing the c or by calling help and usage. // If c already has help command or c has no subcommands, it will do nothing. func (c *Command) InitDefaultHelpCmd() { … } // ResetCommands delete parent, subcommand and help command from c. func (c *Command) ResetCommands() { … } type commandSorterByName … func (c commandSorterByName) Len() int { … } func (c commandSorterByName) Swap(i, j int) { … } func (c commandSorterByName) Less(i, j int) bool { … } // Commands returns a sorted slice of child commands. func (c *Command) Commands() []*Command { … } // AddCommand adds one or more commands to this parent command. func (c *Command) AddCommand(cmds ...*Command) { … } // Groups returns a slice of child command groups. func (c *Command) Groups() []*Group { … } // AllChildCommandsHaveGroup returns if all subcommands are assigned to a group func (c *Command) AllChildCommandsHaveGroup() bool { … } // ContainsGroup return if groupID exists in the list of command groups. func (c *Command) ContainsGroup(groupID string) bool { … } // AddGroup adds one or more command groups to this parent command. func (c *Command) AddGroup(groups ...*Group) { … } // RemoveCommand removes one or more commands from a parent command. func (c *Command) RemoveCommand(cmds ...*Command) { … } // Print is a convenience method to Print to the defined output, fallback to Stderr if not set. func (c *Command) Print(i ...interface{ … } // Println is a convenience method to Println to the defined output, fallback to Stderr if not set. func (c *Command) Println(i ...interface{ … } // Printf is a convenience method to Printf to the defined output, fallback to Stderr if not set. func (c *Command) Printf(format string, i ...interface{ … } // PrintErr is a convenience method to Print to the defined Err output, fallback to Stderr if not set. func (c *Command) PrintErr(i ...interface{ … } // PrintErrln is a convenience method to Println to the defined Err output, fallback to Stderr if not set. func (c *Command) PrintErrln(i ...interface{ … } // PrintErrf is a convenience method to Printf to the defined Err output, fallback to Stderr if not set. func (c *Command) PrintErrf(format string, i ...interface{ … } // CommandPath returns the full path to this command. func (c *Command) CommandPath() string { … } func (c *Command) displayName() string { … } // UseLine puts out the full usage for a given command (including parents). func (c *Command) UseLine() string { … } // DebugFlags used to determine which flags have been assigned to which commands // and which persist. func (c *Command) DebugFlags() { … } // Name returns the command's name: the first word in the use line. func (c *Command) Name() string { … } // HasAlias determines if a given string is an alias of the command. func (c *Command) HasAlias(s string) bool { … } // CalledAs returns the command name or alias that was used to invoke // this command or an empty string if the command has not been called. func (c *Command) CalledAs() string { … } // hasNameOrAliasPrefix returns true if the Name or any of aliases start // with prefix func (c *Command) hasNameOrAliasPrefix(prefix string) bool { … } // NameAndAliases returns a list of the command name and all aliases func (c *Command) NameAndAliases() string { … } // HasExample determines if the command has example. func (c *Command) HasExample() bool { … } // Runnable determines if the command is itself runnable. func (c *Command) Runnable() bool { … } // HasSubCommands determines if the command has children commands. func (c *Command) HasSubCommands() bool { … } // IsAvailableCommand determines if a command is available as a non-help command // (this includes all non deprecated/hidden commands). func (c *Command) IsAvailableCommand() bool { … } // IsAdditionalHelpTopicCommand determines if a command is an additional // help topic command; additional help topic command is determined by the // fact that it is NOT runnable/hidden/deprecated, and has no sub commands that // are runnable/hidden/deprecated. // Concrete example: https://github.com/spf13/cobra/issues/393#issuecomment-282741924. func (c *Command) IsAdditionalHelpTopicCommand() bool { … } // HasHelpSubCommands determines if a command has any available 'help' sub commands // that need to be shown in the usage/help default template under 'additional help // topics'. func (c *Command) HasHelpSubCommands() bool { … } // HasAvailableSubCommands determines if a command has available sub commands that // need to be shown in the usage/help default template under 'available commands'. func (c *Command) HasAvailableSubCommands() bool { … } // HasParent determines if the command is a child command. func (c *Command) HasParent() bool { … } // GlobalNormalizationFunc returns the global normalization function or nil if it doesn't exist. func (c *Command) GlobalNormalizationFunc() func(f *flag.FlagSet, name string) flag.NormalizedName { … } // Flags returns the complete FlagSet that applies // to this command (local and persistent declared here and by all parents). func (c *Command) Flags() *flag.FlagSet { … } // LocalNonPersistentFlags are flags specific to this command which will NOT persist to subcommands. // This function does not modify the flags of the current command, it's purpose is to return the current state. func (c *Command) LocalNonPersistentFlags() *flag.FlagSet { … } // LocalFlags returns the local FlagSet specifically set in the current command. // This function does not modify the flags of the current command, it's purpose is to return the current state. func (c *Command) LocalFlags() *flag.FlagSet { … } // InheritedFlags returns all flags which were inherited from parent commands. // This function does not modify the flags of the current command, it's purpose is to return the current state. func (c *Command) InheritedFlags() *flag.FlagSet { … } // NonInheritedFlags returns all flags which were not inherited from parent commands. // This function does not modify the flags of the current command, it's purpose is to return the current state. func (c *Command) NonInheritedFlags() *flag.FlagSet { … } // PersistentFlags returns the persistent FlagSet specifically set in the current command. func (c *Command) PersistentFlags() *flag.FlagSet { … } // ResetFlags deletes all flags from command. func (c *Command) ResetFlags() { … } // HasFlags checks if the command contains any flags (local plus persistent from the entire structure). func (c *Command) HasFlags() bool { … } // HasPersistentFlags checks if the command contains persistent flags. func (c *Command) HasPersistentFlags() bool { … } // HasLocalFlags checks if the command has flags specifically declared locally. func (c *Command) HasLocalFlags() bool { … } // HasInheritedFlags checks if the command has flags inherited from its parent command. func (c *Command) HasInheritedFlags() bool { … } // HasAvailableFlags checks if the command contains any flags (local plus persistent from the entire // structure) which are not hidden or deprecated. func (c *Command) HasAvailableFlags() bool { … } // HasAvailablePersistentFlags checks if the command contains persistent flags which are not hidden or deprecated. func (c *Command) HasAvailablePersistentFlags() bool { … } // HasAvailableLocalFlags checks if the command has flags specifically declared locally which are not hidden // or deprecated. func (c *Command) HasAvailableLocalFlags() bool { … } // HasAvailableInheritedFlags checks if the command has flags inherited from its parent command which are // not hidden or deprecated. func (c *Command) HasAvailableInheritedFlags() bool { … } // Flag climbs up the command tree looking for matching flag. func (c *Command) Flag(name string) (flag *flag.Flag) { … } // Recursively find matching persistent flag. func (c *Command) persistentFlag(name string) (flag *flag.Flag) { … } // ParseFlags parses persistent flag tree and local flags. func (c *Command) ParseFlags(args []string) error { … } // Parent returns a commands parent command. func (c *Command) Parent() *Command { … } // mergePersistentFlags merges c.PersistentFlags() to c.Flags() // and adds missing persistent flags of all parents. func (c *Command) mergePersistentFlags() { … } // updateParentsPflags updates c.parentsPflags by adding // new persistent flags of all parents. // If c.parentsPflags == nil, it makes new. func (c *Command) updateParentsPflags() { … } // commandNameMatches checks if two command names are equal // taking into account case sensitivity according to // EnableCaseInsensitive global configuration. func commandNameMatches(s string, t string) bool { … }