expander

package
v0.21.0 Latest Latest
Warning

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

Go to latest
Published: Aug 31, 2026 License: BSD-3-Clause Imports: 11 Imported by: 3

Documentation

Overview

Package expander provides an extension for representing expandable expressions in command line arguments. The main use case is for dynamically evaluating these expressions with values that occur during the execution of the evaluators.

Index

Constants

This section is empty.

Variables

View Source
var Nil = Func(nilImpl)

Nil provides an expander which always provides nil

Functions

func Expand added in v0.17.0

func Expand(pattern string, e Interface, opts ...Option) string

Expands expands the pattern using the given expander and produces a string

func Fprint

func Fprint(pattern string, w io.Writer, e Interface, opts ...Option) (count int, err error)

Fprint expands the pattern using the given expander and writes to the specified writer.

Types

type ErrUnknownToken

type ErrUnknownToken string

ErrUnknownToken is an error reporting a key that no expander recognized. Its string value is the unrecognized key.

func (ErrUnknownToken) Error

func (e ErrUnknownToken) Error() string

Error implements the error interface.

type Func

type Func func(string) any

Func converts the given string key into its variable expansion

func (Func) Expand

func (f Func) Expand(key string) any

Expand implements Interface by invoking f with key.

type Interface

type Interface interface {
	Expand(key string) any
}

Interface converts the given string key into its variable expansion

func Colors

func Colors() Interface

Colors provides an expander which resolves color and style names to their VT100 ANSI escape sequences, returning nil for unrecognized names.

func Compose

func Compose(expanders ...Interface) Interface

Compose combines the given expanders into a single expander, returning the first non-nil expansion produced by them in order.

func Dig added in v0.20.0

func Dig(v any) Interface

Dig navigates the object graph of a value using qualified names as keys.

func Env

func Env() Interface

Env provides an expander which looks up environment variables by name, returning nil when the variable is not set.

func ExpandSlice added in v0.17.0

func ExpandSlice[T any](slice []T) Interface

ExpandSlice creates an expander which uses the underlying slice as its input. Keys resolve as the index into the slice, including supporting negative indexes to reference from the end of the slice.

func Prefix

func Prefix(p string, e Interface) Interface

Prefix provides an expander which looks for and cuts a given prefix and delegates the result to the underlying expander

func Reflect added in v0.12.0

func Reflect(v any) Interface

Reflect provides a simple expander around a given value using reflection. Keys resolve, case-insensitively, to the exported fields of the value (or of the struct it points to) and to its exported methods which take no arguments and return a single value. Methods make it possible to expand values which don't expose their state as fields. For example, the key "year" applied to a time.Time resolves to its Year method. Fields take precedence over methods. For slices and arrays, keys can be parsed as integers as indexes. Negative indexes can be used. For maps with string keys, keys can retrieve the value. Other keys resolves to nil.

func Runtime added in v0.15.0

func Runtime() Interface

Runtime provides an expander for Go runtime variables: numCPU, os, arch, version. Intended for use with Prefix("go", Runtime()).

func Time

func Time(t time.Time) Interface

Time obtains an expander for the specified time

func Unknown

func Unknown() Interface

Unknown provides an expander which resolves every key to an ErrUnknownToken error. It is typically composed last so that unrecognized keys are reported rather than silently expanding to nil.

type Map

type Map map[string]any

Map provides an expander backed by a map, resolving keys directly to their corresponding values.

func (Map) Expand

func (m Map) Expand(k string) any

Expand returns the value stored under k, or nil when k is absent.

type Option added in v0.17.0

type Option interface {
	// contains filtered or unexported methods
}

Option configures how a pattern is compiled by Compile. The Syntax values are themselves options

func WithDelimiters added in v0.17.0

func WithDelimiters(start, end string) Option

WithDelimiters overrides the start and end delimiters used to recognize expressions within a pattern. The end delimiter must be a single byte. The default delimiters are "%(" and ")".

func WithMeta added in v0.17.0

func WithMeta(name string, pattern *Pattern) Option

WithMeta substitutes every expression named name with the expressions from the given pattern. It is applied after the pattern is compiled and may be specified more than once.

type Pattern

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

Pattern is a compiled expandable expression produced by Compile. A Pattern may be expanded repeatedly with different expanders.

func Compile

func Compile(pattern string, opts ...Option) *Pattern

Compile compiles pattern into a Pattern, applying any options. By default the SyntaxDefault syntax and the delimiters "%(" and ")" are used.

func CompilePattern deprecated

func CompilePattern(pattern, start, end string) *Pattern

CompilePattern compiles pattern using the given delimiters.

Deprecated: Use Compile with WithDelimiters instead.

func (*Pattern) AppendText added in v0.17.0

func (p *Pattern) AppendText(b []byte) ([]byte, error)

AppendText implements encoding.TextAppender. The output matches that of calling the Pattern.String method.

func (*Pattern) Expand

func (p *Pattern) Expand(expand Interface) string

Expand expands the pattern with the given replacements

func (*Pattern) ExpandAny added in v0.16.0

func (p *Pattern) ExpandAny(expand Interface) any

ExpandAny expands the pattern, returning the underlying type of the expansion when the pattern consists of a single expression with no format specifier applied.. When the pattern has multiple expressions, a literal fallback, or a format specifier, it falls back to string interpolation like Expand.

func (*Pattern) Fprint added in v0.16.0

func (p *Pattern) Fprint(w io.Writer, e Interface) (count int, err error)

Fprint expands the pattern using the given expander and writes to the specified writer. As a special case, if w has a method Expander() Interface, this method will be called to obtain an expander which composes with the expander e. The main use of this convention is to allow writers to supply control expressions. For an example, see Renderer.

func (*Pattern) MarshalText added in v0.17.0

func (p *Pattern) MarshalText() ([]byte, error)

MarshalText implements encoding.TextMarshaler. The output matches that of calling the Pattern.AppendText method.

func (*Pattern) String

func (p *Pattern) String() string

String implements fmt.Stringer, reconstructing the pattern's source representation using its delimiters.

func (*Pattern) SubexpNames added in v0.19.0

func (p *Pattern) SubexpNames() []string

SubexpNames returns the names of the sub-expressions in the Pattern which appear in the variable placeholders. Names are listed in the order they first occur, and each name is listed once no matter how many placeholders use it. Within SyntaxRecursive, this includes the names of the recursively named fallbacks. The whitespace expressions space, tab, newline, and empty are not variable placeholders and are therefore omitted.

func (*Pattern) UnmarshalText added in v0.17.0

func (p *Pattern) UnmarshalText(text []byte) error

UnmarshalText implements encoding.TextUnmarshaler by calling Compile on the encoded value using the default delimiters

type PatternValue added in v0.21.0

type PatternValue struct {
	Options []Option
	// contains filtered or unexported fields
}

PatternValue provides a wrapper around Pattern which can specify options and be used as the value for a flag or arg

func (*PatternValue) AppendText added in v0.21.0

func (p *PatternValue) AppendText(b []byte) ([]byte, error)

AppendText implements encoding.TextAppender. The output matches that of calling the Pattern.String method.

func (*PatternValue) MarshalText added in v0.21.0

func (p *PatternValue) MarshalText() ([]byte, error)

MarshalText implements encoding.TextMarshaler. The output matches that of calling the Pattern.AppendText method.

func (*PatternValue) Reset added in v0.21.0

func (p *PatternValue) Reset()

Reset clears the pattern for re-use. Options are considered configuration and are not reset.

func (*PatternValue) Set added in v0.21.0

func (p *PatternValue) Set(s string) error

Set provides the implementation of flag.Value

func (*PatternValue) String added in v0.21.0

func (p *PatternValue) String() string

String provides the string representation of the value

func (*PatternValue) UnmarshalText added in v0.21.0

func (p *PatternValue) UnmarshalText(text []byte) error

UnmarshalText implements encoding.TextUnmarshaler by calling Compile on the encoded value using the default delimiters

func (*PatternValue) Value added in v0.21.0

func (p *PatternValue) Value() *Pattern

Value compiles and obtains the pattern

type Renderer

type Renderer struct {
	io.Writer
	// contains filtered or unexported fields
}

Renderer is a specialized writer that understands writing to multiple files and the corresponding variable support. When used as a writer to Fprint, two control expressions are exposed, stdout and stderr, which can be used to redirect to the underlying writers. For example, "%(stderr)debug: %(v:#v)%(newline)%(stdout)%(v)" would print debug text to whatever writer was set for stderr.

func NewRenderer

func NewRenderer(stdout, stderr io.Writer) *Renderer

NewRenderer creates a Renderer that writes to stdout by default and exposes the stdout and stderr control expressions for switching the active writer.

func (*Renderer) Expander added in v0.16.0

func (r *Renderer) Expander() Interface

Expander gets the expander for Renderer, which exposes control expressions, stdout and stderr, which switch the writer to use

type Syntax

type Syntax int

Syntax selects the expression syntax used when compiling a pattern. A Syntax value is itself an Option.

const (
	// SyntaxDefault indicates that the expression syntax uses the
	// form <name>[':' <format>]. That is, an optional format string is allowed
	// to control the output.
	SyntaxDefault Syntax = iota

	// SyntaxRecursive causes recursive expression evaluation to
	// be allowed. For example, the expression ${VISUAL:${EDITOR}}
	// would evaluate both environment variables, falling back to EDITOR.
	SyntaxRecursive
)

func (Syntax) Compile deprecated

func (s Syntax) Compile(pattern string) *Pattern

Compile compiles pattern using the receiver's syntax.

Deprecated: Use Compile with the Syntax value as an option instead.

func (Syntax) CompilePattern deprecated

func (s Syntax) CompilePattern(pattern, start, end string) *Pattern

CompilePattern compiles pattern using the receiver's syntax and the given delimiters.

Deprecated: Use Compile with the Syntax value and WithDelimiters instead.

Jump to

Keyboard shortcuts

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