funcs

package module
v0.8.6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 22, 2024 License: Apache-2.0 Imports: 9 Imported by: 1

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var Map = template.FuncMap{
	"exepath":      run.Executable,
	"exename":      run.ExeName,
	"execachedir":  run.ExeCacheDir,
	"exestatedir":  run.ExeStateDir,
	"execonfigdir": run.ExeConfigDir,
	"cachedir":     futil.UserCacheDir,
	"confdir":      futil.UserConfigDir,
	"homedir":      futil.UserHomeDir,
	"statedir":     futil.UserStateDir,
	"pathsep":      func() string { return string(os.PathSeparator) },
	"pathjoin":     filepath.Join,
	"indent":       to.Indented,
	"aka":          AKA,
	"code":         Code,
	"commands":     Commands,
	"command":      Command,
	"summary":      Summary,
	"usage":        Usage,
	"hasenv":       HasEnv,
	"long":         Long,
}

Functions

func AKA added in v0.3.0

func AKA(x *bonzai.Cmd) string

AKA returns the name followed by all aliases in parenthesis joined with a forward bar (|) suitable for inlining within help documentation. It is available as aka in Map as well.

Example
package main

import (
	"fmt"

	"github.com/rwxrob/bonzai"
	"github.com/rwxrob/bonzai/mark"
	"github.com/rwxrob/bonzai/mark/funcs"
)

func main() {

	var help = &bonzai.Cmd{
		Name:  `help`,
		Alias: `h|-h|-help|--help|/?`,
	}

	fmt.Println(funcs.AKA(help))
	out, _ := mark.Fill(help, funcs.Map, `The {{aka .}} command.`)
	fmt.Println(out)

}
Output:

`help` (`h`|`-h`|`-help`|`--help`|`/?`)
The `help` (`h`|`-h`|`-help`|`--help`|`/?`) command.

func CmdTree added in v0.4.0

func CmdTree(x *bonzai.Cmd, depth int) string

func Code added in v0.3.0

func Code(it any) string

Code returns a string with Markdown backticks surrounding it after converting it to a string with fmt.Printf. This is also available as "code" in Map. This fulfills a specific use case when a developer would like to use backticks in a bonzai.Cmd.Long or bonzai.Cmd.Short but cannot because backticks are already used to contain the multi-line text itself.

Example
package main

import (
	"fmt"

	"github.com/rwxrob/bonzai/mark"
	"github.com/rwxrob/bonzai/mark/funcs"
)

func main() {

	long := `
I'll use the {{code "{{code}}"}} thing instead with something like
{{code "go mod init"}} since cannot use backticks.`

	fmt.Println(funcs.Code(`go mod init`))
	out, _ := mark.Fill(nil, funcs.Map, long)
	fmt.Println(out)

}
Output:

`go mod init`

I'll use the `{{code}}` thing instead with something like
`go mod init` since cannot use backticks.

func Command added in v0.7.0

func Command(x *bonzai.Cmd) string

Command returns the name of the command joined to any aliases at the end.

Example
package main

import (
	"fmt"

	"github.com/rwxrob/bonzai"
	"github.com/rwxrob/bonzai/mark/funcs"
)

func main() {
	var subFooCmd = &bonzai.Cmd{
		Name:  `subfoo`,
		Alias: `sf`,
		Short: `under the foo command`,
	}

	var fooCmd = &bonzai.Cmd{
		Name:  `foo`,
		Alias: `f`,
		Short: `foo this command`,
		Cmds:  []*bonzai.Cmd{subFooCmd},
	}

	var barCmd = &bonzai.Cmd{
		Name:  `bar`,
		Alias: `b`,
		Short: `bar this command`,
	}

	var Cmd = &bonzai.Cmd{
		Name:  `mycmd`,
		Alias: `my|cmd`,
		Short: `my command short summary`,
		Cmds:  []*bonzai.Cmd{fooCmd, barCmd},
		Def:   fooCmd,
	}

	Cmd.Seek(`foo`, `subfoo`) // required for default detection
	fmt.Println(funcs.Commands(Cmd))

}
Output:

foo      - foo this command (default)
  subfoo - under the foo command
bar      - bar this command

func Commands added in v0.5.0

func Commands(x *bonzai.Cmd) string

Commands generates and returns a formatted string representation of the commands and subcommands for the [Cmd] instance. It aligns [Cmd].Short summaries in the output for better readability, adjusting spaces based on the position of the dashes.

Example (Hidden)
package main

import (
	"fmt"

	"github.com/rwxrob/bonzai"
	"github.com/rwxrob/bonzai/mark/funcs"
)

func main() {
	var subFooCmd = &bonzai.Cmd{
		Name:  `subfoo`,
		Alias: `sf`,
		Short: `under the foo command`,
	}

	var hiddenCmd = &bonzai.Cmd{
		Name: `imhidden`,
		Cmds: []*bonzai.Cmd{{Name: `some`}, {Name: `other`}},
	}

	var fooCmd = &bonzai.Cmd{
		Name:  `foo`,
		Alias: `f`,
		Short: `foo this command`,
		Cmds:  []*bonzai.Cmd{subFooCmd},
	}

	var barCmd = &bonzai.Cmd{
		Name:  `bar`,
		Alias: `b`,
		Short: `bar this command`,
	}

	var Cmd = &bonzai.Cmd{
		Name:  `mycmd`,
		Alias: `my|cmd`,
		Short: `my command short summary`,
		Cmds:  []*bonzai.Cmd{fooCmd, barCmd, hiddenCmd.AsHidden()},
		Def:   fooCmd,
	}

	Cmd.Seek(`foo`, `sssh`, `some`) // required for default detection
	fmt.Println(funcs.Commands(Cmd))

}
Output:

foo      - foo this command (default)
  subfoo - under the foo command
bar      - bar this command

func Fill added in v0.8.2

func Fill(it any, f template.FuncMap, in string) (string, error)

Fill processes the input string (in) as a pkg/text/template using the provided function map (f) and the data context (it). It returns the rendered output as a string or an error if any step fails. No functions beyond those passed are merged.

func HasEnv added in v0.7.0

func HasEnv(x *bonzai.Cmd) bool

HasEnv returns true if command has declared any environment variables in its Vars. Note that inherited vars are not resolved to see if they resolved to environment variables (Var.E).

func Long added in v0.7.0

func Long(x *bonzai.Cmd) (string, error)

Long returns the Long description of the command if found dedented so that it is left justified completely. It is first filled using the Map merged with the Funcs from the passed command.

func Summary added in v0.7.0

func Summary(x *bonzai.Cmd) string

Summary returns the Name joined by a long dash with the Short description if it has one.

Example
package main

import (
	"fmt"

	"github.com/rwxrob/bonzai"
	"github.com/rwxrob/bonzai/mark/funcs"
)

func main() {

	var Cmd = &bonzai.Cmd{
		Name:  `mycmd`,
		Short: `my command short summary`,
		Do:    bonzai.Nothing,
	}

	fmt.Print(funcs.Summary(Cmd))

}
Output:

`mycmd` - my command short summary

func Usage added in v0.7.0

func Usage(x *bonzai.Cmd) string

Usage return the Command plus any available Usage information. If there is no usage, it is inferred.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL