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
- Variables
- func Render(w io.Writer, v any) error
- func RenderJSONL(w io.Writer, v any) error
- func RenderMarkdown(w io.Writer, v any) error
- func Run(reg *registry.Registry, args []string) int
- func RunContext(ctx context.Context, reg *registry.Registry, args []string) int
- func RunContextWithOptions(ctx context.Context, reg *registry.Registry, args []string, opts Options) int
- func RunWithOptions(reg *registry.Registry, args []string, opts Options) int
- func ValidFormat(f string) bool
- type App
- type ExecContext
- type ExecFunc
- type Options
Constants ¶
const ( FormatAuto = "auto" FormatText = "text" FormatJSON = "json" FormatJSONL = "jsonl" FormatMarkdown = "markdown" )
FormatAuto / FormatText / FormatJSON / FormatJSONL / FormatMarkdown 是 --format 的合法取值常量。FormatAuto("" 等价)= 按 stdout 是否交互式 终端(TTY)自动选择:交互式用 FormatInteractive(默认 text),非交互式 (管道/重定向/被程序调用)用 FormatPiped(默认 jsonl)。
Variables ¶
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 ¶
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 RenderJSONL ¶ added in v0.4.2
RenderJSONL 以 JSON Lines 写出结果:切片/数组每个元素一行紧凑 JSON, 其余值整体一行。紧凑(不缩进)、每行以 \n 收尾,适合管道与流式消费。 nil 不输出任何内容。
func RenderMarkdown ¶ added in v0.4.2
RenderMarkdown 以 Markdown 写出结果:
nil -> 无输出 string/bool/数字 -> 裸值(转义 | 与换行) time.Time -> RFC 3339 struct, *struct -> 两列 | Field | Value | 表 []struct -> 以结构体字段为列的表 []基本类型 -> "- 元素" 无序列表 map -> 两列 | Key | Value | 表(字符串键排序)
单元格内的 | 转义为 \|,换行替换为 <br>,保证表格不破行。
func RunContext ¶
RunContext is Run with an explicit context for the invoked handlers.
func RunContextWithOptions ¶ added in v0.4.2
func RunContextWithOptions(ctx context.Context, reg *registry.Registry, args []string, opts Options) int
RunContextWithOptions 是 RunWithOptions 的显式 context 形态。
func RunWithOptions ¶ added in v0.4.2
RunWithOptions 是带前端选项(默认输出格式等)的 Run,供根派发器把 --xyz.format 注入 CLI 前端。
func ValidFormat ¶ added in v0.4.2
ValidFormat 报告 f 是否为合法的 --format 取值("" 视为 auto)。
Types ¶
type App ¶
type App struct {
// contains filtered or unexported fields
}
func NewWithOptions ¶ added in v0.2.0
NewWithOptions is New with frontend options; nil writers keep the defaults.
func (*App) Run ¶
New builds the command tree. Unbindable field kinds (nested structs, maps) and ambiguous positionals (required after optional) are configuration errors.
func (*App) RunContext ¶
RunContext is Run with an explicit context, which flows into the invoked handlers (graceful shutdown, cancellation).
type ExecContext ¶ added in v0.2.0
type ExecContext struct {
Path string // 点分注册名,如 user.add
Entry *spec.Entry // 命令元数据(Hints、InputSchema、OutputSchema)
JSON bool // 等价 Format==json(向后兼容;判断机器模式请优先用 Format)
// Format 是本次执行*解析后*的具体格式(text/json/jsonl/markdown,永不为
// auto)。机器模式(json/jsonl)下错误也以 JSON 写 stderr;显式非 text
// 格式绕过命令的 CLIOutputFunc,text 才进入 Output > 信封投影 > Render 链。
Format string
// Interactive 报告本次执行的输出目标是否为交互式终端(TTY)。自定义
// Output 函数与中间件可据此决定是否上色/用富排版(与格式轴正交,是将来
// 样式/彩色轴的同一信号)。
Interactive bool
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)
// Format 是全局默认输出格式(auto|text|json|jsonl|markdown,""=auto)。
// auto = 按 stdout 是否交互式终端解析为 FormatInteractive / FormatPiped。
// 通常由根派发器把 --xyz.format / Config.Format 注入到这里。
Format string
// FormatInteractive / FormatPiped 是 Format=auto 时两种上下文各自的具体
// 格式;空则用内置默认(交互式 text、非交互式 jsonl)。
FormatInteractive string
FormatPiped string
// FormatFromFlag 标记 Format 来自命令行 --xyz.format(命令行层,优先级
// 高于逐命令的 CliHints.Format);false 时 Format 视作代码级全局默认。
FormatFromFlag bool
// Interactive 强制指定交互式与否:nil = 按 Out 是否为 TTY 自动探测;
// 非 nil = 强制该值(测试与嵌入场景用,无需真实终端)。
Interactive *bool
}
Options configures frontend-level behavior for embedding (e.g. mounting the CLI inside a larger program). The zero value keeps os.Stdout/os.Stderr.