Skip to content

Commit 78d7452

Browse files
committed
flags section formations and language
1 parent b994304 commit 78d7452

File tree

1 file changed

+20
-23
lines changed

1 file changed

+20
-23
lines changed

README.md

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -351,24 +351,26 @@ stack.**
351351
## Automatic Flags
352352
353353
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.
355355
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:
358359
359360
```
360361
your-app -err2-trace stderr
361362
```
362363
363364
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`:
365367
366368
```
367-
your-app -assert Debug
369+
your-app -asserter Debug
368370
```
369371
370372
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.
372374
373375
All you need to do is to add `flag.Parse` to your `main` function.
374376
@@ -381,7 +383,7 @@ support packages like `err2` and `glog` and their flags.
381383
382384
```go
383385
import (
384-
goflag "flag"
386+
goflag "flag"
385387
...
386388
)
387389
```
@@ -391,44 +393,39 @@ support packages like `err2` and `glog` and their flags.
391393
```go
392394
func init() {
393395
...
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)
396398
}
397399
```
398400
399401
1. And finally modify your `PersistentPreRunE` in `cmd/root.go` to something
400402
like:
401403
402404
```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)
405407

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()
408410

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+
},
414416
```
415417
416418
As a result you can have bunch of usable flags added to your CLI:
417419
418420
```
419421
Flags:
420-
--alsologtostderr log to standard error as well as files
421422
--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
424423
--err2-log stream stream for logging: nil -> log pkg (default nil)
425424
--err2-panic-trace stream stream for panic tracing (default stderr)
426425
--err2-trace stream stream for error tracing: stderr, stdout (default nil)
427426
...
428427
```
429428
430-
And many others form `glog` in this specific example case.
431-
432429
## Code Snippets
433430
434431
Most of the repetitive code blocks are offered as code snippets. They are in

0 commit comments

Comments
 (0)