@@ -13,8 +13,8 @@ import (
1313// var cfg struct {
1414// Port int `env:"PORT"`
1515// }
16- // env.Load(&cfg, nil) // 1. sets cfg.Port to 8080
17- // env.Usage(&cfg, os.Stdout) // 2. prints cfg.Port's default == 8080 (instead of 0)
16+ // env.Load(&cfg, nil) // 1. sets cfg.Port to 8080
17+ // env.Usage(&cfg, os.Stdout, nil ) // 2. prints cfg.Port's default == 8080 (instead of 0)
1818//
1919// It also speeds up [Usage], since there is no need to parse the struct again.
2020var cache = make (map [reflect.Type ][]Var )
@@ -34,8 +34,8 @@ type Var struct {
3434
3535// Usage writes a usage message documenting all defined environment variables to the given [io.Writer].
3636// An optional usage string can be added for each environment variable via the `usage:"STRING"` struct tag.
37- // The format of the message can be customized by implementing the Usage([]env.Var, io.Writer) method on the cfg's type.
38- func Usage (cfg any , w io.Writer ) {
37+ // The format of the message can be customized by implementing the Usage([]env.Var, io.Writer, *env.Options ) method on the cfg's type.
38+ func Usage (cfg any , w io.Writer , opts * Options ) {
3939 pv := reflect .ValueOf (cfg )
4040 if ! structPtr (pv ) {
4141 panic ("env: cfg must be a non-nil struct pointer" )
@@ -47,14 +47,18 @@ func Usage(cfg any, w io.Writer) {
4747 vars = parseVars (v )
4848 }
4949
50- if u , ok := cfg .(interface { Usage ([]Var , io.Writer ) }); ok {
51- u .Usage (vars , w )
50+ if u , ok := cfg .(interface {
51+ Usage ([]Var , io.Writer , * Options )
52+ }); ok {
53+ u .Usage (vars , w , opts )
5254 } else {
53- defaultUsage (vars , w )
55+ defaultUsage (vars , w , opts )
5456 }
5557}
5658
57- func defaultUsage (vars []Var , w io.Writer ) {
59+ func defaultUsage (vars []Var , w io.Writer , _ * Options ) {
60+ // TODO: use opts.SliceSep to parse slice values.
61+
5862 tw := tabwriter .NewWriter (w , 0 , 0 , 2 , ' ' , 0 )
5963 defer tw .Flush ()
6064
0 commit comments