cli

package
v2.1.0 Latest Latest
Warning

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

Go to latest
Published: Apr 26, 2026 License: MIT Imports: 16 Imported by: 3

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Run

func Run(root any) error

Run executes the application based on the provided root struct. It parses the CLI arguments, resolves commands, binds flags, infuses dependencies, and runs lifecycle hooks.

Example:

func main() {
	app := &CLI{}
	if err := cli.Run(app); err != nil {
		log.Fatal(err)
	}
}

Types

type AfterRunner

type AfterRunner interface {
	After() error
}

AfterRunner is an interface for commands that run after the main run.

type App added in v2.0.3

type App struct {
	RootNode   *parser.CommandNode
	Translator help.Translator
	// contains filtered or unexported fields
}

App represents a CLI application.

func New added in v2.0.3

func New(root any) (*App, error)

New creates a new App from a root struct.

func (*App) AddCommand added in v2.0.3

func (a *App) AddCommand(name string, cmd *parser.CommandNode)

AddCommand adds a dynamic command to the application.

func (*App) GenBashCompletion added in v2.1.0

func (a *App) GenBashCompletion(w io.Writer) error

GenBashCompletion writes a bash completion script for the app.

func (*App) GenFishCompletion added in v2.1.0

func (a *App) GenFishCompletion(w io.Writer) error

GenFishCompletion writes a fish completion script for the app.

func (*App) GenZshCompletion added in v2.1.0

func (a *App) GenZshCompletion(w io.Writer) error

GenZshCompletion writes a zsh completion script for the app.

func (*App) Reload added in v2.0.4

func (a *App) Reload() error

Reload re-parses the root struct to pick up dynamic changes (e.g. map entries).

Example:

err := app.Reload()

func (*App) Run added in v2.0.3

func (a *App) Run() error

Run executes the application.

func (*App) SetContext added in v2.1.0

func (a *App) SetContext(ctx context.Context)

SetContext sets the context for the application.

func (*App) SetName added in v2.0.4

func (a *App) SetName(name string)

SetName sets the name of the root command.

func (*App) SetTranslator added in v2.0.4

func (a *App) SetTranslator(tr help.Translator)

SetTranslator sets the translator for the application.

func (*App) SetVersion added in v2.1.0

func (a *App) SetVersion(v string)

SetVersion sets the version string for the application.

type Base

type Base struct {
	Logger log.Logger      `internal:"ignore"`
	Ctx    context.Context `internal:"ignore"`
}

Base is a struct that can be embedded in commands to provide common functionality.

type BeforeRunner

type BeforeRunner interface {
	Before() error
}

BeforeRunner is an interface for commands that run before the main run.

type Runner

type Runner interface {
	Run() error
}

Runner is an interface for commands that can be run.

Jump to

Keyboard shortcuts

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