Add Cmd.Root and Cmd.PathCmds

This commit is contained in:
rwxrob 2022-04-11 14:08:31 -04:00
parent 04ef25b40f
commit 1492aaf15a
No known key found for this signature in database
GPG Key ID: 2B9111F33082AE77
2 changed files with 37 additions and 1 deletions

View File

@ -277,6 +277,17 @@ func (x *Cmd) Run() {
Exit()
}
// Root returns the root Cmd from the current Path. This must always be
// calculated every time since any Cmd can change positions and
// pedigrees at any time at run time. Returns self if no PathCmds found.
func (x *Cmd) Root() *Cmd {
cmds := x.PathCmds()
if len(cmds) > 0 {
return cmds[0].Caller
}
return x.Caller
}
// UsageError returns an error with a single-line usage string.
func (x *Cmd) UsageError() error {
return fmt.Errorf("usage: %v %v", x.Name, UsageFunc(x))
@ -429,6 +440,20 @@ func (x *Cmd) Seek(args []string) (*Cmd, []string) {
return cur, args[n:]
}
// PathCmds returns the path of commands used to arrive at this
// command. The path is determined by walking backward from current
// Caller up rather than depending on anything from the command line
// used to invoke the composing binary. Also see Path, PathNames.
func (x *Cmd) PathCmds() []*Cmd {
path := qstack.New[*Cmd]()
path.Unshift(x)
for p := x.Caller; p != nil; p = p.Caller {
path.Unshift(p)
}
path.Shift()
return path.Items()
}
// PathNames returns the path of command names used to arrive at this
// command. The path is determined by walking backward from current
// Caller up rather than depending on anything from the command line

View File

@ -5,6 +5,7 @@ import (
"path/filepath"
"text/template"
"github.com/rwxrob/term"
"github.com/rwxrob/to"
)
@ -13,7 +14,17 @@ import (
// allowed.
var markFuncMap = template.FuncMap{
"indent": indent,
// semantic
"exe": func(a string) string { return term.Under + a + term.Reset },
"pkg": func(a string) string { return term.Bold + a + term.Reset },
"cmd": func(a string) string { return term.Bold + a + term.Reset },
// stylistic
"indent": indent,
"pre": func(a string) string { return term.Under + a + term.Reset },
// host system information
"exepath": exepath,
"exename": exename,
"execachedir": execachedir,