arity

package
v0.0.0-...-d89a00e Latest Latest
Warning

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

Go to latest
Published: Jul 6, 2026 License: Apache-2.0, MIT Imports: 8 Imported by: 0

Documentation

Overview

Package arity parses a small specification language describing how many positional arguments a command may accept.

Specifications are read from textual configuration through Arity.UnmarshalText or ArityFunc.UnmarshalText. See those methods for the accepted grammar.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrBadArity indicates a command received a number of positional arguments
	// outside the range permitted by an [ArityFunc].
	ErrBadArity = errors.New("bad arity")

	// ErrEmpty indicates the specification contained no terms.
	ErrEmpty = errors.New("empty arity specification")

	// ErrSyntax indicates a term was not a recognized arity expression.
	ErrSyntax = errors.New("invalid arity syntax")

	// ErrNegative indicates a term contained a negative bound, which cannot be
	// an argument count.
	ErrNegative = errors.New("negative arity bound")

	// ErrEmptyRange indicates a term described a range containing no values.
	ErrEmptyRange = errors.New("empty arity range")

	// ErrOverlap indicates two terms described overlapping ranges.
	ErrOverlap = errors.New("overlapping arity ranges")
)

Functions

This section is empty.

Types

type Arity

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

Arity describes the set of permitted argument counts parsed from a specification.

func (Arity) Contains

func (a Arity) Contains(n int) bool

Contains reports whether n is a permitted argument count. The zero-value Arity permits no counts.

func (Arity) String

func (a Arity) String() string

String returns a human-readable description of the permitted argument counts, such as "at most 1 argument" or, for disjoint ranges, "exactly 1 argument, or at least 3 arguments". The zero-value Arity returns an empty string.

func (*Arity) UnmarshalText

func (a *Arity) UnmarshalText(text []byte) error

UnmarshalText parses an arity specification and configures the Arity to permit the described argument counts.

A specification is a comma-separated list of terms. Each term is an exact count ("3"), a comparison (">3", ">=3", "<3", "<=3"), or a range with an exclusive ("1..3") or inclusive ("1..=3") upper bound. Terms may touch but must not overlap.

It returns ErrEmpty, ErrSyntax, ErrNegative, ErrEmptyRange, or ErrOverlap when the specification is invalid.

type ArityFunc

type ArityFunc func(*cobra.Command, []string) error

ArityFunc validates that a command received a permitted number of positional arguments. It satisfies cobra's positional-argument validator signature.

func (*ArityFunc) UnmarshalText

func (af *ArityFunc) UnmarshalText(text []byte) error

UnmarshalText parses an arity specification, using the same grammar and returning the same errors as Arity.UnmarshalText. On success it configures the ArityFunc to report a wrapped ErrBadArity when a command receives an argument count outside the permitted range.

Jump to

Keyboard shortcuts

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