Merge pull request #339 from bogem/fixes
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Some fixes
authored by Eric Paris · committed by GitHub · parents d671d98, 5da9888 · Browse files · 3 files·+67 added−70 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+67 added−70 removed
- other:+67 added−70 removed
Provenance
- Commit
9c28e4b- Tree
21e864b- Author
Eric Paris <eparis@redhat.com>- Committer
GitHub <noreply@github.com>- Parent 1
d671d98- Parent 2
5da9888
3 files changed+67 added−70 removed
modified:bash_completions.go+2 added-4 removed
399 unmodified lines400 if !b {401 format += "="402 }403 format += "\")\n"404- if _, err := fmt.Fprintf(w, format, name); err != nil {405- return err406- }407- return nil404+ _, err := fmt.Fprintf(w, format, name)405+ return err406 }407 408 func writeFlags(cmd *Command, w io.Writer) error {409 _, err := fmt.Fprintf(w, ` flags=()232 unmodified linesmodified:cobra.go+17 added-19 removed
35 unmodified lines36 }37 38 var initializers []func()39 40-// 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.42 var EnablePrefixMatching = false43 44-//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.46 var EnableCommandSorting = true47 48-//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.50 func AddTemplateFunc(name string, tmplFunc interface{}) {51 templateFuncs[name] = tmplFunc52 }53 54-//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.56 func AddTemplateFuncs(tmplFuncs template.FuncMap) {57 for k, v := range tmplFuncs {58 templateFuncs[k] = v59 }60 }61 62-//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().63 func OnInitialize(y ...func()) {64- for _, x := range y {65- initializers = append(initializers, x)64+ initializers = append(initializers, y...)66- }65 }66 69-//Gt takes two types and checks whether the first type is greater than the second. In case of types Arrays, Chans,70-//Maps and Slices, Gt will compare their lengths. Ints are compared directly while strings are first parsed as71-//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.70 func Gt(a interface{}, b interface{}) bool {71 var left, right int6472 av := reflect.ValueOf(a)73 19 unmodified lines93 94 return left > right95 }96 99-//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.98 func Eq(a interface{}, b interface{}) bool {99 av := reflect.ValueOf(a)100 bv := reflect.ValueOf(b)101 11 unmodified lines113 func trimRightSpace(s string) string {114 return strings.TrimRightFunc(s, unicode.IsSpace)115 }116 119-// 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.118 func appendIfNotPresent(s, stringToAppend string) string {119 if strings.Contains(s, stringToAppend) {120 return s121 }122 return s + " " + stringToAppend123 }124 127-//rpad adds padding to the right of a string125+// rpad adds padding to the right of a string.126 func rpad(s string, padding int) string {127 template := fmt.Sprintf("%%-%ds", padding)128 return fmt.Sprintf(template, s)129 }5 unmodified lines135 template.Must(t.Parse(text))136 return t.Execute(w, data)137 }138 141-// ld compares two strings and returns the levenshtein distance between them139+// ld compares two strings and returns the levenshtein distance between them.140 func ld(s, t string, ignoreCase bool) int {141 if ignoreCase {142 s = strings.ToLower(s)143 t = strings.ToLower(t)30 unmodified linesmodified:command.go+48 added-47 removed
138 unmodified lines139 func (c *Command) SetOutput(output io.Writer) {140 c.output = &output141 }142 143-// Usage can be defined by application143+// Usage can be defined by application.144 func (c *Command) SetUsageFunc(f func(*Command) error) {145 c.usageFunc = f146 }147 148-// Can be defined by Application148+// Can be defined by Application.149 func (c *Command) SetUsageTemplate(s string) {150 c.usageTemplate = s151 }152 153-// Can be defined by Application153+// Can be defined by Application.154 func (c *Command) SetHelpFunc(f func(*Command, []string)) {155 c.helpFunc = f156 }157 158 func (c *Command) SetHelpCommand(cmd *Command) {159 c.helpCommand = cmd160 }161 162-// Can be defined by Application162+// Can be defined by Application.163 func (c *Command) SetHelpTemplate(s string) {164 c.helpTemplate = s165 }166 27 unmodified lines194 return def195 }196 197 // 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.199 func (c *Command) UsageFunc() (f func(*Command) error) {200 if c.usageFunc != nil {201 return c.usageFunc202 }10 unmodified lines213 return err214 }215 }216 217-// 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.220 func (c *Command) Usage() error {221 return c.UsageFunc()(c)222 }223 224 // 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.226 func (c *Command) HelpFunc() func(*Command, []string) {227 cmd := c228 for cmd != nil {229 if cmd.helpFunc != nil {9 unmodified lines239 }240 }241 }242 243-// 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.246 func (c *Command) Help() error {247 c.HelpFunc()(c, []string{})248 return nil249 }82 unmodified lines332 333 {{end}}{{if or .Runnable .HasSubCommands}}{{.UsageString}}{{end}}`334 }335 336-// Really only used when casting a command to a commander336+// Really only used when casting a command to a commander.337 func (c *Command) resetChildrensParents() {338 for _, x := range c.commands {339 x.parent = c340 }403 unmodified lines744 }745 c.AddCommand(c.helpCommand)746 }747 748-// Used for testing748+// Used for testing.749 func (c *Command) ResetCommands() {750 c.commands = nil751 c.helpCommand = nil752 }753 754-// Sorts commands by their names754+// Sorts commands by their names.755 type commandSorterByName []*Command756 757 func (c commandSorterByName) Len() int { return len(c) }758 func (c commandSorterByName) Swap(i, j int) { c[i], c[j] = c[j], c[i] }71 unmodified lines830 }831 }832 }833 834-// 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.835 func (c *Command) Print(i ...interface{}) {836 fmt.Fprint(c.OutOrStderr(), i...)837 }838 839-// 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.840 func (c *Command) Println(i ...interface{}) {841 str := fmt.Sprintln(i...)842 c.Print(str)843 }844 845-// 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.846 func (c *Command) Printf(format string, i ...interface{}) {847 str := fmt.Sprintf(format, i...)848 c.Print(str)849 }8 unmodified lines858 }859 return str860 }861 862-//The full usage for a given command (including parents)862+// UseLine puts out the full usage for a given command (including parents).863 func (c *Command) UseLine() string {864 str := ""865 if c.HasParent() {866 str = c.parent.CommandPath() + " "1 unmodified line868 return str + c.Use869 }870 871 // For use in determining which flags have been assigned to which commands872-// and which persist872+// and which persist.873 func (c *Command) DebugFlags() {874 c.Println("DebugFlags called on", c.Name())875 var debugflags func(*Command)876 66 unmodified lines943 func (c *Command) HasExample() bool {944 return len(c.Example) > 0945 }946 947-// Runnable determines if the command is itself runnable947+// Runnable determines if the command is itself runnable.948 func (c *Command) Runnable() bool {949 return c.Run != nil || c.RunE != nil950 }951 952-// HasSubCommands determines if the command has children commands952+// HasSubCommands determines if the command has children commands.953 func (c *Command) HasSubCommands() bool {954 return len(c.commands) > 0955 }956 957 // 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).959 func (c *Command) IsAvailableCommand() bool {960 if len(c.Deprecated) != 0 || c.Hidden {961 return false962 }10 unmodified lines973 }974 975 // 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.978 func (c *Command) IsHelpCommand() bool {979 980 // 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 lines994 }995 996 // 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'.999 func (c *Command) HasHelpSubCommands() bool {1000 1001 // return true on the first found available 'help' sub command1002 for _, sub := range c.commands {6 unmodified lines1009 return false1010 }1011 1012 // 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'.1014 func (c *Command) HasAvailableSubCommands() bool {1015 1016 // return true on the first found available (non deprecated/help/hidden)1017 // sub command7 unmodified lines1025 // sub commands1026 return false1027 }1028 1029-// Determine if the command is a child command1029+// HasParent determines if the command is a child command.1030 func (c *Command) HasParent() bool {1031 return c.parent != nil1032 }1033 1034-// GlobalNormalizationFunc returns the global normalization function or nil if doesn't exists1034+// GlobalNormalizationFunc returns the global normalization function or nil if doesn't exists.1035 func (c *Command) GlobalNormalizationFunc() func(f *flag.FlagSet, name string) flag.NormalizedName {1036 return c.globNormFunc1037 }1038 1039-// 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).1041 func (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 lines1048 }1049 return c.flags1050 }1051 1051-// 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.1053 func (c *Command) LocalNonPersistentFlags() *flag.FlagSet {1054 persistentFlags := c.PersistentFlags()1055 1056 out := flag.NewFlagSet(c.Name(), flag.ContinueOnError)4 unmodified lines1061 })1062 return out1063 }1064 1064-// Get the local FlagSet specifically set in the current command1065+// LocalFlags returns the local FlagSet specifically set in the current command.1066 func (c *Command) LocalFlags() *flag.FlagSet {1067 c.mergePersistentFlags()1068 1069 local := flag.NewFlagSet(c.Name(), flag.ContinueOnError)9 unmodified lines1079 }1080 return local1081 }1082 1082-// All Flags which were inherited from parents commands1083+// InheritedFlags returns all flags which were inherited from parents commands.1084 func (c *Command) InheritedFlags() *flag.FlagSet {1085 c.mergePersistentFlags()1086 1087 inherited := flag.NewFlagSet(c.Name(), flag.ContinueOnError)20 unmodified lines1108 1109 return inherited1110 }1111 1111-// All Flags which were not inherited from parent commands1112+// NonInheritedFlags returns all flags which were not inherited from parent commands.1113 func (c *Command) NonInheritedFlags() *flag.FlagSet {1114 return c.LocalFlags()1115 }1116 1116-// Get the Persistent FlagSet specifically set in the current command1117+// PersistentFlags returns the persistent FlagSet specifically set in the current command.1118 func (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 lines1125 }1126 return c.pflags1127 }1128 1128-// For use in testing1129+// ResetFlags is used in testing.1130 func (c *Command) ResetFlags() {1131 c.flagErrorBuf = new(bytes.Buffer)1132 c.flagErrorBuf.Reset()1133 c.flags = flag.NewFlagSet(c.Name(), flag.ContinueOnError)1 unmodified line1135 c.pflags = flag.NewFlagSet(c.Name(), flag.ContinueOnError)1136 c.pflags.SetOutput(c.flagErrorBuf)1137 }1138 1138-// 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).1140 func (c *Command) HasFlags() bool {1141 return c.Flags().HasFlags()1142 }1143 1143-// Does the command contain persistent flags1144+// Does the command contain persistent flags.1145 func (c *Command) HasPersistentFlags() bool {1146 return c.PersistentFlags().HasFlags()1147 }1148 1148-// Does the command has flags specifically declared locally1149+// Does the command has flags specifically declared locally.1150 func (c *Command) HasLocalFlags() bool {1151 return c.LocalFlags().HasFlags()1152 }1153 1153-// Does the command have flags inherited from its parent command1154+// Does the command have flags inherited from its parent command.1155 func (c *Command) HasInheritedFlags() bool {1156 return c.InheritedFlags().HasFlags()1157 }1158 1159 // 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.1161 func (c *Command) HasAvailableFlags() bool {1162 return c.Flags().HasAvailableFlags()1163 }1164 1164-// Does the command contain persistent flags which are not hidden or deprecated1165+// Does the command contain persistent flags which are not hidden or deprecated.1166 func (c *Command) HasAvailablePersistentFlags() bool {1167 return c.PersistentFlags().HasAvailableFlags()1168 }1169 1170 // Does the command has flags specifically declared locally which are not hidden1170-// or deprecated1171+// or deprecated.1172 func (c *Command) HasAvailableLocalFlags() bool {1173 return c.LocalFlags().HasAvailableFlags()1174 }1175 1176 // Does the command have flags inherited from its parent command which are1176-// not hidden or deprecated1177+// not hidden or deprecated.1178 func (c *Command) HasAvailableInheritedFlags() bool {1179 return c.InheritedFlags().HasAvailableFlags()1180 }1181 1181-// Flag climbs up the command tree looking for matching flag1182+// Flag climbs up the command tree looking for matching flag.1183 func (c *Command) Flag(name string) (flag *flag.Flag) {1184 flag = c.Flags().Lookup(name)1185 1186 if flag == nil {2 unmodified lines1189 1190 return1191 }1192 1192-// recursively find matching persistent flag1193+// Recursively find matching persistent flag.1194 func (c *Command) persistentFlag(name string) (flag *flag.Flag) {1195 if c.HasPersistentFlags() {1196 flag = c.PersistentFlags().Lookup(name)1197 }3 unmodified lines1201 }1202 return1203 }1204 1204-// ParseFlags parses persistent flag tree & local flags1205+// ParseFlags parses persistent flag tree and local flags.1206 func (c *Command) ParseFlags(args []string) (err error) {1207 if c.DisableFlagParsing {1208 return nil1209 }1 unmodified line1211 err = c.Flags().Parse(args)1212 return1213 }1214 1214-// Parent returns a commands parent command1215+// Parent returns a commands parent command.1216 func (c *Command) Parent() *Command {1217 return c.parent1218 }1219 38 unmodified lines