diff --git a/z/cmd.go b/z/cmd.go index cb24aef..f95a5ff 100644 --- a/z/cmd.go +++ b/z/cmd.go @@ -582,12 +582,26 @@ func (x *Cmd) Set(key, val string) error { return Vars.Set(path+key, val) } +// Del is a shorter version of Z.Vars.Del(x.Path()+"."+key.val) for +// convenience. Logs the error Z.Vars is not defined (see UseVars). +func (x *Cmd) Del(key string) error { + if Vars == nil { + return UsesVars{x} + } + path := x.Path() + if path != "." { + path += "." + } + Vars.Del(path + key) + return nil +} + // Fill fills out the passed text/template string using the Cmd instance // as the data object source for the template. It is called by the Get* // family of field accessors but can be called directly as well. Also // see markfunc.go for list of predefined template functions. func (x *Cmd) Fill(tmpl string) string { - funcs := to.MergedMaps(markFuncMap, x.Dynamic) + funcs := to.MergedMaps(Dynamic, x.Dynamic) t, err := template.New("t").Funcs(funcs).Parse(tmpl) if err != nil { log.Println(err) diff --git a/z/cmd_test.go b/z/cmd_test.go index 7f00ed4..7bcf55c 100644 --- a/z/cmd_test.go +++ b/z/cmd_test.go @@ -257,7 +257,7 @@ func ExampleCmd_UsageCmdShortcuts() { }, } fmt.Println(x.UsageCmdShortcuts()) - // Output: + // Unordered Output: // foo - a long way to foo // bar - a long long way to bar } diff --git a/z/markfunc.go b/z/markfunc.go index 61e0d41..a8af8e7 100644 --- a/z/markfunc.go +++ b/z/markfunc.go @@ -13,7 +13,14 @@ import ( // in their Description and other places where templated BonzaiMark is // allowed. -var markFuncMap = template.FuncMap{ +// Dynamic contains the package global default template language which +// can be supplemented or overridden by Bonzai developers. Note this is in +// addition to any specific syntax added specifically to a Cmd with +// Cmd.Dynamic (which takes higher priority). Use Z.Dynamic when +// a shared template language is to be used across all Cmds within +// a single Bonzai tree or branch. This allows powerful, template-driven +// applications to work well with one another. +var Dynamic = template.FuncMap{ // semantic "exe": func(a string) string { return term.Under + a + term.Reset },