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
- func ConfirmAction(options Confirmation, action func() error) error
- func ConfirmRequest(options Confirmation) error
- func ExitCode(err error) int
- func Interactive() bool
- func WriteError(w io.Writer, program string, err error, opts ErrorOptions) error
- type Confirmation
- type ErrorOptions
- type ExternalAnswer
- type Failure
- type Route
Constants ¶
const ( ExitSuccess = 0 ExitRuntime = 1 ExitUsage = 2 )
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 Interactive ¶
func Interactive() bool
Interactive reports whether at least one standard stream is a terminal.
func WriteError ¶
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 ¶
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 ¶
NewFailure checks the public code before an application can emit it.