diff --git a/envconf.go b/envconf.go index a6d611e..ce370a5 100644 --- a/envconf.go +++ b/envconf.go @@ -15,6 +15,8 @@ type cEntry struct { dtype DataType unset bool empty bool + defval string // The default value + hasdef bool // Default value is defined } type Config struct { parsed bool @@ -73,6 +75,7 @@ func (c *Config) DefineDefault(key string, val string, dtype DataType) { } entry.dtype = dtype entry.empty = false + entry.defval = val c.env[upper] = entry } else { var entry cEntry @@ -80,6 +83,7 @@ func (c *Config) DefineDefault(key string, val string, dtype DataType) { entry.unset = true entry.empty = false entry.value = val + entry.defval = val c.env[upper] = entry } } @@ -117,7 +121,7 @@ func (c *Config) Parse() { } } -func (c *Config) Help() { +func (c *Config) help() { max := make([]int, 2, 2) for k, v := range c.env { if v.dtype != TypeNone { @@ -129,12 +133,42 @@ func (c *Config) Help() { } } } + fmt.Println() for k, v := range c.env { if v.dtype != TypeNone { - format := fmt.Sprintf("Variable %%-%ds| Type %%-%ds| Default %%s\n", max[0]+3, max[1]+3) - fmt.Printf(format, k, v.dtype, v.value) + if v.hasdef { + format := fmt.Sprintf("Variable %%-%ds| Type %%-%ds| Default %%s\n", max[0]+3, max[1]+3) + fmt.Printf(format, k, v.dtype, v.defval) + } } } + fmt.Println() + for k, v := range c.env { + if v.dtype != TypeNone { + if !v.hasdef { + format := fmt.Sprintf("Variable %%-%ds| Type %%-%ds| Required", max[0]+3, max[1]+3) + fmt.Printf(format, k, v.dtype) + } + } + } + fmt.Println() +} + +func (c *Config) StatusHelp() bool { + help := false + flags := make(map[string]bool) + flags["--help"] = true + flags["-help"] = true + flags["-h"] = true + if len(os.Args) > 1 { + key := strings.ToLower(strings.TrimSpace(os.Args[1])) + _, ok := flags[key] + if ok { + c.help() + return false + } + } + return c.Status() } // Status prints out failures that occured while parsing the environment to os.Stderr.