cli

package
v0.33.0 Latest Latest
Warning

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

Go to latest
Published: Nov 30, 2026 License: AGPL-3.0 Imports: 16 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 maps errors to the exit codes and stderr diagnostics of a command-line program and checks the confirmation of commands that ask or change data.

A Failure carries a validated code, a message and a next step for a person, optionally another one for an agent. WriteError prints "<program>: <code>: <message>" and the next step of the route, adds an optional detail as one JSON line, shows an external service's answer as a block of its own, and appends usage text only for syntax errors. ExitCode maps any error to success, a runtime failure or a usage problem.

Always enforced

Every text passes the redaction hook of ErrorOptions before it is cut or escaped, and control characters are made visible. Nothing is written for an invalid diagnostic. An unrepresentable value and an unknown or repeated field are usage problems. An external service's answer is always marked untrusted and is redacted before it is bounded: at most 8 entries, 800 bytes per message and 200 per other field, and the block says "truncated" when something was cut.

ConfirmRequest never reads standard input and never waits for an answer nobody can give: in agent mode or without a terminal, a needed question becomes a failure that names the flag to repeat the request with. A data-changing request needs its own confirmation in addition to the answer to an ordinary question.

Options

  • ErrorOptions.Usage: usage text written after a syntax error. Default: none is written.
  • ErrorOptions.Route: the route of the diagnostic, human or agent. Default: RouteHuman.
  • ErrorOptions.Redact: replaces known secrets in a text. Default: texts are used as they are.
  • ErrorOptions.ExternalJSON: writes an external answer as one JSON line. Default: a text block.
  • Failure.AgentNextStep: another next step for the agent route. Default: NextStep.
  • Failure.Detail: one JSON line after the next step. Default: none.
  • Failure.External: an external service's answer. Default: none.
  • Confirmation.RequiresConfirm: the request changes data and needs the Confirm flag besides Yes. Default: only an ordinary question is asked.

Index

Constants

View Source
const (
	ExitSuccess = 0
	ExitRuntime = 1
	ExitUsage   = 2
)
View Source
const (
	AgentFlagHelp   = "agent mode: compact output by default, no progress, and no questions; use --yes for actions that ask"
	YesFlagHelp     = "answer this command's ordinary confirmation question with yes"
	ConfirmFlagHelp = "confirm this one data-changing request; required separately from --yes"
)

Variables

This section is empty.

Functions

func ConfirmAction

func ConfirmAction(options Confirmation, action func() error) error

ConfirmAction checks all confirmation gates before invoking action.

func ConfirmRequest

func ConfirmRequest(options Confirmation) error

ConfirmRequest never reads stdin. It opens a terminal only when a question is needed.

func ExitCode

func ExitCode(err error) int

ExitCode maps a diagnostic to the public process exit contract.

func Interactive

func Interactive() bool

Interactive reports whether at least one standard stream is a terminal.

func WriteError

func WriteError(w io.Writer, program string, err error, opts ErrorOptions) error

WriteError writes the stable diagnostic to stderr. Only syntax errors carry usage text. Nothing is written when the diagnostic itself is invalid.

Types

type Confirmation

type Confirmation struct {
	Agent           bool
	Yes             bool
	Confirm         bool
	RequiresConfirm bool
	Question        string
}

Confirmation describes the two distinct gates of a changing command.

type ErrorOptions

type ErrorOptions struct {
	// Usage is written only after a syntax error.
	Usage string
	Route Route
	// Redact replaces the known secrets in a text. It runs on every text before it is cut or escaped.
	Redact func(string) string
	// ExternalJSON writes an external answer as one JSON line instead of a text block.
	ExternalJSON bool
}

ErrorOptions are the application's choices for one diagnostic.

type ExternalAnswer

type ExternalAnswer struct {
	// Source names who answered, for example "service"; the block is headed "<Source> answer".
	Source string
	httpx.Original
}

ExternalAnswer is what an external service answered about a failure. It is untrusted: WriteError redacts, bounds, and marks it as a block of its own, so a reader never takes it for the application's text.

type Failure

type Failure struct {
	Code     output.Code
	Message  string
	NextStep string
	// AgentNextStep replaces NextStep on RouteAgent when it is set.
	AgentNextStep string
	// Detail is written as one JSON line after the next step.
	Detail any
	// External is what an external service answered; it is shown apart from the application's own text.
	External *ExternalAnswer
	Usage    bool
	Syntax   bool
	Cause    error
}

Failure is a diagnostic with a stable code and an actionable next step for each route.

func NewFailure

func NewFailure(code output.Code, message, nextStep string, usage bool) (*Failure, error)

NewFailure checks the public code before an application can emit it.

func (*Failure) Error

func (e *Failure) Error() string

func (*Failure) Unwrap

func (e *Failure) Unwrap() error

type Route

type Route int

Route is the reader a diagnostic addresses.

const (
	// RouteHuman addresses a person at a terminal: a next step names a command or a key.
	RouteHuman Route = iota
	// RouteAgent addresses an agent: where only a person can act, the next step tells it to ask the user.
	RouteAgent
)

Jump to

Keyboard shortcuts

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