@@ -237,21 +237,87 @@ func Set(name, value string) error {
237
237
return CommandLine .Set (name , value )
238
238
}
239
239
240
- // PrintDefaults prints, to standard error unless configured
241
- // otherwise, the default values of all defined flags in the set.
240
+ // isZeroValue guesses whether the string represents the zero
241
+ // value for a flag. It is not accurate but in practice works OK.
242
+ func isZeroValue (value string ) bool {
243
+ switch value {
244
+ case "false" :
245
+ return true
246
+ case "" :
247
+ return true
248
+ case "0" :
249
+ return true
250
+ }
251
+ return false
252
+ }
253
+
254
+ // UnquoteUsage extracts a back-quoted name from the usage
255
+ // string for a flag and returns it and the un-quoted usage.
256
+ // Given "a `name` to show" it returns ("name", "a name to show").
257
+ // If there are no back quotes, the name is an educated guess of the
258
+ // type of the flag's value, or the empty string if the flag is boolean.
259
+ func UnquoteUsage (flag * Flag ) (name string , usage string ) {
260
+ // Look for a back-quoted name, but avoid the strings package.
261
+ usage = flag .Usage
262
+ for i := 0 ; i < len (usage ); i ++ {
263
+ if usage [i ] == '`' {
264
+ for j := i + 1 ; j < len (usage ); j ++ {
265
+ if usage [j ] == '`' {
266
+ name = usage [i + 1 : j ]
267
+ usage = usage [:i ] + name + usage [j + 1 :]
268
+ return name , usage
269
+ }
270
+ }
271
+ break // Only one back quote; use type name.
272
+ }
273
+ }
274
+ // No explicit name, so use type if we can find one.
275
+ name = "value"
276
+ switch flag .Value .(type ) {
277
+ case boolFlag :
278
+ name = ""
279
+ case * durationValue :
280
+ name = "duration"
281
+ case * float64Value :
282
+ name = "float"
283
+ case * intValue , * int64Value :
284
+ name = "int"
285
+ case * stringValue :
286
+ name = "string"
287
+ case * uintValue , * uint64Value :
288
+ name = "uint"
289
+ }
290
+ return
291
+ }
292
+
293
+ // PrintDefaults prints to standard error the default values of all
294
+ // defined command-line flags in the set. See the documentation for
295
+ // the global function PrintDefaults for more information.
242
296
func (f * FlagSet ) PrintDefaults () {
243
297
f .VisitAll (func (flag * Flag ) {
244
- format := "--%s=%s: %s\n "
245
- if _ , ok := flag .Value .(* stringValue ); ok {
246
- // put quotes on the value
247
- format = "--%s=%q: %s\n "
248
- }
298
+ s := ""
249
299
if len (flag .Shorthand ) > 0 {
250
- format = " -%s, " + format
300
+ s = fmt . Sprintf ( " -%s, --%s" , flag . Shorthand , flag . Name )
251
301
} else {
252
- format = " %s " + format
302
+ s = fmt . Sprintf ( " --%s" , flag . Name )
253
303
}
254
- fmt .Fprintf (f .out (), format , flag .Shorthand , flag .Name , flag .DefValue , flag .Usage )
304
+
305
+ name , usage := UnquoteUsage (flag )
306
+ if len (name ) > 0 {
307
+ s += " " + name
308
+ }
309
+
310
+ s += "\n \t "
311
+ s += usage
312
+ if ! isZeroValue (flag .DefValue ) {
313
+ if _ , ok := flag .Value .(* stringValue ); ok {
314
+ // put quotes on the value
315
+ s += fmt .Sprintf (" (default %q)" , flag .DefValue )
316
+ } else {
317
+ s += fmt .Sprintf (" (default %v)" , flag .DefValue )
318
+ }
319
+ }
320
+ fmt .Fprint (f .out (), s , "\n " )
255
321
})
256
322
}
257
323
@@ -262,7 +328,11 @@ func PrintDefaults() {
262
328
263
329
// defaultUsage is the default function to print a usage message.
264
330
func defaultUsage (f * FlagSet ) {
265
- fmt .Fprintf (f .out (), "Usage of %s:\n " , f .name )
331
+ if f .name == "" {
332
+ fmt .Fprintf (f .out (), "Usage:\n " )
333
+ } else {
334
+ fmt .Fprintf (f .out (), "Usage of %s:\n " , f .name )
335
+ }
266
336
f .PrintDefaults ()
267
337
}
268
338
0 commit comments