command

package
v0.4.1 Latest Latest
Warning

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

Go to latest
Published: May 25, 2025 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package command implements generic nested command parsing.

Index

Constants

View Source
const UsageInternal = "internal"

UsageInternal causes the command to be hidden from help text when set as the usage string.

Variables

View Source
var (
	ErrEmptyTree = errors.New("subcommand tree has no nodes")
	ErrNoMatch   = errors.New("did not match any subcommand")
)
View Source
var ErrHelp = errors.New("help requested")

Functions

This section is empty.

Types

type BoolFlag

type BoolFlag bool

BoolFlag is the default value of a bool flag.

func (BoolFlag) Define

func (v BoolFlag) Define(b *strings.Builder, set *flag.FlagSet, p any, name, usage string)

type Command

type Command interface {
	Parse(arguments []string) error

	// MustParse determines exit outcomes for Parse errors
	// and calls handleError if [HandlerFunc] returns a non-nil error.
	MustParse(arguments []string, handleError func(error))
	// contains filtered or unexported methods
}

func New

func New(output io.Writer, logf LogFunc, name string, early HandlerFunc) Command

New initialises a root Node.

type Flag

type Flag[T any] interface {
	// Flag defines a generic flag type in Node's flag set.
	Flag(p any, name string, value FlagDefiner, usage string) T
}

type FlagDefiner

type FlagDefiner interface {
	// Define defines the flag in set.
	Define(b *strings.Builder, set *flag.FlagSet, p any, name, usage string)
}

FlagDefiner is a deferred flag definer value, usually encapsulating the default value.

type FlagError

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

FlagError wraps errors returned by flag.

func (FlagError) Is

func (e FlagError) Is(target error) bool

func (FlagError) Success

func (e FlagError) Success() bool

type HandlerFunc

type HandlerFunc = func(args []string) error

HandlerFunc is called when matching a directly handled subcommand tree.

type IntFlag

type IntFlag int

IntFlag is the default value of an int flag.

func (IntFlag) Define

func (v IntFlag) Define(b *strings.Builder, set *flag.FlagSet, p any, name, usage string)

type LogFunc

type LogFunc = func(format string, a ...any)

LogFunc is the function signature of a printf function.

type Node

type Node baseNode[Node]

type RepeatableFlag

type RepeatableFlag []string

RepeatableFlag implements an ordered, repeatable string flag.

func (*RepeatableFlag) Define

func (r *RepeatableFlag) Define(b *strings.Builder, set *flag.FlagSet, _ any, name, usage string)

func (*RepeatableFlag) Set

func (r *RepeatableFlag) Set(v string) error

func (*RepeatableFlag) String

func (r *RepeatableFlag) String() string

type StringFlag

type StringFlag string

StringFlag is the default value of a string flag.

func (StringFlag) Define

func (v StringFlag) Define(b *strings.Builder, set *flag.FlagSet, p any, name, usage string)

Jump to

Keyboard shortcuts

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