Search and commands

↑ ↓ to select · Enter to open · Esc to close

Type to search packages.

cobra

v0.0.0-20160702192511-5e2db986a612
github.com/tj/cobra

Documentation

pkg.go.dev ↗

Table of contents

↓ to select · Enter to jump· Shift+Enter for source

92 matches

Top ↑

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.

Constants

BashCompFilenameExt (4 declarations)

const (
	BashCompFilenameExt     = "cobra_annotation_bash_completion_filename_extentions"
	BashCompCustom          = "cobra_annotation_bash_completion_custom"
	BashCompOneRequiredFlag = "cobra_annotation_bash_completion_one_required_flag"
	BashCompSubdirsInDir    = "cobra_annotation_bash_completion_subdirs_in_dir"
)

Variables

EnableCommandSorting

var EnableCommandSorting = true

EnableCommandSorting controls sorting of the slice of commands, which is turned on by default. To disable sorting, set it to false.

EnablePrefixMatching

var EnablePrefixMatching = false

automatic prefix matching can be a dangerous thing to automatically enable in CLI tools. Set this to true to enable it

Functions

AddTemplateFunc

func AddTemplateFunc(name string, tmplFunc interface{})

AddTemplateFunc adds a template function that's available to Usage and Help template generation.

AddTemplateFuncs

func AddTemplateFuncs(tmplFuncs template.FuncMap)

AddTemplateFuncs adds multiple template functions availalble to Usage and Help template generation.

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.

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.

MarkFlagCustom

func MarkFlagCustom(flags *pflag.FlagSet, name string, f string) error

MarkFlagCustom adds the BashCompCustom annotation to the named flag in the flag set, if it exists. Generated bash autocompletion will call the bash function f for the flag.

MarkFlagFilename

func MarkFlagFilename(flags *pflag.FlagSet, name string, extensions ...string) error

MarkFlagFilename adds the BashCompFilenameExt annotation to the named flag in the flag set, if it exists. Generated bash autocompletion will select filenames for the flag, limiting to named extensions if provided.

MarkFlagRequired

func MarkFlagRequired(flags *pflag.FlagSet, name string) error

MarkFlagRequired adds the BashCompOneRequiredFlag annotation to the named flag in the flag set, if it exists.

OnInitialize

func OnInitialize(y ...func())

OnInitialize takes a series of func() arguments and appends them to a slice of func().

Types

Command

type Command struct {

	// The one-line usage message.
	Use string
	// An array of aliases that can be used instead of the first word in Use.
	Aliases []string
	// An array of command names for which this command will be suggested - similar to aliases but only suggests.
	SuggestFor []string
	// The short description shown in the 'help' output.
	Short string
	// The long message shown in the 'help <this-command>' output.
	Long string
	// Examples of how to use the command
	Example string
	// List of all valid non-flag arguments that are accepted in bash completions
	ValidArgs []string
	// List of aliases for ValidArgs. These are not suggested to the user in the bash
	// completion, but accepted if entered manually.
	ArgAliases []string
	// Custom functions used by the bash autocompletion generator
	BashCompletionFunction string
	// Is this command deprecated and should print this string when used?
	Deprecated string
	// Is this command hidden and should NOT show up in the list of available commands?
	Hidden bool

	// SilenceErrors is an option to quiet errors down stream
	SilenceErrors bool
	// Silence Usage is an option to silence usage when an error occurs.
	SilenceUsage bool
	// The *Run functions are executed in the following order:
	//   * PersistentPreRun()
	//   * PreRun()
	//   * Run()
	//   * PostRun()
	//   * PersistentPostRun()
	// All functions get the same args, the arguments after the command name
	// PersistentPreRun: children of this command will inherit and execute
	PersistentPreRun func(cmd *Command, args []string)
	// PersistentPreRunE: PersistentPreRun but returns an error
	PersistentPreRunE func(cmd *Command, args []string) error
	// PreRun: children of this command will not inherit.
	PreRun func(cmd *Command, args []string)
	// PreRunE: PreRun but returns an error
	PreRunE func(cmd *Command, args []string) error
	// Run: Typically the actual work function. Most commands will only implement this
	Run func(cmd *Command, args []string)
	// RunE: Run but returns an error
	RunE func(cmd *Command, args []string) error
	// PostRun: run after the Run command.
	PostRun func(cmd *Command, args []string)
	// PostRunE: PostRun but returns an error
	PostRunE func(cmd *Command, args []string) error
	// PersistentPostRun: children of this command will inherit and execute after PostRun
	PersistentPostRun func(cmd *Command, args []string)
	// PersistentPostRunE: PersistentPostRun but returns an error
	PersistentPostRunE func(cmd *Command, args []string) error
	// DisableAutoGenTag remove
	DisableAutoGenTag bool

	// Disable the suggestions based on Levenshtein distance that go along with 'unknown command' messages
	DisableSuggestions bool
	// If displaying suggestions, allows to set the minimum levenshtein distance to display, must be > 0
	SuggestionsMinimumDistance int

	// Disable the flag parsing. If this is true all flags will be passed to the command as arguments.
	DisableFlagParsing bool
	// contains filtered or unexported fields
}

Command is just that, a command for your application. eg. 'go run' ... 'run' is the command. Cobra requires you to define the usage and description as part of your command definition to ensure usability.

Command.AddCommand

func (c *Command) AddCommand(cmds ...*Command)

AddCommand adds one or more commands to this parent command.

Command.ArgsLenAtDash

func (c *Command) ArgsLenAtDash() int

ArgsLenAtDash will return the length of f.Args at the moment when a -- was found during arg parsing. This allows your program to know which args were before the -- and which came after. (Description from https://godoc.org/github.com/tj/pflag#FlagSet.ArgsLenAtDash).

Command.CommandPath

func (c *Command) CommandPath() string

CommandPath returns the full path to this command.

Command.CommandPathPadding

func (c *Command) CommandPathPadding() int

Command.Commands

func (c *Command) Commands() []*Command

Commands returns a sorted slice of child commands.

Command.DebugFlags

func (c *Command) DebugFlags()

For use in determining which flags have been assigned to which commands and which persist

Command.Execute

func (c *Command) Execute() error

Call execute to use the args (os.Args[1:] by default) and run through the command tree finding appropriate matches for commands and then corresponding flags.

Command.ExecuteC

func (c *Command) ExecuteC() (cmd *Command, err error)

Command.Find

func (c *Command) Find(args []string) (*Command, []string, error)

find the target command given the args and command tree Meant to be run on the highest node. Only searches down.

Command.Flag

func (c *Command) Flag(name string) (flag *flag.Flag)

Flag climbs up the command tree looking for matching flag

Command.Flags

func (c *Command) Flags() *flag.FlagSet

Get the complete FlagSet that applies to this command (local and persistent declared here and by all parents)

Command.GenBashCompletion

func (cmd *Command) GenBashCompletion(w io.Writer) error

Command.GenBashCompletionFile

func (cmd *Command) GenBashCompletionFile(filename string) error

Command.GlobalNormalizationFunc

func (c *Command) GlobalNormalizationFunc() func(f *flag.FlagSet, name string) flag.NormalizedName

GlobalNormalizationFunc returns the global normalization function or nil if doesn't exists

Command.HasAlias

func (c *Command) HasAlias(s string) bool

HasAlias determines if a given string is an alias of the command.

Command.HasAvailableFlags

func (c *Command) HasAvailableFlags() bool

Does the command contain any flags (local plus persistent from the entire structure) which are not hidden or deprecated

Command.HasAvailableInheritedFlags

func (c *Command) HasAvailableInheritedFlags() bool

Does the command have flags inherited from its parent command which are not hidden or deprecated

Command.HasAvailableLocalFlags

func (c *Command) HasAvailableLocalFlags() bool

Does the command has flags specifically declared locally which are not hidden or deprecated

Command.HasAvailablePersistentFlags

func (c *Command) HasAvailablePersistentFlags() bool

Does the command contain persistent flags which are not hidden or deprecated

Command.HasAvailableSubCommands

func (c *Command) HasAvailableSubCommands() bool

HasAvailableSubCommands determines if a command has available sub commands that need to be shown in the usage/help default template under 'available commands'

Command.HasExample

func (c *Command) HasExample() bool

Command.HasFlags

func (c *Command) HasFlags() bool

Does the command contain any flags (local plus persistent from the entire structure)

Command.HasHelpSubCommands

func (c *Command) HasHelpSubCommands() bool

HasHelpSubCommands determines if a command has any avilable 'help' sub commands that need to be shown in the usage/help default template under 'additional help topics'

Command.HasInheritedFlags

func (c *Command) HasInheritedFlags() bool

Does the command have flags inherited from its parent command

Command.HasLocalFlags

func (c *Command) HasLocalFlags() bool

Does the command has flags specifically declared locally

Command.HasParent

func (c *Command) HasParent() bool

Determine if the command is a child command

Command.HasPersistentFlags

func (c *Command) HasPersistentFlags() bool

Does the command contain persistent flags

Command.HasSubCommands

func (c *Command) HasSubCommands() bool

HasSubCommands determines if the command has children commands

Command.Help

func (c *Command) Help() error

Output the help for the command Used when a user calls help [command] by the default HelpFunc in the commander

Command.HelpFunc

func (c *Command) HelpFunc() func(*Command, []string)

HelpFunc returns either the function set by SetHelpFunc for this command or a parent, or it returns a function which calls c.Help()

Command.HelpTemplate

func (c *Command) HelpTemplate() string

Command.InheritedFlags

func (c *Command) InheritedFlags() *flag.FlagSet

All Flags which were inherited from parents commands

Command.IsAvailableCommand

func (c *Command) IsAvailableCommand() bool

IsAvailableCommand determines if a command is available as a non-help command (this includes all non deprecated/hidden commands)

Command.IsHelpCommand

func (c *Command) IsHelpCommand() bool

IsHelpCommand determines if a command is a 'help' command; a help command is determined by the fact that it is NOT runnable/hidden/deprecated, and has no sub commands that are runnable/hidden/deprecated

Command.LocalFlags

func (c *Command) LocalFlags() *flag.FlagSet

Get the local FlagSet specifically set in the current command

Command.LocalNonPersistentFlags

func (c *Command) LocalNonPersistentFlags() *flag.FlagSet

LocalNonPersistentFlags are flags specific to this command which will NOT persist to subcommands

Command.MarkFlagCustom

func (cmd *Command) MarkFlagCustom(name string, f string) error

MarkFlagCustom adds the BashCompCustom annotation to the named flag, if it exists. Generated bash autocompletion will call the bash function f for the flag.

Command.MarkFlagFilename

func (cmd *Command) MarkFlagFilename(name string, extensions ...string) error

MarkFlagFilename adds the BashCompFilenameExt annotation to the named flag, if it exists. Generated bash autocompletion will select filenames for the flag, limiting to named extensions if provided.

Command.MarkFlagRequired

func (cmd *Command) MarkFlagRequired(name string) error

MarkFlagRequired adds the BashCompOneRequiredFlag annotation to the named flag, if it exists.

Command.MarkPersistentFlagFilename

func (cmd *Command) MarkPersistentFlagFilename(name string, extensions ...string) error

MarkPersistentFlagFilename adds the BashCompFilenameExt annotation to the named persistent flag, if it exists. Generated bash autocompletion will select filenames for the flag, limiting to named extensions if provided.

Command.MarkPersistentFlagRequired

func (cmd *Command) MarkPersistentFlagRequired(name string) error

MarkPersistentFlagRequired adds the BashCompOneRequiredFlag annotation to the named persistent flag, if it exists.

Command.Name

func (c *Command) Name() string

Name returns the command's name: the first word in the use line.

Command.NameAndAliases

func (c *Command) NameAndAliases() string

Command.NamePadding

func (c *Command) NamePadding() int

Command.NonInheritedFlags

func (c *Command) NonInheritedFlags() *flag.FlagSet

All Flags which were not inherited from parent commands

Command.Out

func (c *Command) Out() io.Writer

Command.Parent

func (c *Command) Parent() *Command

Parent returns a commands parent command

Command.ParseFlags

func (c *Command) ParseFlags(args []string) (err error)

ParseFlags parses persistent flag tree & local flags

Command.PersistentFlags

func (c *Command) PersistentFlags() *flag.FlagSet

Get the Persistent FlagSet specifically set in the current command

Command.Print

func (c *Command) Print(i ...interface{})

Print is a convenience method to Print to the defined output

Command.Printf

func (c *Command) Printf(format string, i ...interface{})

Printf is a convenience method to Printf to the defined output

Command.Println

func (c *Command) Println(i ...interface{})

Println is a convenience method to Println to the defined output

Command.RemoveCommand

func (c *Command) RemoveCommand(cmds ...*Command)

RemoveCommand removes one or more commands from a parent command.

Command.ResetCommands

func (c *Command) ResetCommands()

Used for testing

Command.ResetFlags

func (c *Command) ResetFlags()

For use in testing

Command.Root

func (c *Command) Root() *Command

Command.Runnable

func (c *Command) Runnable() bool

Runnable determines if the command is itself runnable

Command.SetArgs

func (c *Command) SetArgs(a []string)

os.Args[1:] by default, if desired, can be overridden particularly useful when testing.

Command.SetGlobalNormalizationFunc

func (c *Command) SetGlobalNormalizationFunc(n func(f *flag.FlagSet, name string) flag.NormalizedName)

SetGlobalNormalizationFunc sets a normalization function to all flag sets and also to child commands. The user should not have a cyclic dependency on commands.

Command.SetHelpCommand

func (c *Command) SetHelpCommand(cmd *Command)

Command.SetHelpFunc

func (c *Command) SetHelpFunc(f func(*Command, []string))

Can be defined by Application

Command.SetHelpTemplate

func (c *Command) SetHelpTemplate(s string)

Can be defined by Application

Command.SetOutput

func (c *Command) SetOutput(output io.Writer)

SetOutput sets the destination for usage and error messages. If output is nil, os.Stderr is used.

Command.SetUsageFunc

func (c *Command) SetUsageFunc(f func(*Command) error)

Usage can be defined by application

Command.SetUsageTemplate

func (c *Command) SetUsageTemplate(s string)

Can be defined by Application

Command.SuggestionsFor

func (c *Command) SuggestionsFor(typedName string) []string

Command.Usage

func (c *Command) Usage() error

Output the usage for the command Used when a user provides invalid input Can be defined by user by overriding UsageFunc

Command.UsageFunc

func (c *Command) UsageFunc() (f func(*Command) error)

Command.UsagePadding

func (c *Command) UsagePadding() int

Command.UsageString

func (c *Command) UsageString() string

Command.UsageTemplate

func (c *Command) UsageTemplate() string

Command.UseLine

func (c *Command) UseLine() string

The full usage for a given command (including parents)

Command.VisitParents

func (c *Command) VisitParents(fn func(*Command))

Dependency graph