cli

package
v0.1.12 Latest Latest
Warning

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

Go to latest
Published: Oct 11, 2026 License: MIT Imports: 18 Imported by: 1 module, 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 generates a kong command line from an operation registry.

Each operation's CLI path ("note <id> delete") becomes nested kong commands built with reflect.StructOf. Input fields keep their own kong tags, so a field is declared once for the CLI and the wire. A path placeholder (<id>) becomes a branching positional argument and fills the input field with that json name. Root flags declared in Options.Globals with a json tag fill the input field with the same json name, so `tool --limit 5 notes list` and `tool notes list --limit 5` both work.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Run

func Run(ctx context.Context, reg *op.Registry, opts Options, args []string) (code int)

Run parses args, runs the selected operation or command and returns the process exit code. It never calls os.Exit.

func Validate

func Validate(reg *op.Registry, opts Options) error

Validate builds the command tree without running it, so a tool's tests catch a declaration kong rejects, such as a duplicate flag. It also rejects a command flag that kong accepts but would never deliver: one that reuses the name, an alias or the short form of a root flag (--json, --agent, --fields, --yes, --version, --help or one of the tool's Globals), and an operation flag named --apply or --dry-run. Run does not repeat this check, so a tool that already ships such a flag keeps running.

Types

type Command

type Command struct {
	Name string
	Help string
	Cmd  any
}

Command is a hand-written kong command. Cmd is a pointer to a kong command struct whose Run method takes a *Context.

type Context

type Context struct {
	context.Context
	Registry *op.Registry
	Stdin    io.Reader
	Stdout   io.Writer
	Stderr   io.Writer
	// JSON reports whether the caller asked for machine output (--json or
	// --agent).
	JSON bool
}

Context is what a hand-written command's Run method receives.

type Options

type Options struct {
	// Description is the root help text.
	Description string
	// Globals is an optional pointer to a struct of root flags. A field with a
	// json tag fills the operation input field with the same json name and
	// type whenever the flag is given on the command line; otherwise the
	// input's own default applies, as it does over HTTP and MCP. Fields
	// without a json tag are root flags the tool reads itself.
	Globals any
	// Commands are hand-written commands mounted next to the operations, for
	// the few actions that do not fit the registry.
	Commands []Command
	Stdin    io.Reader
	Stdout   io.Writer
	Stderr   io.Writer
}

Options configures the generated command line.

Jump to

Keyboard shortcuts

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