diff options
author | Brian Picciano <mediocregopher@gmail.com> | 2022-05-17 11:56:17 -0600 |
---|---|---|
committer | Brian Picciano <mediocregopher@gmail.com> | 2022-05-17 11:56:17 -0600 |
commit | 4d2332582355c0b4c70d7817e569139d502d97a1 (patch) | |
tree | 07d8778811f625ec46b6b0c79af252f393ee4d52 /srv/src/cfg/cfg.go | |
parent | 788aba3d0d0bc98c0164f87cf3a4941cf4587a54 (diff) |
Implement cfg.BoolVar method
Diffstat (limited to 'srv/src/cfg/cfg.go')
-rw-r--r-- | srv/src/cfg/cfg.go | 21 |
1 files changed, 21 insertions, 0 deletions
diff --git a/srv/src/cfg/cfg.go b/srv/src/cfg/cfg.go index 32fc3e7..d87c45b 100644 --- a/srv/src/cfg/cfg.go +++ b/srv/src/cfg/cfg.go @@ -211,6 +211,27 @@ func (c *Cfg) Int(name string, value int, usage string) *int { return p } +// BoolVar is equivalent to flag.FlagSet's BoolVar method, but will additionally +// set up an environment variable for the parameter. +func (c *Cfg) BoolVar(p *bool, name string, value bool, usage string) { + + envName := c.envifyName(name) + + c.flagSet.BoolVar(p, name, value, envifyUsage(envName, usage)) + + if valStr := c.params.Env[envName]; valStr != "" { + *p = valStr != "" && valStr != "0" && valStr != "false" + } +} + +// Bool is equivalent to flag.FlagSet's Bool method, but will additionally set +// up an environment variable for the parameter. +func (c *Cfg) Bool(name string, value bool, usage string) *bool { + p := new(bool) + c.BoolVar(p, name, value, usage) + return p +} + // SubCmd should be called _after_ Init. Init will have consumed all arguments // up until the first non-flag argument. This non-flag argument is a // sub-command, and is returned by this method. This method also resets Cfg's |