cli

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Aug 18, 2026 License: MIT Imports: 15 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package cli is the CLI frontend: it consumes a registry's entries and turns their cli bindings (shorthands, positionals, env fallbacks, transport-specific defaults) into a command tree. Dotted registry names map to nested subcommands: "user.add" becomes "user add".

The frontend is implemented on the standard library only (no cobra / pflag), so this package carries zero third-party dependencies.

Output rendering lives in Render: primitives print bare (no {"data": ...} envelope), structs print as aligned key/value pairs, slices of structs as an aligned table, and --json flips everything to raw JSON.

Index

Constants

This section is empty.

Variables

View Source
var Version = "dev"

Version is the version reported by the CLI frontend's -v/--version flag when the frontend is embedded directly via cli.Run. The root dispatcher (xyz.Run) owns its own xyz.Version and answers -v before reaching here.

Functions

func Render

func Render(w io.Writer, v any) error

Render writes the CLI frontend's default human-readable form of a command result:

nil                 -> nothing
string              -> the raw string
bool, int, float    -> the bare value
time.Time           -> RFC 3339
[]basic             -> one element per line
struct, *struct     -> aligned "key  value" pairs from json-tagged fields
[]struct            -> an aligned table (header + separator + rows)
map                 -> sorted "key  value" pairs

The result is never wrapped in an envelope like {"data": ...}: --json emits the raw JSON value instead of calling Render.

func Run

func Run(reg *registry.Registry, args []string) int

Run builds the tree and executes, for one-call use.

func RunContext

func RunContext(ctx context.Context, reg *registry.Registry, args []string) int

RunContext is Run with an explicit context for the invoked handlers.

Types

type App

type App struct {
	// contains filtered or unexported fields
}

func New

func New(reg *registry.Registry) (*App, error)

App is the CLI frontend for one registry.

func NewWithOptions added in v0.2.0

func NewWithOptions(reg *registry.Registry, opts Options) (*App, error)

NewWithOptions is New with frontend options; nil writers keep the defaults.

func (*App) Run

func (a *App) Run(args []string) int

New builds the command tree. Unbindable field kinds (nested structs, maps) and ambiguous positionals (required after optional) are configuration errors.

func (*App) RunContext

func (a *App) RunContext(ctx context.Context, args []string) int

RunContext is Run with an explicit context, which flows into the invoked handlers (graceful shutdown, cancellation).

func (*App) SetOutput added in v0.2.0

func (a *App) SetOutput(out, errOut io.Writer)

SetOutput redirects the frontend's output streams; nil keeps the current writer. Useful when embedding the CLI in a larger program or in tests.

func (*App) Use added in v0.2.0

func (a *App) Use(mws ...ExecFunc)

Use appends Execute middleware (outermost first). next() continues the remaining chain down to Invoke + rendering. Middleware may mutate args, short-circuit (skip next for a custom rendering) or wrap next for timing/tracing.

type ExecContext added in v0.2.0

type ExecContext struct {
	Path  string      // 点分注册名,如 user.add
	Entry *spec.Entry // 命令元数据(Hints、InputSchema、OutputSchema)
	JSON  bool        // --json 是否生效(未调用 next 时自行渲染可参考)
	Out   io.Writer   // 结果的输出目标
}

ExecContext is a read-only snapshot of one leaf-command execution, passed to Execute middleware (App.Use).

type ExecFunc added in v0.2.0

type ExecFunc func(ctx context.Context, ec *ExecContext, args map[string]any, next func() error) error

ExecFunc is an Execute middleware around leaf execution: args is the already-parsed argument map (flags, env and positionals applied); next() continues the chain down to Invoke + rendering. Return value semantics are identical to normal command errors (mapped to exit codes by kind).

type Options added in v0.2.0

type Options struct {
	Out    io.Writer // 命令结果的输出目标(默认 os.Stdout)
	ErrOut io.Writer // 错误与帮助的输出目标(默认 os.Stderr)
}

Options configures frontend-level behavior for embedding (e.g. mounting the CLI inside a larger program). The zero value keeps os.Stdout/os.Stderr.

Jump to

Keyboard shortcuts

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