Skip to content
Myr

Edit documentation

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by bogem · committed by bogem · parent 101d434 · Browse files · 2 files+64 added−63 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+64 added−63 removed

  • other:+64 added−63 removed

2 files+64 added−63 removed

modified:cobra.go+16 added-16 removed
35 unmodified lines
36}3738var initializers []func()3940// automatic prefix matching can be a dangerous thing to automatically enable in CLI tools.41// Set this to true to enable it40// Automatic prefix matching can be a dangerous thing to automatically enable in CLI tools.41// Set this to true to enable it.42var EnablePrefixMatching = false4344//EnableCommandSorting controls sorting of the slice of commands, which is turned on by default.45//To disable sorting, set it to false.44// EnableCommandSorting controls sorting of the slice of commands, which is turned on by default.45// To disable sorting, set it to false.46var EnableCommandSorting = true4748//AddTemplateFunc adds a template function that's available to Usage and Help49//template generation.48// AddTemplateFunc adds a template function that's available to Usage and Help49// template generation.50func AddTemplateFunc(name string, tmplFunc interface{}) {51 templateFuncs[name] = tmplFunc52}5354//AddTemplateFuncs adds multiple template functions availalble to Usage and55//Help template generation.54// AddTemplateFuncs adds multiple template functions availalble to Usage and55// Help template generation.56func AddTemplateFuncs(tmplFuncs template.FuncMap) {57 for k, v := range tmplFuncs {58 templateFuncs[k] = v59 }60}6162//OnInitialize takes a series of func() arguments and appends them to a slice of func().62// OnInitialize takes a series of func() arguments and appends them to a slice of func().63func OnInitialize(y ...func()) {64 initializers = append(initializers, y...)65}6667//Gt takes two types and checks whether the first type is greater than the second. In case of types Arrays, Chans,68//Maps and Slices, Gt will compare their lengths. Ints are compared directly while strings are first parsed as69//ints and then compared.67// Gt takes two types and checks whether the first type is greater than the second. In case of types Arrays, Chans,68// Maps and Slices, Gt will compare their lengths. Ints are compared directly while strings are first parsed as69// ints and then compared.70func Gt(a interface{}, b interface{}) bool {71 var left, right int6472 av := reflect.ValueOf(a)73
19 unmodified lines
9394 return left > right95}9697//Eq takes two types and checks whether they are equal. Supported types are int and string. Unsupported types will panic.97// Eq takes two types and checks whether they are equal. Supported types are int and string. Unsupported types will panic.98func Eq(a interface{}, b interface{}) bool {99 av := reflect.ValueOf(a)100 bv := reflect.ValueOf(b)101
11 unmodified lines
113func trimRightSpace(s string) string {114 return strings.TrimRightFunc(s, unicode.IsSpace)115}116117// appendIfNotPresent will append stringToAppend to the end of s, but only if it's not yet present in s117// appendIfNotPresent will append stringToAppend to the end of s, but only if it's not yet present in s.118func appendIfNotPresent(s, stringToAppend string) string {119 if strings.Contains(s, stringToAppend) {120 return s121 }122 return s + " " + stringToAppend123}124125//rpad adds padding to the right of a string125// rpad adds padding to the right of a string.126func rpad(s string, padding int) string {127 template := fmt.Sprintf("%%-%ds", padding)128 return fmt.Sprintf(template, s)129}
5 unmodified lines
135 template.Must(t.Parse(text))136 return t.Execute(w, data)137}138139// ld compares two strings and returns the levenshtein distance between them139// ld compares two strings and returns the levenshtein distance between them.140func ld(s, t string, ignoreCase bool) int {141 if ignoreCase {142 s = strings.ToLower(s)143 t = strings.ToLower(t)
30 unmodified lines
modified:command.go+48 added-47 removed
138 unmodified lines
139func (c *Command) SetOutput(output io.Writer) {140 c.output = &output141}142143// Usage can be defined by application143// Usage can be defined by application.144func (c *Command) SetUsageFunc(f func(*Command) error) {145 c.usageFunc = f146}147148// Can be defined by Application148// Can be defined by Application.149func (c *Command) SetUsageTemplate(s string) {150 c.usageTemplate = s151}152153// Can be defined by Application153// Can be defined by Application.154func (c *Command) SetHelpFunc(f func(*Command, []string)) {155 c.helpFunc = f156}157158func (c *Command) SetHelpCommand(cmd *Command) {159 c.helpCommand = cmd160}161162// Can be defined by Application162// Can be defined by Application.163func (c *Command) SetHelpTemplate(s string) {164 c.helpTemplate = s165}166
27 unmodified lines
194 return def195}196197// UsageFunc returns either the function set by SetUsageFunc for this command198// or a parent, or it returns a default usage function198// or a parent, or it returns a default usage function.199func (c *Command) UsageFunc() (f func(*Command) error) {200 if c.usageFunc != nil {201 return c.usageFunc202 }
10 unmodified lines
213 return err214 }215}216217// Output the usage for the command218// Used when a user provides invalid input219// Can be defined by user by overriding UsageFunc217// Usage puts out the usage for the command.218// Used when a user provides invalid input.219// Can be defined by user by overriding UsageFunc.220func (c *Command) Usage() error {221 return c.UsageFunc()(c)222}223224// HelpFunc returns either the function set by SetHelpFunc for this command225// or a parent, or it returns a function with default help behavior225// or a parent, or it returns a function with default help behavior.226func (c *Command) HelpFunc() func(*Command, []string) {227 cmd := c228 for cmd != nil {229 if cmd.helpFunc != nil {
9 unmodified lines
239 }240 }241}242243// Output the help for the command244// Used when a user calls help [command]245// Can be defined by user by overriding HelpFunc243// Help puts out the help for the command.244// Used when a user calls help [command].245// Can be defined by user by overriding HelpFunc.246func (c *Command) Help() error {247 c.HelpFunc()(c, []string{})248 return nil249}
82 unmodified lines
332333{{end}}{{if or .Runnable .HasSubCommands}}{{.UsageString}}{{end}}`334}335336// Really only used when casting a command to a commander336// Really only used when casting a command to a commander.337func (c *Command) resetChildrensParents() {338 for _, x := range c.commands {339 x.parent = c340 }
403 unmodified lines
744 }745 c.AddCommand(c.helpCommand)746}747748// Used for testing748// Used for testing.749func (c *Command) ResetCommands() {750 c.commands = nil751 c.helpCommand = nil752}753754// Sorts commands by their names754// Sorts commands by their names.755type commandSorterByName []*Command756757func (c commandSorterByName) Len() int { return len(c) }758func (c commandSorterByName) Swap(i, j int) { c[i], c[j] = c[j], c[i] }
71 unmodified lines
830 }831 }832}833834// Print is a convenience method to Print to the defined output, fallback to Stderr if not set834// Print is a convenience method to Print to the defined output, fallback to Stderr if not set.835func (c *Command) Print(i ...interface{}) {836 fmt.Fprint(c.OutOrStderr(), i...)837}838839// Println is a convenience method to Println to the defined output, fallback to Stderr if not set839// Println is a convenience method to Println to the defined output, fallback to Stderr if not set.840func (c *Command) Println(i ...interface{}) {841 str := fmt.Sprintln(i...)842 c.Print(str)843}844845// Printf is a convenience method to Printf to the defined output, fallback to Stderr if not set845// Printf is a convenience method to Printf to the defined output, fallback to Stderr if not set.846func (c *Command) Printf(format string, i ...interface{}) {847 str := fmt.Sprintf(format, i...)848 c.Print(str)849}
8 unmodified lines
858 }859 return str860}861862//The full usage for a given command (including parents)862// UseLine puts out the full usage for a given command (including parents).863func (c *Command) UseLine() string {864 str := ""865 if c.HasParent() {866 str = c.parent.CommandPath() + " "
1 unmodified line
868 return str + c.Use869}870871// For use in determining which flags have been assigned to which commands872// and which persist872// and which persist.873func (c *Command) DebugFlags() {874 c.Println("DebugFlags called on", c.Name())875 var debugflags func(*Command)876
66 unmodified lines
943func (c *Command) HasExample() bool {944 return len(c.Example) > 0945}946947// Runnable determines if the command is itself runnable947// Runnable determines if the command is itself runnable.948func (c *Command) Runnable() bool {949 return c.Run != nil || c.RunE != nil950}951952// HasSubCommands determines if the command has children commands952// HasSubCommands determines if the command has children commands.953func (c *Command) HasSubCommands() bool {954 return len(c.commands) > 0955}956957// IsAvailableCommand determines if a command is available as a non-help command958// (this includes all non deprecated/hidden commands)958// (this includes all non deprecated/hidden commands).959func (c *Command) IsAvailableCommand() bool {960 if len(c.Deprecated) != 0 || c.Hidden {961 return false962 }
10 unmodified lines
973}974975// IsHelpCommand determines if a command is a 'help' command; a help command is976// determined by the fact that it is NOT runnable/hidden/deprecated, and has no977// sub commands that are runnable/hidden/deprecated977// sub commands that are runnable/hidden/deprecated.978func (c *Command) IsHelpCommand() bool {979980 // if a command is runnable, deprecated, or hidden it is not a 'help' command981 if c.Runnable() || len(c.Deprecated) != 0 || c.Hidden {
12 unmodified lines
994}995996// HasHelpSubCommands determines if a command has any available 'help' sub commands997// that need to be shown in the usage/help default template under 'additional help998// topics'998// topics'.999func (c *Command) HasHelpSubCommands() bool {10001001 // return true on the first found available 'help' sub command1002 for _, sub := range c.commands {
6 unmodified lines
1009 return false1010}10111012// HasAvailableSubCommands determines if a command has available sub commands that1013// need to be shown in the usage/help default template under 'available commands'1013// need to be shown in the usage/help default template under 'available commands'.1014func (c *Command) HasAvailableSubCommands() bool {10151016 // return true on the first found available (non deprecated/help/hidden)1017 // sub command
7 unmodified lines
1025 // sub commands1026 return false1027}10281029// Determine if the command is a child command1029// HasParent determines if the command is a child command.1030func (c *Command) HasParent() bool {1031 return c.parent != nil1032}10331034// GlobalNormalizationFunc returns the global normalization function or nil if doesn't exists1034// GlobalNormalizationFunc returns the global normalization function or nil if doesn't exists.1035func (c *Command) GlobalNormalizationFunc() func(f *flag.FlagSet, name string) flag.NormalizedName {1036 return c.globNormFunc1037}10381039// Get the complete FlagSet that applies to this command (local and persistent declared here and by all parents)1039// Flage returns the complete FlagSet that applies1040// to this command (local and persistent declared here and by all parents).1041func (c *Command) Flags() *flag.FlagSet {1042 if c.flags == nil {1043 c.flags = flag.NewFlagSet(c.Name(), flag.ContinueOnError)1044 if c.flagErrorBuf == nil {
3 unmodified lines
1048 }1049 return c.flags1050}10511051// LocalNonPersistentFlags are flags specific to this command which will NOT persist to subcommands1052// LocalNonPersistentFlags are flags specific to this command which will NOT persist to subcommands.1053func (c *Command) LocalNonPersistentFlags() *flag.FlagSet {1054 persistentFlags := c.PersistentFlags()10551056 out := flag.NewFlagSet(c.Name(), flag.ContinueOnError)
4 unmodified lines
1061 })1062 return out1063}10641064// Get the local FlagSet specifically set in the current command1065// LocalFlags returns the local FlagSet specifically set in the current command.1066func (c *Command) LocalFlags() *flag.FlagSet {1067 c.mergePersistentFlags()10681069 local := flag.NewFlagSet(c.Name(), flag.ContinueOnError)
9 unmodified lines
1079 }1080 return local1081}10821082// All Flags which were inherited from parents commands1083// InheritedFlags returns all flags which were inherited from parents commands.1084func (c *Command) InheritedFlags() *flag.FlagSet {1085 c.mergePersistentFlags()10861087 inherited := flag.NewFlagSet(c.Name(), flag.ContinueOnError)
20 unmodified lines
11081109 return inherited1110}11111111// All Flags which were not inherited from parent commands1112// NonInheritedFlags returns all flags which were not inherited from parent commands.1113func (c *Command) NonInheritedFlags() *flag.FlagSet {1114 return c.LocalFlags()1115}11161116// Get the Persistent FlagSet specifically set in the current command1117// PersistentFlags returns the persistent FlagSet specifically set in the current command.1118func (c *Command) PersistentFlags() *flag.FlagSet {1119 if c.pflags == nil {1120 c.pflags = flag.NewFlagSet(c.Name(), flag.ContinueOnError)1121 if c.flagErrorBuf == nil {
3 unmodified lines
1125 }1126 return c.pflags1127}11281128// For use in testing1129// ResetFlags is used in testing.1130func (c *Command) ResetFlags() {1131 c.flagErrorBuf = new(bytes.Buffer)1132 c.flagErrorBuf.Reset()1133 c.flags = flag.NewFlagSet(c.Name(), flag.ContinueOnError)
1 unmodified line
1135 c.pflags = flag.NewFlagSet(c.Name(), flag.ContinueOnError)1136 c.pflags.SetOutput(c.flagErrorBuf)1137}11381138// Does the command contain any flags (local plus persistent from the entire structure)1139// Does the command contain any flags (local plus persistent from the entire structure).1140func (c *Command) HasFlags() bool {1141 return c.Flags().HasFlags()1142}11431143// Does the command contain persistent flags1144// Does the command contain persistent flags.1145func (c *Command) HasPersistentFlags() bool {1146 return c.PersistentFlags().HasFlags()1147}11481148// Does the command has flags specifically declared locally1149// Does the command has flags specifically declared locally.1150func (c *Command) HasLocalFlags() bool {1151 return c.LocalFlags().HasFlags()1152}11531153// Does the command have flags inherited from its parent command1154// Does the command have flags inherited from its parent command.1155func (c *Command) HasInheritedFlags() bool {1156 return c.InheritedFlags().HasFlags()1157}11581159// Does the command contain any flags (local plus persistent from the entire1159// structure) which are not hidden or deprecated1160// structure) which are not hidden or deprecated.1161func (c *Command) HasAvailableFlags() bool {1162 return c.Flags().HasAvailableFlags()1163}11641164// Does the command contain persistent flags which are not hidden or deprecated1165// Does the command contain persistent flags which are not hidden or deprecated.1166func (c *Command) HasAvailablePersistentFlags() bool {1167 return c.PersistentFlags().HasAvailableFlags()1168}11691170// Does the command has flags specifically declared locally which are not hidden1170// or deprecated1171// or deprecated.1172func (c *Command) HasAvailableLocalFlags() bool {1173 return c.LocalFlags().HasAvailableFlags()1174}11751176// Does the command have flags inherited from its parent command which are1176// not hidden or deprecated1177// not hidden or deprecated.1178func (c *Command) HasAvailableInheritedFlags() bool {1179 return c.InheritedFlags().HasAvailableFlags()1180}11811181// Flag climbs up the command tree looking for matching flag1182// Flag climbs up the command tree looking for matching flag.1183func (c *Command) Flag(name string) (flag *flag.Flag) {1184 flag = c.Flags().Lookup(name)11851186 if flag == nil {
2 unmodified lines
11891190 return1191}11921192// recursively find matching persistent flag1193// Recursively find matching persistent flag.1194func (c *Command) persistentFlag(name string) (flag *flag.Flag) {1195 if c.HasPersistentFlags() {1196 flag = c.PersistentFlags().Lookup(name)1197 }
3 unmodified lines
1201 }1202 return1203}12041204// ParseFlags parses persistent flag tree & local flags1205// ParseFlags parses persistent flag tree and local flags.1206func (c *Command) ParseFlags(args []string) (err error) {1207 if c.DisableFlagParsing {1208 return nil1209 }
1 unmodified line
1211 err = c.Flags().Parse(args)1212 return1213}12141214// Parent returns a commands parent command1215// Parent returns a commands parent command.1216func (c *Command) Parent() *Command {1217 return c.parent1218}1219
38 unmodified lines