Documentation
¶
Overview ¶
Package bonzai provides a rooted node tree of commands and singular parameters making tab completion a breeze and complicated applications much easier to intuit without reading all the docs. Documentation is embedded with each command removing the need for separate man pages and such and can be viewed as text or a locally served web page.
Rooted Node Tree ¶
Commands and parameters are linked to create a rooted node tree of the following types of nodes:
- Leaves with a method and optional parameters
- Branches with leaves, other branches, and a optional method
- Parameters, single words that are passed to a leaf command
Index ¶
- Variables
- func ArgsFrom(line string) []string
- func AutoUpdate[T int | string](cur T, newURL, sum, exe string)
- func Exit()
- func ExitError(err ...interface{})
- func ExitOff()
- func ExitOn()
- func Files(dir string) []string
- func ReplaceSelf(url, sumurl string) error
- type Cmd
- func (x *Cmd) Add(name string, aliases ...string) *Cmd
- func (x *Cmd) Cmd(name string) *Cmd
- func (x *Cmd) CmdNames() []string
- func (x *Cmd) GetCaller() comp.Command
- func (x *Cmd) GetCommands() []string
- func (x *Cmd) GetCompleter() comp.Completer
- func (x *Cmd) GetHidden() []string
- func (x *Cmd) GetName() string
- func (x *Cmd) GetOther() map[string]string
- func (x *Cmd) GetParams() []string
- func (x *Cmd) IsHidden(name string) bool
- func (x *Cmd) Param(p string) string
- func (x *Cmd) Run()
- func (x *Cmd) Seek(args []string) (*Cmd, []string)
- func (x *Cmd) UsageError() error
- type Method
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var DoNotExit bool
DoNotExit effectively disables Exit and ExitError allowing the program to continue running, usually for test evaluation.
var Exe string
Exe holds the full path to the current running process executable which is determined at init() time by calling os.Executable and passing it to path/filepath.EvalSymlinks to ensure it is the actual binary executable file. Errors are reported to stderr, but there should never be an error logged unless something is wrong with the Go runtime environment.
Functions ¶
func ArgsFrom ¶
ArgsFrom returns a list of field strings split on space with an extra trailing special space item appended if the line has any trailing spaces at all signifying a definite word boundary and not a potential prefix.
Example ¶
package main
import (
"fmt"
"github.com/rwxrob/bonzai"
)
func main() {
fmt.Printf("%q\n", bonzai.ArgsFrom(`greet hi french`))
fmt.Printf("%q\n", bonzai.ArgsFrom(`greet hi french `))
}
Output: ["greet" "hi" "french"] ["greet" "hi" "french" ""]
func AutoUpdate ¶ added in v0.0.19
AutoUpdate automatically updates the current process executable (see Exe) by starting a goroutine that checks the current version against a remote one and calling ReplaceSelf if needed.
If cur is an int assumes it is an isosec (see uniq.IsoSecond) and that newURL will return just a single line with an isosec in the body (usually a file named UPDATED).
If cur is a string assumes it is a valid semantic version (beginning with a 'v') and will expect a single JSON string (don't forget the wrapping double-quotes) from newURL (usually in a file named VERSION) which will be compared using the Compare function from golang.org/x/mod/semver.
If a URL to a checksum file (sum) is not empty will optionally validate the new version downloaded against the checksum before replace the currently running process executable with the new one. The format of the checksum file is the same as that output by any of the major checksum commands (sha512sum, for example) with one or more lines beginning with the checksum, whitespace, and then the name of the file. A single checksum file can be used for multiple versions but the most recent should always be at the top. When the update completes a message notifying of the update is logged to stderr.
Since AutoUpdate happens in the background no return value is provided. This means that failed Internet connections and other common reasons blocking the update silently fail. To enable logging of the update process (presumably for debugging) add the AutoUpdate flag to the Trace flags (trace.Flags|=trace.AutoUpdate).
func Exit ¶
func Exit()
Exit calls os.Exit(0) unless DoNotExit has been set to true. Cmds should never call Exit themselves returning a nil error from their Methods instead.
func ExitError ¶
func ExitError(err ...interface{})
ExitError prints err and exits with 1 return value unless DoNotExit has been set to true. Commands should usually never call ExitError themselves returning an error from their Method instead.
func Files ¶ added in v0.0.19
Files returns a slice of strings matching the names of the files within the given directory adding a slash to the end of any directories and escaping any spaces by adding backslash. Note that this function assumes forward slash path separators since completion is only supported on operating systems where such is the case.
Example ¶
package main
import (
"github.com/rwxrob/bonzai/util"
"github.com/rwxrob/fn/each"
)
func main() {
each.Println(util.Files("testdata/files"))
}
Output: testdata/files/bar testdata/files/blah testdata/files/dir1/ testdata/files/foo testdata/files/other testdata/files/some
Example (Empty) ¶
package main
import (
"os"
"github.com/rwxrob/bonzai/util"
"github.com/rwxrob/fn/each"
)
func main() {
os.Chdir("testdata/files")
defer os.Chdir("../..")
each.Println(util.Files(""))
}
Output: bar blah dir1/ foo other some
Example (Not_Directory) ¶
package main
import (
"fmt"
"github.com/rwxrob/bonzai/util"
)
func main() {
fmt.Println(util.Files("none"))
}
Output: []
Example (Spaces) ¶
package main
import (
"github.com/rwxrob/bonzai/util"
"github.com/rwxrob/fn/each"
)
func main() {
each.Println(util.Files("testdata/files/dir1"))
}
Output: testdata/files/dir1/some\ thing
func ReplaceSelf ¶ added in v0.0.19
ReplaceSelf replaces the current running executable at its current location with the successfully retrieved file at the specified URL or file path and duplicates the original files permissions. Only http and https URLs are currently supported. If not empty, a checksum file will be fetched from sumurl and used to validate the download before making the replacement. For security reasons, no backup copy of the replaced executable is kept. Also see AutoUpdate.
Types ¶
type Cmd ¶
type Cmd struct {
Name string `json:"name,omitempty"`
Aliases []string `json:"aliases,omitempty"`
Summary string `json:"summary,omitempty"`
Usage string `json:"usage,omitempty"`
Version string `json:"version,omitempty"`
Updated int `json:"updated,omitempty"` // isosec
Copyright string `json:"copyright,omitempty"`
License string `json:"license,omitempty"`
Description string `json:"description,omitempty"`
Site string `json:"site,omitempty"`
Source string `json:"source,omitempty"`
Issues string `json:"issues,omitempty"`
Other map[string]string `json:"issues,omitempty"`
Commands []*Cmd `json:"commands,omitempty"`
Params []string `json:"params,omitempty"`
Hidden []string `json:"hide,omitempty"`
Completer comp.Completer `json:"-"`
Call Method `json:"-"`
Caller *Cmd `json:"-"`
// contains filtered or unexported fields
}
Cmd is a struct the easier to use and read when creating implementations of the Command interface.
Params ¶
Params require a Method. While Methods may receive any number of arguments, Params are a way of helping completion for regular parameters. Standard completion will not recursively complete multiple params, one param per completion.
func (*Cmd) Add ¶
Add creates a new Cmd and sets the name and aliases and adds to Commands returning a reference to the new Cmd. The name must be first.
func (*Cmd) CmdNames ¶
Example ¶
package main
import (
"fmt"
"github.com/rwxrob/bonzai"
)
func main() {
foo := new(bonzai.Cmd)
foo.Add("bar")
foo.Add("blah")
foo.Add("other")
fmt.Println(foo.CmdNames())
}
Output: [bar blah other]
func (*Cmd) GetCommands ¶
GetCommands fulfills the comp.Command interface.
Example ¶
package main
import (
"fmt"
"github.com/rwxrob/bonzai"
)
func main() {
foo := new(bonzai.Cmd)
foo.Add("bar")
foo.Add("blah")
foo.Add("other")
fmt.Println(foo.GetCommands())
}
Output: [bar blah other]
func (*Cmd) GetCompleter ¶
GetCompleter fulfills the Command interface.
func (*Cmd) GetParams ¶
GetParams fulfills the comp.Command interface.
Example ¶
package main
import (
"fmt"
"github.com/rwxrob/bonzai"
)
func main() {
foo := new(bonzai.Cmd)
foo.Params = []string{"box", "bing", "and"}
fmt.Println(foo.GetParams())
}
Output: [box bing and]
func (*Cmd) IsHidden ¶
IsHidden returns true if the specified name is in the list of Hidden commands.
func (*Cmd) Run ¶
func (x *Cmd) Run()
Run is for running a command within a specific runtime (shell) and performs completion if completion context is detected. Otherwise, it executes the leaf Cmd returned from Seek calling its Method, and then Exits. Normally, Run is called from within main() to convert the Cmd into an actual executable program and normally it exits the program. Exiting can be controlled, however, with ExitOn/ExitOff when testing or for other purposes requiring multiple Run calls. Using Call instead will also just call the Cmd's Call Method without exiting. Note: Only bash runtime ("COMP_LINE") is currently supported, but others such a zsh and shell-less REPLs are planned.
func (*Cmd) Seek ¶
Example ¶
package main
import (
"fmt"
"github.com/rwxrob/bonzai"
"github.com/rwxrob/bonzai/inc/help"
)
func main() {
hello := &bonzai.Cmd{
Name: `hello`,
Params: []string{"there"},
Call: func(_ *bonzai.Cmd, args ...string) error {
if len(args) > 0 {
fmt.Printf("hello %v\n", args[0])
return nil
}
fmt.Println("hello")
return nil
},
}
hi := &bonzai.Cmd{
Name: `hi`,
Params: []string{"there", "ya"},
Call: func(_ *bonzai.Cmd, args ...string) error {
if len(args) > 0 {
fmt.Printf("hi %v\n", args[0])
return nil
}
fmt.Println("hi")
return nil
},
}
yo := &bonzai.Cmd{
Name: `yo`,
Call: func(_ *bonzai.Cmd, args ...string) error {
fmt.Println("yo")
return nil
},
}
salut := &bonzai.Cmd{
Name: `salut`,
Params: []string{"la"},
Call: func(_ *bonzai.Cmd, args ...string) error {
if len(args) > 0 {
fmt.Printf("salut %v\n", args[0])
return nil
}
fmt.Println("salut")
return nil
},
}
french := &bonzai.Cmd{
Name: `french`,
Aliases: []string{"fr"},
Commands: []*bonzai.Cmd{help.Cmd, salut},
}
greet := &bonzai.Cmd{
Name: `greet`,
Commands: []*bonzai.Cmd{help.Cmd, yo, hi, hello, french},
}
cmd, args := greet.Seek(bonzai.ArgsFrom(`hi there`))
fmt.Printf("%v %q\n", cmd.Name, args)
cmd, args = greet.Seek(bonzai.ArgsFrom(`french salut`))
fmt.Printf("%v %q\n", cmd.Name, args)
cmd, args = greet.Seek(bonzai.ArgsFrom(`french salut `))
fmt.Printf("%v %q\n", cmd.Name, args)
cmd, args = greet.Seek(bonzai.ArgsFrom(`french h`))
fmt.Printf("%v %q\n", cmd.Name, args)
cmd, args = greet.Seek(bonzai.ArgsFrom(`french help`))
fmt.Printf("%v %q\n", cmd.Name, args)
}
Output: hi ["there"] salut [] salut [""] french ["h"] help []
func (*Cmd) UsageError ¶ added in v0.0.7
UsageError returns an error with a single-line usage string.
type Method ¶
Method defines the main code to execute for a command (Cmd). By convention the parameter list should be named "args" if there are args expected and underscore (_) if not. Methods must never write error output to anything but standard error and should almost always use the log package to do so.
Directories
¶
| Path | Synopsis |
|---|---|
|
anim
module
|
|
|
choose
module
|
|
|
cmds
|
|
|
help
module
|
|
|
kimono
module
|
|
|
rayfish
module
|
|
|
sunrise
module
|
|
|
vars
module
|
|
|
completers/git
module
|
|
|
completers/git/examples/tags
module
|
|
|
depends
module
|
|
|
ds
module
|
|
|
dtime
module
|
|
|
edit
module
|
|
|
fn
module
|
|
|
tr
module
|
|
|
futil
module
|
|
|
github
module
|
|
|
Package inc contains a predefined set of popular commands that some may choose to include/compose into their Bonzai command trees.
|
Package inc contains a predefined set of popular commands that some may choose to include/compose into their Bonzai command trees. |
|
is
module
|
|
|
json
module
|
|
|
funcs
module
|
|
|
renderers/less
module
|
|
|
renderers/man
module
|
|
|
renderers/plain
module
|
|
|
viewers/less
module
|
|
|
old
|
|
|
is
Package z (often imported as "is") defines the Bonzai Parsing Expression Grammar Notation (BPEGN) (aka "Bonzai Scanner Expect" language) implemented entirely using Go types (mostly slices and structs).
|
Package z (often imported as "is") defines the Bonzai Parsing Expression Grammar Notation (BPEGN) (aka "Bonzai Scanner Expect" language) implemented entirely using Go types (mostly slices and structs). |
|
opts
module
|
|
|
persisters
module
|
|
|
injson
module
|
|
|
inmem
module
|
|
|
inprops
module
|
|
|
inyaml
module
|
|
|
pkg
|
|
|
cmd/bon
module
|
|
|
cmd/clip
module
|
|
|
cmd/var
module
|
|
|
run
module
|
|
|
scanner
module
|
|
|
term
module
|
|
|
test
|
|
|
multicall
module
|
|
|
tinout
module
|
|
|
to
module
|
|
|
Package trace provides a tracing framework for use by Bonzai applications.
|
Package trace provides a tracing framework for use by Bonzai applications. |
|
uniq
module
|
|
|
vars
module
|
|
|
web
module
|
|
|
yaml
module
|
|
|
yq
module
|
|
|
yt
module
|