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 Exit()
- func ExitError(err ...interface{})
- func ExitOff()
- func ExitOn()
- 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.
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 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.
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"`
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/cmd"
)
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{cmd.Help, salut},
}
greet := &bonzai.Cmd{
Name: `greet`,
Commands: []*bonzai.Cmd{cmd.Help, 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
|
|
|
Package cmd contains a predefined set of popular commands that some may choose to compose into their Bonzai command trees, notably cmd.Help.
|
Package cmd contains a predefined set of popular commands that some may choose to compose into their Bonzai command trees, notably cmd.Help. |
|
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
|
|
|
Package each shamelessly attempts to bring the better parts of Lisp loops to Go specifically in order to enable rapid, and clean applications development --- particularly when replacing shell scripts with Go.
|
Package each shamelessly attempts to bring the better parts of Lisp loops to Go specifically in order to enable rapid, and clean applications development --- particularly when replacing shell scripts with Go. |
|
edit
module
|
|
|
Package fn implements the traditional map/filter/reduce/each functions and an array type (A) for those who prefer a more object-oriented approach.
|
Package fn implements the traditional map/filter/reduce/each functions and an array type (A) for those who prefer a more object-oriented approach. |
|
tr
module
|
|
|
futil
module
|
|
|
github
module
|
|
|
is
module
|
|
|
json
module
|
|
|
Package loop shamelessly attempts to bring the better parts of Lisp loops to Go specifically in order to enable rapid, and clean applications development --- particularly when replacing shell scripts with Go.
|
Package loop shamelessly attempts to bring the better parts of Lisp loops to Go specifically in order to enable rapid, and clean applications development --- particularly when replacing shell scripts with Go. |
|
Package mapf contains nothing but map functions suitable for use with the fn.Map generic function or the equivalent fn.A method.
|
Package mapf contains nothing but map functions suitable for use with the fn.Map generic function or the equivalent fn.A method. |
|
mark
module
|
|
|
funcs
module
|
|
|
renderers/less
module
|
|
|
renderers/man
module
|
|
|
renderers/plain
module
|
|
|
viewers/less
module
|
|
|
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
|
|
|
uniq
module
|
|
|
vars
module
|
|
|
web
module
|
|
|
yaml
module
|
|
|
yq
module
|
|
|
yt
module
|