Add VarDefs default Var and move to Cmd scope

main v0.15.0
Rob Muhlestein 2 years ago
parent 494d2d684c
commit a135921e96

@ -124,11 +124,6 @@ var Vars bonzai.Vars
// than producing usually long usage lines.
var UsageFunc = InferredUsage
// VarDefsFromConf will cause the Get method to check for an existing
// Conf value matching the path as the and if found Set it to that
// default and then return it.
var VarDefsFromConf bool
// InferredUsage returns a single line of text summarizing only the
// Commands (less any Hidden commands), Params, and Aliases. If a Cmd
// is currently in an invalid state (Params without Call, no Call and no
@ -295,6 +290,10 @@ func ArgsOrIn(args []string) string {
return strings.Join(args, " ")
}
// VarVals is a map keyed to individual variable keys from Vars. See
// Cmd.ConfVars and Cmd.Get.
type VarVals map[string]string
// ArgMap is a map keyed to individual arguments that should be
// expanded by being replaced with the slice of strings. See
// Cmd.Shortcuts.

@ -49,6 +49,9 @@ type Cmd struct {
Params []string `json:"params,omitempty"`
Hidden []string `json:"hidden,omitempty"`
// default values for Var variables, also for initialization
VarDefs VarVals `json:"vardefs,omitempty"`
// standard or custom completer, usually of form compfoo.New()
Comp bonzai.Completer `json:"-"`
@ -60,14 +63,15 @@ type Cmd struct {
Input io.Reader
// faster than lots of "if" in Call
MinArgs int `json:"-"` // minimum number of args required (including parms)
MaxArgs int `json:"-"` // maximum number of args required (including parms)
NumArgs int `json:"-"` // exact number of args required (including parms)
NoArgs bool `json:"-"` // must not have any args
MinParm int `json:"-"` // minimum number of params required
MaxParm int `json:"-"` // maximum number of params required
UseConf bool `json:"-"` // requires Z.Conf be assigned
UseVars bool `json:"-"` // requires Z.Var be assigned
MinArgs int `json:"-"` // minimum number of args required (including parms)
MaxArgs int `json:"-"` // maximum number of args required (including parms)
NumArgs int `json:"-"` // exact number of args required (including parms)
NoArgs bool `json:"-"` // must not have any args
MinParm int `json:"-"` // minimum number of params required
MaxParm int `json:"-"` // maximum number of params required
UseConf bool `json:"-"` // requires Z.Conf be assigned
UseVars bool `json:"-"` // requires Z.Var be assigned
ConfVars bool `json:"-"` // vars default to conf values before VarDefs
_aliases map[string]*Cmd // see cacheAliases called from Run->Seek->Resolve
_sections map[string]string // see cacheSections called from Run
@ -594,9 +598,19 @@ func (x *Cmd) Get(key string) (string, error) {
return v, nil
}
v, _ = Conf.Query(ptr)
if v != "" && v != "null" {
x.Set(key, v)
if x.ConfVars {
v, _ = Conf.Query(ptr)
if v != "" && v != "null" {
x.Set(key, v)
return v, nil
}
}
if x.VarDefs != nil {
v, _ = x.VarDefs[key]
if v != "" {
x.Set(key, v)
}
return v, nil
}

Loading…
Cancel
Save