cli

package
v0.5.2 Latest Latest
Warning

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

Go to latest
Published: Nov 17, 2026 License: MIT, MIT Imports: 10 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 is lys-shell's declarative command tree.

A tree is plain data: Commands holding Args and child Commands, each leaf naming a Run handler. Parsing, help, usage, errors, and the completion scripts all derive from that one declaration; nothing is restated per subcommand.

Index

Constants

This section is empty.

Variables

View Source
var CompletionShells = []PossibleValue{
	{Name: "bash"}, {Name: "elvish"}, {Name: "fish"}, {Name: "powershell"}, {Name: "zsh"},
}

CompletionShells are the shells a completions command accepts.

Functions

func ColorEnabled

func ColorEnabled(f *os.File) bool

ColorEnabled reports whether output to f should be colored: NO_COLOR wins, then CLICOLOR_FORCE, then CLICOLOR=0; otherwise a terminal colors when TERM is set to something other than dumb, CLICOLOR is on, or CI is set.

func Run

func Run(root *Command, args []string, out Output) int

Run parses args (without argv[0]) against root, runs the matched command, and returns the process exit status: 0 on success and for help/version, 2 for a usage error, 1 when the handler fails (printed as "Error: <err>").

func Value

func Value[T any](m *Matches, id string) (T, bool)

Value returns an arg's single parsed value (its default when absent and it has one). It panics when T is not the arg's parsed type: that is a declaration bug, caught by the tree tests.

func Values

func Values[T any](m *Matches, id string) []T

Values returns every parsed value of a multi-valued arg.

func WriteCompletions

func WriteCompletions(w io.Writer, root *Command, shell string) error

WriteCompletions writes the completion script for shell, generated from root with the root name as the binary name.

Types

type Arg

type Arg struct {
	// ID keys the parsed value in Matches; its upper-case form is the
	// default value name.
	ID    string
	Short rune
	Long  string
	// ValueName overrides the <PLACEHOLDER>.
	ValueName string
	// Help is the short help; LongHelp replaces it in --help.
	Help     string
	LongHelp string
	// Value parses (and so validates) the raw value. A positional
	// without one takes a String.
	Value ValueParser
	// Required makes the arg mandatory.
	Required bool
	// Multiple makes a positional collect every remaining value.
	Multiple bool
	// Defaults are the values used when the arg is absent; a nil slice
	// means no default (an empty-string default is []string{""}).
	Defaults []string
	// TrailingVarArg makes the positional swallow everything after it,
	// flags included.
	TrailingVarArg bool
	// AllowHyphenValues lets this arg's values start with '-'.
	AllowHyphenValues bool
}

Arg is one argument. An arg with neither Short nor Long is positional. A named arg without a Value parser is a boolean flag; with one it takes a single value.

type Command

type Command struct {
	// Name is the subcommand token; for the root it is the binary name.
	Name string
	// About is the one-line description shown in parent listings and
	// in short (-h) help.
	About string
	// LongAbout replaces About in --help when set.
	LongAbout string
	// AfterLongHelp is appended to --help only.
	AfterLongHelp string
	// Version, on the root, adds -V/--version printing "<name> <version>".
	Version string
	// Args are the options, flags, and positionals in declaration order.
	Args []*Arg
	// Subcommands are the children in declaration order.
	Subcommands []*Command
	// SubcommandOptional lets a command with subcommands run without
	// one. Otherwise a subcommand is
	// required and a bare invocation prints help to stderr, exit 2.
	SubcommandOptional bool
	// AllowHyphenValues lets every value-taking arg accept values that
	// start with '-'.
	AllowHyphenValues bool
	// DisableHelpFlag drops the generated -h/--help.
	DisableHelpFlag bool
	// Run executes the command. Commands with required subcommands have
	// none; the matched leaf's Run is called.
	Run func(*Matches) error
}

Command is one node of the tree. The zero value of every optional field is the default.

type Error

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

Error is a parse outcome that ends the run before a handler: help or version output (exit 0), or a usage error (exit 2).

func (*Error) Error

func (e *Error) Error() string

Error renders without styling so an *Error reads as a plain error.

func (*Error) ExitCode

func (e *Error) ExitCode() int

ExitCode is 0 for help/version, 2 for everything else.

func (*Error) Unwrap

func (e *Error) Unwrap() error

Unwrap returns the value parser's error behind an invalid value.

type ExitError

type ExitError struct{ Code int }

ExitError ends a run with Code and prints nothing: for handlers whose exit status is their result (the portal backend's).

func (ExitError) Error

func (e ExitError) Error() string

type Matches

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

Matches holds one command's parsed values.

func (*Matches) Flag

func (m *Matches) Flag(id string) bool

Flag reports a boolean flag.

func (*Matches) Stderr

func (m *Matches) Stderr() io.Writer

Stderr is where the handler prints diagnostics it reports itself (a returned error is printed by Run as "Error: ...").

func (*Matches) Stdout

func (m *Matches) Stdout() io.Writer

Stdout is where the handler prints its output.

func (*Matches) Subcommand

func (m *Matches) Subcommand() string

Subcommand names the matched subcommand, "" when none was given.

type Output

type Output struct {
	Stdout      io.Writer
	Stderr      io.Writer
	StdoutColor bool
	StderrColor bool
}

Output is where a run writes, and whether each stream takes ANSI styling: help on stdout and errors on stderr are colored independently.

func StdOutput

func StdOutput() Output

StdOutput is the process's own streams with the auto color choice.

type PossibleValue

type PossibleValue struct {
	Name string
	Help string
}

PossibleValue is one accepted value of an enumerated arg; Help shows in --help's "Possible values:" list.

type ValueParser

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

ValueParser turns one raw command-line value into its typed form, so a handler never re-validates a string.

var F64 ValueParser = f64Parser{}

F64 parses a 64-bit floating-point number.

var Path ValueParser = pathParser{}

Path accepts any non-empty value; an empty one is "a value is required".

var String ValueParser = stringParser{}

String accepts any value.

var U32 ValueParser = u32Parser{}

U32 parses a decimal unsigned 32-bit integer.

func Enum

func Enum(values ...PossibleValue) ValueParser

Enum accepts exactly one of values; the parsed value is the matching Name.

func Func

func Func[T any](parse func(string) (T, error)) ValueParser

Func parses with parse; its error becomes the invalid-value message.

Jump to

Keyboard shortcuts

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