flags

package
v0.1.24 Latest Latest
Warning

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

Go to latest
Published: Aug 3, 2026 License: AGPL-3.0 Imports: 3 Imported by: 0

Documentation

Overview

Package flags translates a command segment's Unix-style flag tokens into the typed option values that the underlying gloo-foo/cmd-* constructors expect.

A command declares its flags as a Set of Spec entries (short letter, long name, and either a constant option for boolean flags or a Maker for value-taking flags). Parse walks a segment's arguments against that Set, separating typed options from positionals, and honours short clusters (-lwc), inline values (-n10, --num=5), the GNU "-NUM" shorthand, and "--" long forms. The package holds no knowledge of the REPL or of any specific command; it is the reusable flag-translation layer behind the line domain.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AtoiArg

func AtoiArg(value Argument) (int, error)

AtoiArg parses an argument as a base-10 integer.

func FirstOr

func FirstOr(positional Argv, def Default) string

FirstOr returns the first positional as a string, or def when there are none.

func IntList

func IntList(value Argument) ([]int, error)

IntList parses a comma-separated integer list like "1,3,5".

func NumArg

func NumArg(value Argument) (any, error)

NumArg parses an argument as an int when integral, otherwise a float64. Seq accepts both forms.

func RangeArg

func RangeArg(value Argument) (lo, hi int, err error)

RangeArg parses a "lo-hi" integer range.

Types

type Argument

type Argument string

Argument is a single parsed token of a command segment.

type Argv

type Argv []Argument

Argv is the ordered argument list of one command segment.

func Parse

func Parse(set Set, args Argv) (opts []any, positional Argv, err error)

Parse splits args into typed flag options and positional arguments using the command's flag table.

func (Argv) Anys

func (a Argv) Anys() []any

Anys converts the arguments to an []any (for variadic ...any constructors that classify their inputs, such as Exec).

func (Argv) Strings

func (a Argv) Strings() []string

Strings converts the arguments to a plain string slice.

type Default

type Default string

Default is the fallback argument value a command uses when no positional was supplied (e.g. "." as the implicit path of ls and find).

type LongFlag

type LongFlag string

LongFlag is a flag's long name, without its dashes (the "num" of "--num").

type Maker

type Maker func(value Argument) (any, error)

Maker converts a flag's string value into the typed option value that the underlying cmd-* constructor expects (e.g. "10" into head.HeadLines(10)).

func Int64Maker

func Int64Maker(maker func(int64) any) Maker

Int64Maker builds a value-flag Maker for an int64-typed option.

func IntMaker

func IntMaker(maker func(int) any) Maker

IntMaker builds a value-flag Maker for an int-typed option.

func StrMaker

func StrMaker(maker func(string) any) Maker

StrMaker builds a value-flag Maker for a string-typed option.

type Set

type Set []Spec

Set is a command's complete flag table.

type ShortFlag

type ShortFlag string

ShortFlag is a flag's single-letter short name, without its dash (the "n" of "-n").

type Spec

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

Spec declares one command flag: its short letter (without dash), its long name (without dashes), and either a constant option (boolean flags) or a maker (value-taking flags). A numeric spec has no short or long name; it captures a bare "-NUM" token (the GNU head/tail shorthand) through its maker.

func Bool

func Bool(short ShortFlag, long LongFlag, opt any) Spec

Bool declares a boolean flag mapping to a constant option value.

func Num

func Num(maker Maker) Spec

Num declares the GNU "-NUM" shorthand, mapping the digits through maker.

func Value

func Value(short ShortFlag, long LongFlag, maker Maker) Spec

Value declares a value-taking flag mapping its value through maker.

Jump to

Keyboard shortcuts

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