Documentation
¶
Overview ¶
Package expr provides conventions for command line expressions, which are flag-like arguments which are ordered and processed using pipe and filter semantics.
Evaluators ¶
Evaluators support the binding pattern.
Say that you have an expression that has an operand "-name TEXT", and you have a function SetName(string)cli.Evaluator that can produce the evaluator that is actually used. You can use the following to simplify the binding of the string argument.
&cli.Arg {
Name: "expression",
Value: expr.Expression{
Exprs: []*expr.Expr{
{
Name: "name",
Args: cli.Args(cli.String()),
Evaluator: expr.BindEvaluator(SetName, bind.String()),
}
}
}
}
Notice that the bind.String() call doesn't require you to name the argument from which to obtain the value. When unspecified, it uses the first argument in the argument list for the Expr.
Compilers ¶
When an expression is evaluated, the sequence of binding evaluators that was parsed is compiled into the evaluator which implements the evaluation pipeline. The default compiler, Compile, chains each binding evaluator to the next one. Setting Expression.Compiler replaces this behavior, which is typically used to rewrite the sequence of binding evaluators before delegating to Compile:
&expr.Expression{
Compiler: func(items []expr.BindingEvaluator) expr.Evaluator {
return expr.Compile(append(items, implicitPrint))
},
}
Expression.Compile applies the compiler eagerly, producing a copy of the expression which evaluates using the pipeline that was compiled.
Index ¶
- Constants
- type Action
- type ActionEvaluator
- type BindingEvaluator
- type Compiler
- type Evaluator
- func BindEvaluator[T any, E Evaluator](factory func(T) E, t bind.Binder[T]) Evaluator
- func BindEvaluator2[T, U any, E Evaluator](eval func(T, U) E, t bind.Binder[T], u bind.Binder[U]) Evaluator
- func BindEvaluator3[T, U, V any, E Evaluator](eval func(T, U, V) E, t bind.Binder[T], u bind.Binder[U], v bind.Binder[V]) Evaluator
- func Compile(items []BindingEvaluator) Evaluator
- func ComposeEvaluator(e ...Evaluator) Evaluator
- func Do(a cli.Action) Evaluator
- func Error(err error) Evaluator
- func EvaluatorOf(v any) Evaluator
- func NewEvaluator[V any](fn func(context.Context, V, func(any) error) error) Evaluator
- func NewEvaluator0[V any](fn func(context.Context, V) error) Evaluator
- type EvaluatorFunc
- type Expr
- func (e *Expr) Arg(name any) (*cli.Arg, bool)
- func (e *Expr) LocalArgs() []*cli.Arg
- func (e *Expr) LookupData(name any) (any, bool)
- func (e *Expr) Names() []string
- func (s Expr) OccurrenceValue(name string, index int) any
- func (s Expr) OccurrenceValues(name string) []any
- func (s Expr) Raw(name string) []string
- func (s Expr) RawOccurrences(name string) []string
- func (e *Expr) SetAliases(a []string)
- func (e *Expr) SetCategory(name string)
- func (e *Expr) SetData(name, v any)
- func (e *Expr) SetDescription(value string)
- func (e *Expr) SetHelpText(name string)
- func (e *Expr) SetHidden(value bool)
- func (e *Expr) SetLocalArgs(args []*cli.Arg) error
- func (e *Expr) SetManualText(name string)
- func (e *Expr) SetName(name string)
- func (e *Expr) SetNumeric(value bool)
- func (e *Expr) SetUsageText(value string)
- func (e *Expr) Synopsis() string
- type Expression
- func (e *Expression) Append(expr BindingEvaluator)
- func (e *Expression) BindingEvaluators() iter.Seq[BindingEvaluator]
- func (e *Expression) Clone() *Expression
- func (e *Expression) Compile() *Expression
- func (e *Expression) Evaluate(ctx context.Context, items ...any) error
- func (e *Expression) EvaluateParallel(ctx context.Context, jobs int, items ...any) error
- func (e *Expression) Initializer() cli.Action
- func (e *Expression) Prepend(expr BindingEvaluator)
- func (e *Expression) Set(arg string) error
- func (e *Expression) String() string
- func (e *Expression) VisibleExprs() []*Expr
- func (e *Expression) Walk(fn func(BindingEvaluator) error) error
- type Invariant
- type Operator
- type Predicate
- type PredicateContext
- type Yielder
Constants ¶
const ( AlwaysTrue = Invariant(true) AlwaysFalse = Invariant(false) )
Always or never yield the input value
const ( // ParseAllowInlineValues is placed in the Uses pipeline of the Arg that contains // the expression and when present allows expressions to parse with inline arguments // when either (a) the short alias is an uppercase letter or (b) the inline // arguments have the equal sign. For example, if an expression -filter has // the alias -l, then it is acceptable to parse -lKEY=VALUE because an // equal sign is present. If the alias -L were present, then -LVALUE could // be parsed. These rules are designed to avoid ambiguity because in general, // ordinary expression names shouldn't use uppercase names and the equal sign // is a common delimiter in arguments. ParseAllowInlineValues cli.Option = reservedoptions.ExprParseAllowInlineValues )
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Action ¶ added in v0.23.0
type Action = cli.Action
Action represents the building block of the various actions that the expr extension supports
func Evaluate ¶ added in v0.23.0
Evaluate provides an action which evaluates the expression with the given input items. The expression is retrieved from the arg in scope.
func EvaluateParallel ¶ added in v0.23.0
EvaluateParallel provides an action which evaluates the expression with the given input items. The expression is retrieved from the arg in scope and runs in parallel. By convention, if this is present within the Uses pipeline, it registers a flag to configure the number of jobs.
func SetEvaluator ¶
SetEvaluator provides an action used in the Uses pipeline of the Expr which updates its evaluator
type ActionEvaluator ¶ added in v0.14.0
type ActionEvaluator interface {
Evaluator
cli.Action
}
ActionEvaluator provides an evaluator which can also be used as an action
func NewActionEvaluator ¶ added in v0.14.0
func NewActionEvaluator(action cli.Action, eval Evaluator) ActionEvaluator
NewActionEvaluator provides an evaluator which also provides an action. The typical use is to implement an initializer within the action that sets up the required flags and actions that the evaluator depends on.
type BindingEvaluator ¶ added in v0.14.0
type BindingEvaluator interface {
Evaluator
// Expr retrieves the expression operator if it is available
Expr() *Expr
}
BindingEvaluator provides the relationship between an evaluator and the evaluation context. Optionally, the original Expr is made available. A binding can support being reset if it exposes a method Reset(). Resetting a binding occurs when an expression is evaluated multiple times.
func NewBindingEvaluator ¶ added in v0.14.0
func NewBindingEvaluator(ev Evaluator, exprlookup ...any) BindingEvaluator
NewBindingEvaluator creates an expression binding evaluator. The ev parameter is how the expression is evaluated. The remaining arguments specify the *Expr expression operator to use and optionally a Lookup. The main use case for this function is to create a custom evaluation step that is appended to the expression pipeline
type Compiler ¶ added in v0.22.0
type Compiler func([]BindingEvaluator) Evaluator
Compiler converts the sequence of binding evaluators within an expression into the evaluator which implements the evaluation pipeline. A compiler is set on Expression.Compiler, and when unset, the default compiler, Compile, is used.
type Evaluator ¶
type Evaluator interface {
// Evaluate performs the evaluation. The v argument is the value of the prior
// expression operator. The yield argument is used to pass one or more additional
// values to the next expression operator.
Evaluate(c context.Context, v any, yield func(any) error) error
}
Evaluator provides the evaluation function for an expression operator.
func BindEvaluator ¶ added in v0.10.0
BindEvaluator produces an evaluator from the bound values.
func BindEvaluator2 ¶ added in v0.10.0
func BindEvaluator2[T, U any, E Evaluator](eval func(T, U) E, t bind.Binder[T], u bind.Binder[U]) Evaluator
BindEvaluator2 produces an evaluator from the bound values.
func BindEvaluator3 ¶ added in v0.10.0
func BindEvaluator3[T, U, V any, E Evaluator](eval func(T, U, V) E, t bind.Binder[T], u bind.Binder[U], v bind.Binder[V]) Evaluator
BindEvaluator3 produces an evaluator from the bound values.
func Compile ¶ added in v0.22.0
func Compile(items []BindingEvaluator) Evaluator
Compile provides the default compiler for an expression, which converts the sequence of binding evaluators into the evaluator that implements the evaluation pipeline. Each binding evaluator yields the values it produces to the next one in the sequence, and the last one yields to the yielder which is passed to the resulting evaluator's Evaluate method.
func ComposeEvaluator ¶
ComposeEvaluator produces an evaluator which considers each evaluator in turn. If any evaluator yields the value, evaluation stops. If any evaluator returns an error, the evaluation stops and returns the error. This evaluator can be thought of as a logical conjunction in the case where evaluators work like Boolean predicates. Indeed, Predicate is often the type of the evaluators passed this function.
func Error ¶
Error provides an evaluator which yields an error. The zero value is also useful, returning a generic error
func EvaluatorOf ¶
EvaluatorOf creates an expression evaluator for a given value. The value must be bool or a function. If a bool, then it works as a predicate for the corresponding invariant (i.e. false filters out all values, and true includes all values). If an error value, then it always returns such an error. If a function, the signature must match either the Evaluator.Evaluate function signature or a variation that excludes the context and/or yielder. You can also use bool as a return type as in the same signature used by Predicate. These are valid signatures:
- func(*cli.Context, any, func(any)error) error
- func(*cli.Context, any) error
- func(*cli.Context, any) bool
- func(*cli.Context, any)
- func(*cli.Context) error
- func(*cli.Context) bool
- func(*cli.Context)
- func(context.Context, any, func(any)error) error
- func(context.Context, any) error
- func(context.Context, any) bool
- func(context.Context, any)
- func(context.Context) error
- func(context.Context) bool
- func(context.Context)
- func(any, func(any)error) error
- func(any) bool
- func(any) error
- func(any)
- func() bool
- func() error
- func()
Each of these signatures can also name a type that is more specific than any for the value parameter, in which case reflection is used to invoke the function. For example, func(context.Context, string) bool is valid, and each value in the expression pipeline is passed to it as a string; a value which can't be used as a string causes evaluation to return an error. The yield function is not covariant, however: it must always be spelled func(any) error.
func NewEvaluator ¶ added in v0.22.0
NewEvaluator creates an evaluator from a strongly typed function. The value flowing through the expression pipeline is converted to V before fn is invoked
func NewEvaluator0 ¶ added in v0.22.0
NewEvaluator0 creates an evaluator from a strongly typed function which does not yield values directly. The value flowing through the expression pipeline is converted to V before fn is invoked, and if fn returns no error, the value is yielded to continue the pipeline.
type EvaluatorFunc ¶
EvaluatorFunc provides the basic function for an Evaluator
type Expr ¶
type Expr struct {
// Name provides the name of the expression operator. This value must be set, and it is used to access
// the expression operator's value via the context
Name string
// Aliases provides a list of alternative names for the expression operator. In general, Name should
// be used for the long name of the expression operator, and Aliases should contain the short name.
// If there are additional names for compatibility reasons, they should be included
// with Aliases but listed after the preferred names. Note that only one short name
// and one long name is displayed on help screens by default.
Aliases []string
// Args contains each of the arguments that are processed for the expression operators. Expression
// operators don't contain values directly; they process one or more arguments.
Args []*cli.Arg
// HelpText contains text which briefly describes the usage of the expression operator.
// For style, generally the usage text should be limited to about 40 characters.
// Sentence case is recommended for the usage text. Uppercase is recommended for the
// text of placeholders. The placeholder is used in the synopsis for the expression operator as well
// as error messages.
HelpText string
// ManualText provides the text shown in the manual. The default templates don't use this value
ManualText string
// UsageText provides the usage for the expression operator. If left blank, a succinct synopsis
// is generated from the type of the expression operator's arguments
UsageText string
// Category specifies the expression operator category. When categories are used, operators are grouped
// together on the help screen
Category string
// Description provides a long description. The long description is
// not used in any templates by default. The type of Description should be string or
// fmt.Stringer. Refer to func Description for details.
Description any
// Evaluate provides the evaluation behavior for the expression. The value should
// implement Evaluator or support runtime conversion to that interface via
// the rules provided by the cli.EvaluatorOf function. If the evaluator is also a
// [cli.Action] or implements the method Initializer() cli.Action as a convention, then
// the action will be invoked during the initialization of the Expression.
Evaluate any
// Before executes before the expression is evaluated. Refer to cli.Action about the correct
// function signature to use.
Before any
// After executes after the expression is evaluated. Refer to cli.Action about the correct
// function signature to use.
After any
// Uses provides an action handler that is always executed during the initialization phase
// of the app. Typically, hooks and other configuration actions are added to this handler.
Uses any
// Data provides an arbitrary mapping of additional data. This data can be used by
// middleware and it is made available to templates
Data map[string]any
// Options sets various options about how to treat the expression operator. For example, options can
// hide the expression operator.
Options cli.Option
// contains filtered or unexported fields
}
Expr represents an operator in an expression. An expression is composed of an ordered series of operators meant to describe how to process one or more values. A well-known implementation of an expression is in the Unix `find` command where each file is processed through a series of operands to filter a list of files.
func (*Expr) LookupData ¶
LookupData obtains the data if it exists
func (Expr) OccurrenceValue ¶ added in v0.16.0
func (Expr) OccurrenceValues ¶ added in v0.16.0
func (Expr) Raw ¶ added in v0.20.0
Raw obtains the values which were specified for an arg. The empty name designates the expression itself, whose raw value is the args it was parsed from.
func (Expr) RawOccurrences ¶ added in v0.20.0
RawOccurrences obtains the values which were specified for an arg, excluding the name that was used. The empty name designates the expression itself, whose occurrences are the args it was parsed from excluding the name of the expression.
func (*Expr) SetCategory ¶
SetCategory sets the category of the expression
func (*Expr) SetData ¶
SetData sets the specified metadata on the expression operator. Despite its signature, Expr does not
func (*Expr) SetDescription ¶
SetDescription sets the description of the expression
func (*Expr) SetHelpText ¶
SetHelpText sets the help text of the expression
func (*Expr) SetLocalArgs ¶
SetLocalArgs sets arguments
func (*Expr) SetManualText ¶
SetManualText sets the manual text of the expression
func (*Expr) SetNumeric ¶ added in v0.18.0
SetNumeric marks the expression as handling the numeric syntax -0, -1, etc.
func (*Expr) SetUsageText ¶
SetUsageText sets the usage text of the expression
type Expression ¶
type Expression struct {
// Exprs identifies the expression operators that are allowed
Exprs []*Expr
// Compiler provides the function which converts the binding evaluators that
// were parsed into the evaluator which is used as the evaluation pipeline.
// When nil, the default behavior, which is implemented by Compile, is used.
// A custom compiler is typically used to rewrite the sequence of binding
// evaluators before delegating to Compile. The sequence passed to the
// compiler is a copy, so it is safe to rewrite it.
Compiler Compiler
// contains filtered or unexported fields
}
Expression provides the parsed result of the expression that can be evaluated with the given inputs.
func FromContext ¶
func FromContext(c context.Context, nameopt ...string) *Expression
FromContext obtains the expression from the context. When nameopt is not specified or is the empty string, then the expression from the current arg or apparent from one of the args in the current command is returned.
func (*Expression) Append ¶
func (e *Expression) Append(expr BindingEvaluator)
func (*Expression) BindingEvaluators ¶ added in v0.14.0
func (e *Expression) BindingEvaluators() iter.Seq[BindingEvaluator]
BindingEvaluators enumerates all the binding evaluators on the expression
func (*Expression) Clone ¶ added in v0.22.0
func (e *Expression) Clone() *Expression
Clone provides a copy of the expression. The expression operators, the arguments which were parsed, and the binding evaluators are shallow copied, which makes it possible to rewrite or add to the binding evaluators of the copy without affecting the original expression.
func (*Expression) Compile ¶ added in v0.22.0
func (e *Expression) Compile() *Expression
Compile provides a copy of the expression with the compiler step applied. The resulting expression evaluates using the pipeline which was compiled from the binding evaluators as they exist now rather than compiling the pipeline when it is evaluated. Consequently, binding evaluators which are added to the copy using Append or Prepend take no part in its evaluation.
func (*Expression) Evaluate ¶
func (e *Expression) Evaluate(ctx context.Context, items ...any) error
Evaluate evaluates the expression pipeline for each of the items. The pipeline is obtained from the compiler, which is Expression.Compiler or the default compiler, Compile, when it is unset. If the expression was compiled using Expression.Compile, the pipeline that was compiled is used as-is.
func (*Expression) EvaluateParallel ¶ added in v0.16.0
EvaluateParallel evaluates the expression pipeline in parallel for multiple items. The jobs parameter controls the maximum number of concurrent evaluations. When jobs is zero or negative, the method uses runtime.NumCPU() as the default. Context propagation allows goroutines to be canceled or to time out. All errors are joined and returned together.
func (*Expression) Initializer ¶
func (e *Expression) Initializer() cli.Action
Initializer is an action that binds expression handling to an argument. This is set up automatically when a command defines any expression operators. The exprFunc argument is used to determine which expressions to used. If nil, the default behavior is used which is to lookup Command.Exprs from the context
func (*Expression) Prepend ¶
func (e *Expression) Prepend(expr BindingEvaluator)
func (*Expression) Set ¶
func (e *Expression) Set(arg string) error
func (*Expression) String ¶
func (e *Expression) String() string
func (*Expression) VisibleExprs ¶
func (e *Expression) VisibleExprs() []*Expr
VisibleExprs filters all expression operators by whether they are not hidden
func (*Expression) Walk ¶
func (e *Expression) Walk(fn func(BindingEvaluator) error) error
type Invariant ¶
type Invariant bool
Invariant provides an evaluator which either always or never yields the input value
func (Invariant) Initializer ¶ added in v0.23.0
type Operator ¶ added in v0.23.0
type Operator rune
Operator provides an operator in the small predicate expression language. Though this implements BindingEvaluator, it is only meant as a placeholder for which a Compiler must interpret. Indeed, the default Compile function panics when they are present.
Operators that can be parsed into expressions. Unlike other exprs, these are typically symbols. Indeed, the underlying type of this is rune which represents their common representation.
type Predicate ¶
Predicate provides a simple predicate which filters values. The function takes the prior operand and returns true or false depending upon whether the operand should be yielded to the next step in the expression pipeline.
type PredicateContext ¶ added in v0.23.0
PredicateContext provides a simple predicate which filters values. The function takes the context and prior operand and returns true or false depending upon whether the operand should be yielded to the next step in the expression pipeline.
Directories
¶
| Path | Synopsis |
|---|---|
|
Package expander provides an extension for representing expandable expressions in command line arguments.
|
Package expander provides an extension for representing expandable expressions in command line arguments. |
|
Package operator provides the small predicate expression language.
|
Package operator provides the small predicate expression language. |
|
Package printer provides the expression operators which print the values which flow through an expression evaluation pipeline.
|
Package printer provides the expression operators which print the values which flow through an expression evaluation pipeline. |