cli

package
v0.3.25 Latest Latest
Warning

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

Go to latest
Published: Nov 11, 2026 License: MIT Imports: 11 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 port of the clap subset the Rust CLI (lys-shell/src/cli) uses, so the Go binary keeps its help text, flags, error messages, exit codes, and shell completions byte-for-byte.

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. The rendering ports clap_builder 4.5 (output/help_template.rs, output/usage.rs, error/format.rs, parser/parser.rs) and clap_complete 4.6 (aot/shells).

Index

Constants

This section is empty.

Variables

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

CompletionShells are clap_complete's Shell values, the accepted arguments of a completions command.

Functions

func ColorEnabled

func ColorEnabled(f *os.File) bool

ColorEnabled reports whether output to f should be colored, with anstream's auto choice: 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 (clap's), 1 when the handler fails (printed as "Error: <err>", as lys-shell's main.rs does).

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 clap_complete's script for shell, generated from root (clap_complete::generate with the root name as bin 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
	// (a clap derive Vec<T>).
	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 (clap's trailing_var_arg + allow_hyphen_values).
	TrailingVarArg bool
	// AllowHyphenValues lets this arg's values start with '-'.
	AllowHyphenValues bool
}

Arg is one argument (a clap Arg). An arg with neither Short nor Long is positional. A named arg without a Value parser is a boolean flag (ArgAction::SetTrue); 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 (a multi-paragraph
	// doc comment in clap derive).
	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 (clap derive's Option<Subcommand>). 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 '-' (clap's Command::allow_hyphen_values).
	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 (a clap Command). The zero value of every optional field is clap's 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). It renders as clap's RichFormatter does.

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 clap's: 0 for help/version, 2 for everything else.

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 (clap's ArgMatches).

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 (clap colors help on stdout and errors on stderr 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 (clap's value_parser!), so a handler never re-validates a string.

var F64 ValueParser = f64Parser{}

F64 parses an f64 with Rust's f64::from_str grammar and messages.

var Path ValueParser = pathParser{}

Path accepts any non-empty value (value_parser!(PathBuf)); an empty one is "a value is required".

var String ValueParser = stringParser{}

String accepts any value (value_parser!(String)).

var U32 ValueParser = u32Parser{}

U32 parses a u32 the way clap's RangedI64ValueParser<u32> does: an i64 parse (Rust's messages) and then the range check.

func Enum

func Enum(values ...PossibleValue) ValueParser

Enum accepts exactly one of values (a clap ValueEnum); the parsed value is the matching Name.

func Func added in v0.2.0

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

Func parses with parse (a clap value_parser over a fn); 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