@@ -351,24 +351,26 @@ stack.**
351
351
## Automatic Flags
352
352
353
353
When you are using ` err2` or ` assert` packages, i.e., just importing them, you
354
- have an option to automatically add support for flags .
354
+ have an option to automatically add support for Go's standard ` flag ` package .
355
355
356
- Let's say you have build CLI tool and it returns an error. You can run it again
357
- with:
356
+ Let's say you have build CLI (` your-app` ) tool with the support for Go's flag
357
+ package, and the app returns an error. Let's assume you're a developer. You can
358
+ run it again with:
358
359
359
360
` ` `
360
361
your-app -err2-trace stderr
361
362
` ` `
362
363
363
364
Now you get full error trace addition to the error message. Naturally, this
364
- also works asserts, which you can configure also with the flags:
365
+ also works with assertions. You can configure their output with the flag
366
+ ` asserter` :
365
367
366
368
` ` `
367
- your-app -assert Debug
369
+ your-app -asserter Debug
368
370
` ` `
369
371
370
372
That adds more information to the assertion statement, which in default is in
371
- production (` Prod` ) mode, i.e., K&D error message.
373
+ production (` Prod` ) mode, i.e., outputs K&D error message.
372
374
373
375
All you need to do is to add ` flag.Parse ` to your ` main` function.
374
376
@@ -381,7 +383,7 @@ support packages like `err2` and `glog` and their flags.
381
383
382
384
` ` ` go
383
385
import (
384
- goflag " flag"
386
+ goflag " flag"
385
387
...
386
388
)
387
389
` ` `
@@ -391,44 +393,39 @@ support packages like `err2` and `glog` and their flags.
391
393
` ` ` go
392
394
func init () {
393
395
...
394
- // NOTE! Very important. Adds support for std flag pkg users: glog, err2
395
- pflag.CommandLine .AddGoFlagSet (goflag.CommandLine )
396
+ // NOTE! Very important. Adds support for std flag pkg users: glog, err2
397
+ pflag.CommandLine .AddGoFlagSet (goflag.CommandLine )
396
398
}
397
399
` ` `
398
400
399
401
1. And finally modify your ` PersistentPreRunE` in ` cmd/root.go ` to something
400
402
like:
401
403
402
404
` ` ` go
403
- PersistentPreRunE: func (cmd *cobra.Command , args []string ) (err error ) {
404
- defer err2.Handle (&err)
405
+ PersistentPreRunE: func (cmd *cobra.Command , args []string ) (err error ) {
406
+ defer err2.Handle (&err)
405
407
406
- // NOTE! Very important. Adds support for std flag pkg users: glog, err2
407
- goflag.Parse ()
408
+ // NOTE! Very important. Adds support for std flag pkg users: glog, err2
409
+ goflag.Parse ()
408
410
409
- try.To (goflag.Set (" logtostderr" , " true" ))
410
- handleViperFlags (cmd) // local helper with envs
411
- glog.CopyStandardLogTo (" ERROR" ) // for err2
412
- return nil
413
- },
411
+ try.To (goflag.Set (" logtostderr" , " true" ))
412
+ handleViperFlags (cmd) // local helper with envs
413
+ glog.CopyStandardLogTo (" ERROR" ) // for err2
414
+ return nil
415
+ },
414
416
` ` `
415
417
416
418
As a result you can have bunch of usable flags added to your CLI:
417
419
418
420
` ` `
419
421
Flags:
420
- --alsologtostderr log to standard error as well as files
421
422
--asserter asserter asserter: Plain, Prod, Dev, Debug (default Prod)
422
- --config string configuration file, FCLI_CONFIG
423
- -n, --dry-run perform a trial run with no changes made, FCLI_DRY_RUN
424
423
--err2-log stream stream for logging: nil -> log pkg (default nil )
425
424
--err2-panic -trace stream stream for panic tracing (default stderr)
426
425
--err2-trace stream stream for error tracing: stderr, stdout (default nil )
427
426
...
428
427
` ` `
429
428
430
- And many others form ` glog` in this specific example case.
431
-
432
429
## Code Snippets
433
430
434
431
Most of the repetitive code blocks are offered as code snippets. They are in
0 commit comments