@@ -46,6 +46,8 @@ func CopyFile(src, dst string) (err error) {
46
46
- [ Assertion] ( #assertion )
47
47
- [ Assertion Package for Runtime Use] ( #assertion-package-for-runtime-use )
48
48
- [ Assertion Package for Unit Testing] ( #assertion-package-for-unit-testing )
49
+ - [ Automatic Flags] ( #automatic-flags )
50
+ - [ Support for Cobra Flags] ( support-for-cobra-flags )
49
51
- [ Code Snippets] ( #code-snippets )
50
52
- [ Background] ( #background )
51
53
- [ Learnings by so far] ( #learnings-by-so-far )
@@ -346,6 +348,87 @@ can be the same or different modules.
346
348
execution, we will find it and can even move thru every step in the call
347
349
stack.**
348
350
351
+ ## Automatic Flags
352
+
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.
355
+
356
+ Let's say you have build CLI tool and it returns an error. You can run it again
357
+ with:
358
+
359
+ ` ` `
360
+ your-app -err2-trace stderr
361
+ ` ` `
362
+
363
+ 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
+
366
+ ` ` `
367
+ your-app -assert Debug
368
+ ` ` `
369
+
370
+ That adds more information to the assertion statement, which in default is in
371
+ production (` Prod` ) mode, i.e., K&D error message.
372
+
373
+ All you need to do is to add ` flag.Parse ` to your ` main` function.
374
+
375
+ #### Support for Cobra Flags
376
+
377
+ If you are using [cobra](https://github.com/spf13/cobra) you can still easily
378
+ support packages like ` err2` and ` glog` and their flags.
379
+
380
+ 1. Add std flag package to imports in ` cmd/root.go ` :
381
+
382
+ ` ` ` go
383
+ import (
384
+ goflag " flag"
385
+ ...
386
+ )
387
+ ` ` `
388
+
389
+ 1. Add the following to (usually) ` cmd/root.go ` 's ` init` function's end:
390
+
391
+ ` ` ` go
392
+ func init () {
393
+ ...
394
+ // NOTE! Very important. Adds support for std flag pkg users: glog, err2
395
+ pflag.CommandLine .AddGoFlagSet (goflag.CommandLine )
396
+ }
397
+ ` ` `
398
+
399
+ 1. And finally modify your ` PersistentPreRunE` in ` cmd/root.go ` to something
400
+ like:
401
+
402
+ ` ` ` go
403
+ PersistentPreRunE: func (cmd *cobra.Command , args []string ) (err error ) {
404
+ defer err2.Handle (&err)
405
+
406
+ // NOTE! Very important. Adds support for std flag pkg users: glog, err2
407
+ goflag.Parse ()
408
+
409
+ try.To (goflag.Set (" logtostderr" , " true" ))
410
+ handleViperFlags (cmd) // local helper with envs
411
+ glog.CopyStandardLogTo (" ERROR" ) // for err2
412
+ return nil
413
+ },
414
+ ` ` `
415
+
416
+ As a result you can have bunch of usable flags added to your CLI:
417
+
418
+ ` ` `
419
+ Flags:
420
+ --alsologtostderr log to standard error as well as files
421
+ --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
+ --err2-log stream stream for logging: nil -> log pkg (default nil )
425
+ --err2-panic -trace stream stream for panic tracing (default stderr)
426
+ --err2-trace stream stream for error tracing: stderr, stdout (default nil )
427
+ ...
428
+ ` ` `
429
+
430
+ And many others form ` glog` in this specific example case.
431
+
349
432
## Code Snippets
350
433
351
434
Most of the repetitive code blocks are offered as code snippets. They are in
@@ -430,45 +513,12 @@ Please see the full version history from [CHANGELOG](./CHANGELOG.md).
430
513
431
514
### Latest Release
432
515
433
- ##### 0.9.41
434
- - Issue #18: **bug fixed**: noerr-handler had to be the last one of the err2
435
- handlers
436
-
437
- ##### 0.9.40
438
- - Significant performance boost for: ` defer err2.Handle /Catch ()`
439
- - **3x faster happy path than the previous version, which is now equal to
440
- simplest ` defer ` function in the ` err` -returning function** . (Please see
441
- the ` defer ` benchmarks in the ` err2_test.go ` and run ` make bench_reca` )
442
- - the solution caused a change to API, where the core reason is Go's
443
- optimization "bug". (We don't have confirmation yet.)
444
- - Changed API for deferred error handling: ` defer err2.Handle /Catch ()`
445
- - *Obsolete*:
446
- ` ` ` go
447
- defer err2.Handle (&err, func () {}) // <- relaying closure to access err val
448
- ` ` `
449
- - Current version:
450
- ` ` ` go
451
- defer err2.Handle (&err, func (err error ) error { return err }) // not a closure
452
- ` ` `
453
- Because handler function is not relaying closures any more, it opens a new
454
- opportunity to use and build general helper functions: ` err2.Noop ` , etc.
455
- - Use auto-migration scripts especially for large code-bases. More information
456
- can be found in the ` scripts/` directory's [readme file](./scripts/README.md).
457
- - Added a new (*experimental*) API:
458
- ` ` ` go
459
- defer err2.Handle (&err, func (noerr bool ) {
460
- assert.That (noerr) // noerr is always true!!
461
- doSomething ()
462
- })
463
- ` ` `
464
- This is experimental because we aren't sure if this is something we want to
465
- have in the ` err2` package.
466
- - Bug fixes: ` ResultX.Logf ()` now works as it should
467
- - More documentation
516
+ ##### 0.9.5
517
+ - ` flag` package integration:
468
518
469
519
### Upcoming releases
470
520
471
- ##### 0.9.5
472
- - Idea: Go's standard lib's flag pkg integration (similar to ` glog ` )
473
- - Continue removing unused parts from ` assert ` pkg
474
- - More documentation, repairing for some sort of marketing
521
+ ##### 0.9.6
522
+ - Idea: TODO
523
+ - Continue removing unused parts
524
+ - More documentation
0 commit comments