From 4d2332582355c0b4c70d7817e569139d502d97a1 Mon Sep 17 00:00:00 2001 From: Brian Picciano Date: Tue, 17 May 2022 11:56:17 -0600 Subject: Implement cfg.BoolVar method --- srv/src/cfg/cfg.go | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) (limited to 'srv/src/cfg/cfg.go') 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 -- cgit v1.2.3