cli

package
v1.0.0-alpha.33 Latest Latest
Warning

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

Go to latest
Published: Nov 24, 2026 License: Apache-2.0 Imports: 18 Imported by: 0 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 holds what every costroid command shares on the command line: the shape of a help page, what a mistake and an error print, and the codes a command exits with.

Index

Constants

View Source
const (
	// ExitDone: the command did its work, and a verdict is success or neutral.
	ExitDone = 0
	// ExitFailed: the command did its work and the answer is a failure.
	ExitFailed = 1
	// ExitUsage: the command line is wrong.
	ExitUsage = 2
	// ExitCapped: run stopped the agent at the cap.
	ExitCapped = 124
	// ExitCouldNot: costroid could not do its work.
	ExitCouldNot = 125
	// ExitInterrupted: Ctrl-C ended the command. It is 128 and the number of
	// SIGINT, which a shell reports for a process the signal ended.
	ExitInterrupted = 130
)

The codes a costroid command exits with, one for each way a command ends. No command picks a number of its own. costroid run passes the agent's code through, so every code below ExitCapped is the agent's there, and a wrong command line of run itself exits ExitCouldNot.

View Source
const (
	// Width is the widest line a help page prints.
	Width = 80
	// Documentation is the address every page's own address starts with.
	Documentation = "https://docs.costroid.com"
)
View Source
const Troubleshooting = Documentation + "/troubleshooting"

Troubleshooting is the page that lists the lines costroid prints.

Variables

View Source
var QuietRow = Row{"-q, --quiet", "keep warnings and errors on standard error, and drop every other line"}

QuietRow is the flag's line under Flags on every help page.

View Source
var UsageExit = Row{"2", "the command line is wrong"}

UsageExit is the row every command's help lists for ExitUsage. run's lists none: its own wrong command line exits ExitCouldNot.

Functions

func Explain

func Explain(stderr io.Writer, err error)

Explain prints what stopped a command, under costroid's name: what is wrong on the first line, then why and what to do next when the error says them. An error that names no next step ends on the page that lists costroid's lines.

func Fail

func Fail(stderr io.Writer, err error) int

Fail explains err and answers the code its command exits with: ExitUsage for a command line that is wrong, ExitCouldNot for anything else.

func File

func File(err error) error

File is err said by the file it ends on, when it ends on one: "read a workflow: stat x: no such file or directory" reads "x does not exist". It is for a file the user named, where what each caller put before the file names a step of the program and not the thing that is wrong.

func Main

func Main(run func(ctx context.Context) int) int

Main runs a command and answers the code costroid exits with. SIGTERM ends the command's context, and the command ends as it chooses. Ctrl-C ends the context too, and costroid then exits ExitInterrupted, whatever the command answers, once the command has returned or the time it asked for has passed.

func Mistaken

func Mistaken(command string, err error) error

Mistaken says the command line of command is wrong for the reason err gives: an argument too many, a required flag left out, a value of the wrong form. The lines end on where the command's usage is. An error that is already a Mistake comes back as it is, and so does a request for help, which is no mistake.

func Nearest

func Nearest(typed string, names []string) []string

Nearest answers the names among names that typed most likely stands for, or none when none is near: the names one edit away, or else, for a word of five letters or more, the names two edits away, and otherwise the one name typed opens.

func NearestLine

func NearestLine(kind, before string, near []string) string

NearestLine names what is nearest to a mistyped word as one sentence: kind is command or flag, and before stands before each name. It is empty when near is.

func NoArgument

func NoArgument(set *flag.FlagSet) error

NoArgument is the mistake of an argument where set's command takes none. It answers nil when the command line gave none.

func Notes

func Notes(stderr io.Writer) io.Writer

Notes is where a command writes the lines --quiet drops: what it did, what it is about to wait for, and what to do next.

func OnInterrupt

func OnInterrupt(need Interrupt) (forget func())

OnInterrupt records what a command needs when Ctrl-C reaches it, until the function it answers is called.

func Parse

func Parse(set *flag.FlagSet, args []string, stderr io.Writer) error

Parse parses args with set and prints nothing. It answers flag.ErrHelp when help was asked for and a *Mistake when a flag was refused; the caller prints either. The set's name is the command's, with its subcommand when it has one. Parse gives every set --quiet, and under it stderr drops the lines that are neither a warning nor an error.

func Plain

func Plain(err error) string

Plain is err's text with what the system said put in words. Go names the call and its error, as in "open rows.jsonl: no such file or directory" or `Get "https://host/path": dial tcp 127.0.0.1:443: connect: connection refused`, and a reader named the file or the host, never the call.

func Quiet

func Quiet(stderr io.Writer) bool

Quiet reports whether the command line stderr belongs to gave --quiet.

func Say

func Say(stderr io.Writer, format string, args ...any)

Say prints one of the lines --quiet drops on stderr.

func Stopped

func Stopped(what string, then ...string) error

Stopped is work a command could not do: what is wrong, then why when costroid knows it or the choices the reader has, then what to do next. A line of why or of what to do is a sentence, and the last line names a command, a file or a page.

func Under

func Under(stderr io.Writer) io.Writer

Under is the writer stderr was made from, which a child process is given so that it holds the file itself.

func Usage

func Usage(command string) string

Usage is the line that says where the usage of command is, which a command's own finding about its command line ends on.

func Without

func Without(name string, err error) error

Without is err without the name of the package that made it, where that name opens its text or one of the parts a colon divides it into. A package opens its errors with its own name, as in "policy: rows line 1", and the name is the program's, not a thing the reader named.

Types

type Block

type Block interface {
	// contains filtered or unexported methods
}

Block is one piece of a section: Text, Rows or Lines.

type Group

type Group struct {
	Command string
	// Summary is one sentence, the one the command's Page opens with.
	Summary     string
	Subcommands []Row
	// Example is one command line, on as many lines as it needs.
	Example []string
}

Group is the short page of a command that takes a subcommand, which a call with none prints: what the command does, each subcommand with what it does, one call to copy and where the whole page is.

func (Group) String

func (g Group) String() string

func (Group) Unknown

func (g Group) Unknown(typed string) error

Unknown is the mistake of a subcommand the group does not have: it names typed and the nearest subcommand, when one is near, and runs neither.

type Interrupt

type Interrupt struct {
	// Wait is how long costroid waits for the command to return once its
	// context has ended, which a command that holds a child process needs to
	// stop it.
	Wait time.Duration
	// Doing says what the command does in that time, such as "stopping
	// claude". It is printed at once.
	Doing string
	// Restore runs at once, for a command that changed the terminal.
	Restore func()
	// Own says the command ends on an interrupt by its own rule, as a
	// service that drains does, and exits with its own code.
	Own bool
}

Interrupt is what a command needs when Ctrl-C reaches it. The zero value asks for nothing: the command's context ends and costroid exits ExitInterrupted.

type Lines

type Lines []string

Lines print as they are, below the prose they belong to.

type Messages

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

Messages is a command's standard error. Standard output holds what a command was asked to print, and Messages holds what it says about its work. A warning or an error is written to it as to any writer, and stays under --quiet. Every other line goes through Say or Notes, and --quiet drops it.

func NewMessages

func NewMessages(to io.Writer) *Messages

NewMessages is the standard error of one command line, written to to.

func (*Messages) Write

func (m *Messages) Write(p []byte) (int, error)

type Mistake

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

Mistake is a command line that is wrong: a flag the command does not have, a flag without its value, a value of the wrong form, an argument too many or one left out. Its text opens with what was typed and what is wrong with it, then names the nearest name the command has, when one is near, and the command's help. A command that stops on one exits ExitUsage.

func (*Mistake) Error

func (m *Mistake) Error() string

type Page

type Page struct {
	// Command names the exit code table.
	Command string
	// Summary is one sentence.
	Summary string
	// Usage holds one line per element. A form too long for one line goes on
	// in the next element, which opens with the spaces that align it.
	Usage []string
	// Examples are command lines to run as written, the commonest first.
	Examples []string
	// Flags gives each flag, with its value's name, and what it does. String
	// adds --quiet, which every command takes.
	Flags    []Row
	Sections []Section
	// Environment says what the command reads from the environment.
	Environment []Block
	Exits       []Row
	// Docs is the command's page under Documentation.
	Docs string
}

Page is one command's help. String prints its parts in one order: what the command does, how it is called, calls to copy, its flags, whatever else the command needs said, what it reads from the environment, its exit codes and the address of its page in the documentation.

func (Page) String

func (p Page) String() string

type Row

type Row [2]string

Row is a term and what it means.

type Rows

type Rows []Row

Rows is a table of terms.

type Section

type Section struct {
	Heading string
	Body    []Block
}

Section is text under a heading that says what it is.

type Text

type Text string

Text is prose. A blank line separates its paragraphs, and each is wrapped again, so the line breaks of the source do not show.

Jump to

Keyboard shortcuts

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