add status help

This commit is contained in:
Roy Olav Purser 2021-06-18 14:56:49 +02:00
parent cf1331e3cd
commit 4d89362709
Signed by: roypur
GPG Key ID: E14D26A036F21656

View File

@ -15,6 +15,8 @@ type cEntry struct {
dtype DataType dtype DataType
unset bool unset bool
empty bool empty bool
defval string // The default value
hasdef bool // Default value is defined
} }
type Config struct { type Config struct {
parsed bool parsed bool
@ -73,6 +75,7 @@ func (c *Config) DefineDefault(key string, val string, dtype DataType) {
} }
entry.dtype = dtype entry.dtype = dtype
entry.empty = false entry.empty = false
entry.defval = val
c.env[upper] = entry c.env[upper] = entry
} else { } else {
var entry cEntry var entry cEntry
@ -80,6 +83,7 @@ func (c *Config) DefineDefault(key string, val string, dtype DataType) {
entry.unset = true entry.unset = true
entry.empty = false entry.empty = false
entry.value = val entry.value = val
entry.defval = val
c.env[upper] = entry 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) max := make([]int, 2, 2)
for k, v := range c.env { for k, v := range c.env {
if v.dtype != TypeNone { if v.dtype != TypeNone {
@ -129,12 +133,42 @@ func (c *Config) Help() {
} }
} }
} }
fmt.Println()
for k, v := range c.env { for k, v := range c.env {
if v.dtype != TypeNone { if v.dtype != TypeNone {
format := fmt.Sprintf("Variable %%-%ds| Type %%-%ds| Default %%s\n", max[0]+3, max[1]+3) if v.hasdef {
fmt.Printf(format, k, v.dtype, v.value) 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. // Status prints out failures that occured while parsing the environment to os.Stderr.