@@ -141,6 +141,8 @@ func main() {
141
141
142
142
flVersion := pflag .Bool ("version" , false , "print the version and exit" )
143
143
flHelp := pflag .BoolP ("help" , "h" , false , "print help text and exit" )
144
+ pflag .BoolVarP (flHelp , "__?" , "?" , false , "print help text and exit" ) // support -? as an alias to -h
145
+ pflag .CommandLine .MarkHidden ("__?" )
144
146
flManual := pflag .Bool ("man" , false , "print the full manual and exit" )
145
147
146
148
flVerbose := pflag .IntP ("verbose" , "v" ,
@@ -306,6 +308,23 @@ func main() {
306
308
"DEPRECATED: use --period instead" )
307
309
mustMarkDeprecated ("wait" , "use --period instead" )
308
310
311
+ // For whatever reason pflag hardcodes stderr for the "usage" line when
312
+ // using the default FlagSet. We tweak the output a bit anyway.
313
+ usage := func (out io.Writer , msg string ) {
314
+ // When pflag parsing hits an error, it prints a message before and
315
+ // after the usage, which makes for nice reading.
316
+ if msg != "" {
317
+ fmt .Fprintln (out , msg )
318
+ }
319
+ fmt .Fprintln (out , "Usage:" )
320
+ pflag .CommandLine .SetOutput (out )
321
+ pflag .PrintDefaults ()
322
+ if msg != "" {
323
+ fmt .Fprintln (out , msg )
324
+ }
325
+ }
326
+ pflag .Usage = func () { usage (os .Stderr , "" ) }
327
+
309
328
//
310
329
// Parse and verify flags. Errors here are fatal.
311
330
//
@@ -318,8 +337,7 @@ func main() {
318
337
os .Exit (0 )
319
338
}
320
339
if * flHelp {
321
- pflag .CommandLine .SetOutput (os .Stdout )
322
- pflag .PrintDefaults ()
340
+ usage (os .Stdout , "" )
323
341
os .Exit (0 )
324
342
}
325
343
if * flManual {
@@ -329,7 +347,7 @@ func main() {
329
347
330
348
// Make sure we have a root dir in which to work.
331
349
if * flRoot == "" {
332
- fmt . Fprintf (os .Stderr , "ERROR : --root must be specified\n " )
350
+ usage (os .Stderr , "required flag : --root must be specified" )
333
351
os .Exit (1 )
334
352
}
335
353
var absRoot absPath
@@ -1029,6 +1047,8 @@ func handleConfigError(log *logging.Logger, printUsage bool, format string, a ..
1029
1047
fmt .Fprintln (os .Stderr , s )
1030
1048
if printUsage {
1031
1049
pflag .Usage ()
1050
+ // pflag prints flag errors both before and after usage
1051
+ fmt .Fprintln (os .Stderr , s )
1032
1052
}
1033
1053
log .ExportError (s )
1034
1054
os .Exit (1 )
0 commit comments