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 ¶
- Variables
- func Expand(pattern string, e Interface, opts ...Option) string
- func Fprint(pattern string, w io.Writer, e Interface, opts ...Option) (count int, err error)
- type ErrUnknownToken
- type Func
- type Interface
- type Map
- type Option
- type Pattern
- func (p *Pattern) AppendText(b []byte) ([]byte, error)
- func (p *Pattern) Expand(expand Interface) string
- func (p *Pattern) ExpandAny(expand Interface) any
- func (p *Pattern) Fprint(w io.Writer, e Interface) (count int, err error)
- func (p *Pattern) MarshalText() ([]byte, error)
- func (p *Pattern) String() string
- func (p *Pattern) UnmarshalText(text []byte) error
- func (p *Pattern) WithMeta(name string, pattern *Pattern) *Pattern
- type Renderer
- type Syntax
Constants ¶
This section is empty.
Variables ¶
var Nil = Func(nilImpl)
Nil provides an expander which always provides nil
Functions ¶
Types ¶
type ErrUnknownToken ¶
type ErrUnknownToken string
func (ErrUnknownToken) Error ¶
func (e ErrUnknownToken) Error() string
type Interface ¶
Interface converts the given string key into its variable expansion
func ExpandSlice ¶ added in v0.17.0
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 ¶
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
Reflect provides a simple expander around a given value using reflection
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
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 ")".
type Pattern ¶
type Pattern struct {
// contains filtered or unexported fields
}
func Compile ¶
Compile compiles pattern into a Pattern, applying any options. By default the SyntaxDefault syntax and the delimiters "%(" and ")" are used.
func CompilePattern
deprecated
func (*Pattern) AppendText ¶ added in v0.17.0
AppendText implements encoding.TextAppender. The output matches that of calling the Pattern.String method.
func (*Pattern) ExpandAny ¶ added in v0.16.0
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
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
MarshalText implements encoding.TextMarshaler. The output matches that of calling the Pattern.AppendText method.
func (*Pattern) UnmarshalText ¶ added in v0.17.0
UnmarshalText implements encoding.TextUnmarshaler by calling Compile on the encoded value using the default delimiters
type Renderer ¶
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 ¶
type Syntax ¶
type Syntax int
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 )