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 ¶
- Variables
- func ColorEnabled(f *os.File) bool
- func Run(root *Command, args []string, out Output) int
- func Value[T any](m *Matches, id string) (T, bool)
- func Values[T any](m *Matches, id string) []T
- func WriteCompletions(w io.Writer, root *Command, shell string) error
- type Arg
- type Command
- type Error
- type ExitError
- type Matches
- type Output
- type PossibleValue
- type ValueParser
Constants ¶
This section is empty.
Variables ¶
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 ¶
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 ¶
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 ¶
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.
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.
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).
type Matches ¶
type Matches struct {
// contains filtered or unexported fields
}
Matches holds one command's parsed values (clap's ArgMatches).
func (*Matches) Stderr ¶
Stderr is where the handler prints diagnostics it reports itself (a returned error is printed by Run as "Error: ...").
func (*Matches) Subcommand ¶
Subcommand names the matched subcommand, "" when none was given.
type Output ¶
Output is where a run writes, and whether each stream takes ANSI styling (clap colors help on stdout and errors on stderr independently).
type PossibleValue ¶
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.