Documentation
¶
Overview ¶
Package cobra is a commander providing a simple interface to create powerful modern CLI interfaces. In addition to providing an interface, Cobra simultaneously provides a controller to organize your application code.
Index ¶
- Constants
- Variables
- func AddTemplateFunc(name string, tmplFunc interface{})
- func AddTemplateFuncs(tmplFuncs template.FuncMap)
- func ArbitraryArgs(cmd *Command, args []string) error
- func CheckErr(msg interface{})
- func CompDebug(msg string, printToStdErr bool)
- func CompDebugln(msg string, printToStdErr bool)
- func CompError(msg string)
- func CompErrorln(msg string)
- func Eq(a interface{}, b interface{}) bool
- func GetActiveHelpConfig(cmd *Command) string
- func Gt(a interface{}, b interface{}) bool
- func MarkFlagCustom(flags *pflag.FlagSet, name string, f string) error
- func MarkFlagDirname(flags *pflag.FlagSet, name string) error
- func MarkFlagFilename(flags *pflag.FlagSet, name string, extensions ...string) error
- func MarkFlagRequired(flags *pflag.FlagSet, name string) error
- func NoArgs(cmd *Command, args []string) error
- func NoFileCompletions(cmd *Command, args []string, toComplete string) ([]Completion, ShellCompDirective)
- func OnFinalize(y ...func())
- func OnInitialize(y ...func())
- func OnlyValidArgs(cmd *Command, args []string) error
- func WriteStringAndCheck(b io.StringWriter, s string)
- type Command
- func (c *Command) AddCommand(cmds ...*Command)
- func (c *Command) AddGroup(groups ...*Group)
- func (c *Command) AllChildCommandsHaveGroup() bool
- func (c *Command) ArgsLenAtDash() int
- func (c *Command) CalledAs() string
- func (c *Command) CommandPath() string
- func (c *Command) CommandPathPadding() int
- func (c *Command) Commands() []*Command
- func (c *Command) ContainsGroup(groupID string) bool
- func (c *Command) Context() context.Context
- func (c *Command) DebugFlags()
- func (c *Command) DisplayName() string
- func (c *Command) ErrOrStderr() io.Writer
- func (c *Command) ErrPrefix() string
- func (c *Command) Execute() error
- func (c *Command) ExecuteC() (cmd *Command, err error)
- func (c *Command) ExecuteContext(ctx context.Context) error
- func (c *Command) ExecuteContextC(ctx context.Context) (*Command, error)
- func (c *Command) Find(args []string) (*Command, []string, error)
- func (c *Command) Flag(name string) (flag *flag.Flag)
- func (c *Command) FlagErrorFunc() (f func(*Command, error) error)
- func (c *Command) Flags() *flag.FlagSet
- func (c *Command) GenBashCompletion(w io.Writer) error
- func (c *Command) GenBashCompletionFile(filename string) error
- func (c *Command) GenBashCompletionFileV2(filename string, includeDesc bool) error
- func (c *Command) GenBashCompletionV2(w io.Writer, includeDesc bool) error
- func (c *Command) GenFishCompletion(w io.Writer, includeDesc bool) error
- func (c *Command) GenFishCompletionFile(filename string, includeDesc bool) error
- func (c *Command) GenPowerShellCompletion(w io.Writer) error
- func (c *Command) GenPowerShellCompletionFile(filename string) error
- func (c *Command) GenPowerShellCompletionFileWithDesc(filename string) error
- func (c *Command) GenPowerShellCompletionWithDesc(w io.Writer) error
- func (c *Command) GenZshCompletion(w io.Writer) error
- func (c *Command) GenZshCompletionFile(filename string) error
- func (c *Command) GenZshCompletionFileNoDesc(filename string) error
- func (c *Command) GenZshCompletionNoDesc(w io.Writer) error
- func (c *Command) GetFlagCompletionFunc(flagName string) (CompletionFunc, bool)
- func (c *Command) GlobalNormalizationFunc() func(f *flag.FlagSet, name string) flag.NormalizedName
- func (c *Command) Groups() []*Group
- func (c *Command) HasAlias(s string) bool
- func (c *Command) HasAvailableFlags() bool
- func (c *Command) HasAvailableInheritedFlags() bool
- func (c *Command) HasAvailableLocalFlags() bool
- func (c *Command) HasAvailablePersistentFlags() bool
- func (c *Command) HasAvailableSubCommands() bool
- func (c *Command) HasExample() bool
- func (c *Command) HasFlags() bool
- func (c *Command) HasHelpSubCommands() bool
- func (c *Command) HasInheritedFlags() bool
- func (c *Command) HasLocalFlags() bool
- func (c *Command) HasParent() bool
- func (c *Command) HasPersistentFlags() bool
- func (c *Command) HasSubCommands() bool
- func (c *Command) Help() error
- func (c *Command) HelpFunc() func(*Command, []string)
- func (c *Command) HelpTemplate() string
- func (c *Command) InOrStdin() io.Reader
- func (c *Command) InheritedFlags() *flag.FlagSet
- func (c *Command) InitDefaultCompletionCmd(args ...string)
- func (c *Command) InitDefaultHelpCmd()
- func (c *Command) InitDefaultHelpFlag()
- func (c *Command) InitDefaultVersionFlag()
- func (c *Command) IsAdditionalHelpTopicCommand() bool
- func (c *Command) IsAvailableCommand() bool
- func (c *Command) LocalFlags() *flag.FlagSet
- func (c *Command) LocalNonPersistentFlags() *flag.FlagSet
- func (c *Command) MarkFlagCustom(name string, f string) error
- func (c *Command) MarkFlagDirname(name string) error
- func (c *Command) MarkFlagFilename(name string, extensions ...string) error
- func (c *Command) MarkFlagRequired(name string) error
- func (c *Command) MarkFlagsMutuallyExclusive(flagNames ...string)
- func (c *Command) MarkFlagsOneRequired(flagNames ...string)
- func (c *Command) MarkFlagsRequiredTogether(flagNames ...string)
- func (c *Command) MarkPersistentFlagDirname(name string) error
- func (c *Command) MarkPersistentFlagFilename(name string, extensions ...string) error
- func (c *Command) MarkPersistentFlagRequired(name string) error
- func (c *Command) MarkZshCompPositionalArgumentFile(argPosition int, patterns ...string) error
- func (c *Command) MarkZshCompPositionalArgumentWords(argPosition int, words ...string) error
- func (c *Command) Name() string
- func (c *Command) NameAndAliases() string
- func (c *Command) NamePadding() int
- func (c *Command) NonInheritedFlags() *flag.FlagSet
- func (c *Command) OutOrStderr() io.Writer
- func (c *Command) OutOrStdout() io.Writer
- func (c *Command) Parent() *Command
- func (c *Command) ParseFlags(args []string) error
- func (c *Command) PersistentFlags() *flag.FlagSet
- func (c *Command) Print(i ...interface{})
- func (c *Command) PrintErr(i ...interface{})
- func (c *Command) PrintErrf(format string, i ...interface{})
- func (c *Command) PrintErrln(i ...interface{})
- func (c *Command) Printf(format string, i ...interface{})
- func (c *Command) Println(i ...interface{})
- func (c *Command) RegisterFlagCompletionFunc(flagName string, f CompletionFunc) error
- func (c *Command) RemoveCommand(cmds ...*Command)
- func (c *Command) ResetCommands()
- func (c *Command) ResetFlags()
- func (c *Command) Root() *Command
- func (c *Command) Runnable() bool
- func (c *Command) SetArgs(a []string)
- func (c *Command) SetCompletionCommandGroupID(groupID string)
- func (c *Command) SetContext(ctx context.Context)
- func (c *Command) SetErr(newErr io.Writer)
- func (c *Command) SetErrPrefix(s string)
- func (c *Command) SetFlagErrorFunc(f func(*Command, error) error)
- func (c *Command) SetGlobalNormalizationFunc(n func(f *flag.FlagSet, name string) flag.NormalizedName)
- func (c *Command) SetHelpCommand(cmd *Command)
- func (c *Command) SetHelpCommandGroupID(groupID string)
- func (c *Command) SetHelpFunc(f func(*Command, []string))
- func (c *Command) SetHelpTemplate(s string)
- func (c *Command) SetIn(newIn io.Reader)
- func (c *Command) SetOut(newOut io.Writer)
- func (c *Command) SetOutput(output io.Writer)deprecated
- func (c *Command) SetUsageFunc(f func(*Command) error)
- func (c *Command) SetUsageTemplate(s string)
- func (c *Command) SetVersionTemplate(s string)
- func (c *Command) SuggestionsFor(typedName string) []string
- func (c *Command) Traverse(args []string) (*Command, []string, error)
- func (c *Command) Usage() error
- func (c *Command) UsageFunc() (f func(*Command) error)
- func (c *Command) UsagePadding() int
- func (c *Command) UsageString() string
- func (c *Command) UsageTemplate() string
- func (c *Command) UseLine() string
- func (c *Command) ValidateArgs(args []string) error
- func (c *Command) ValidateFlagGroups() error
- func (c *Command) ValidateRequiredFlags() error
- func (c *Command) VersionTemplate() string
- func (c *Command) VisitParents(fn func(*Command))
- type Completion
- type CompletionFunc
- type CompletionOptions
- type FParseErrWhitelist
- type Group
- type PositionalArgs
- type ShellCompDirective
- type SliceValue
Constants ¶
const ( BashCompFilenameExt = "cobra_annotation_bash_completion_filename_extensions" BashCompCustom = "cobra_annotation_bash_completion_custom" BashCompOneRequiredFlag = "cobra_annotation_bash_completion_one_required_flag" BashCompSubdirsInDir = "cobra_annotation_bash_completion_subdirs_in_dir" )
Annotations for Bash completion.
const ( FlagSetByCobraAnnotation = "cobra_annotation_flag_set_by_cobra" CommandDisplayNameAnnotation = "cobra_annotation_command_display_name" )
const ( // ShellCompRequestCmd is the name of the hidden command that is used to request // completion results from the program. It is used by the shell completion scripts. ShellCompRequestCmd = "__complete" // ShellCompNoDescRequestCmd is the name of the hidden command that is used to request // completion results without their description. It is used by the shell completion scripts. ShellCompNoDescRequestCmd = "__completeNoDesc" )
Variables ¶
var EnableCaseInsensitive = defaultCaseInsensitive
EnableCaseInsensitive allows case-insensitive commands names. (case sensitive by default)
var EnableCommandSorting = defaultCommandSorting
EnableCommandSorting controls sorting of the slice of commands, which is turned on by default. To disable sorting, set it to false.
var EnablePrefixMatching = defaultPrefixMatching
EnablePrefixMatching allows setting automatic prefix matching. Automatic prefix matching can be a dangerous thing to automatically enable in CLI tools. Set this to true to enable it.
var EnableTraverseRunHooks = defaultTraverseRunHooks
EnableTraverseRunHooks executes persistent pre-run and post-run hooks from all parents. By default this is disabled, which means only the first run hook to be found is executed.
var MousetrapDisplayDuration = 5 * time.Second
MousetrapDisplayDuration controls how long the MousetrapHelpText message is displayed on Windows if the CLI is started from explorer.exe. Set to 0 to wait for the return key to be pressed. To disable the mousetrap, just set MousetrapHelpText to blank string (""). Works only on Microsoft Windows.
var MousetrapHelpText = `This is a command line tool.
You need to open cmd.exe and run it from there.
`
MousetrapHelpText enables an information splash screen on Windows if the CLI is started from explorer.exe. To disable the mousetrap, just set this variable to blank string (""). Works only on Microsoft Windows.
Functions ¶
func AddTemplateFunc ¶
func AddTemplateFunc(name string, tmplFunc interface{})
AddTemplateFunc adds a template function that's available to Usage and Help template generation.
func AddTemplateFuncs ¶
AddTemplateFuncs adds multiple template functions that are available to Usage and Help template generation.
func ArbitraryArgs ¶
ArbitraryArgs never returns an error.
func CheckErr ¶ added in v1.1.2
func CheckErr(msg interface{})
CheckErr prints the msg with the prefix 'Error:' and exits with error code 1. If the msg is nil, it does nothing.
func CompDebug ¶ added in v1.0.0
CompDebug prints the specified string to the same file as where the completion script prints its logs. Note that completion printouts should never be on stdout as they would be wrongly interpreted as actual completion choices by the completion script.
func CompDebugln ¶ added in v1.0.0
CompDebugln prints the specified string with a newline at the end to the same file as where the completion script prints its logs. Such logs are only printed when the user has set the environment variable BASH_COMP_DEBUG_FILE to the path of some file to be used.
func CompError ¶ added in v1.0.0
func CompError(msg string)
CompError prints the specified completion message to stderr.
func CompErrorln ¶ added in v1.0.0
func CompErrorln(msg string)
CompErrorln prints the specified completion message to stderr with a newline at the end.
func Eq ¶
func Eq(a interface{}, b interface{}) bool
Eq takes two types and checks whether they are equal. Supported types are int and string. Unsupported types will panic.
func GetActiveHelpConfig ¶ added in v1.5.0
GetActiveHelpConfig returns the value of the ActiveHelp environment variable <PROGRAM>_ACTIVE_HELP where <PROGRAM> is the name of the root command in upper case, with all non-ASCII-alphanumeric characters replaced by `_`. It will always return "0" if the global environment variable COBRA_ACTIVE_HELP is set to "0".
func Gt ¶
func Gt(a interface{}, b interface{}) bool
Gt takes two types and checks whether the first type is greater than the second. In case of types Arrays, Chans, Maps and Slices, Gt will compare their lengths. Ints are compared directly while strings are first parsed as ints and then compared.
func MarkFlagCustom ¶
MarkFlagCustom adds the BashCompCustom annotation to the named flag, if it exists. The bash completion script will call the bash function f for the flag.
This will only work for bash completion. It is recommended to instead use c.RegisterFlagCompletionFunc(...) which allows to register a Go function which will work across all shells.
func MarkFlagDirname ¶ added in v0.0.5
MarkFlagDirname instructs the various shell completion implementations to limit completions for the named flag to directory names.
func MarkFlagFilename ¶
MarkFlagFilename instructs the various shell completion implementations to limit completions for the named flag to the specified file extensions.
func MarkFlagRequired ¶
MarkFlagRequired instructs the various shell completion implementations to prioritize the named flag when performing completion, and causes your command to report an error if invoked without the flag.