Documentation
¶
Overview ¶
Copyright 2025 The Joe-cli Authors. All rights reserved. Use of this source code is governed by a BSD-style license that can be found in the LICENSE file. Package bind provides functions for creating late-bound actions and evaluators. It's common to produce actions or evaluators that delegate to other actions or evaluators, to values in the context, or to models where the values that are needed have been set. This package provides idioms that can simplify this code and encourage the pattern of separating the concerns.
For example, compare the equivalent flags:
&cli.App{
Name: "a",
Flags: []*cli.Flag{
{
Name: "cumbersome",
Action: func(c *cli.Context) error {
value := c.Int("")
return logic(value)
},
Value: new(int),
},
{
Name: "clean",
Uses: bind.Call(logic, bind.Int()),
},
},
}
With the clean flag, you benefit from the implicit declaration of the type of the flag's Value and not having to map the value manually. In addition, it encourages you to factor out the logic as its own function with the signature func(int)error, which is decoupled from Joe's types and probably easier to test.
Evaluators ¶
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: cli.Expression{
Exprs: []*cli.Expr{
{
Name: "name",
Args: cli.Args(cli.String()),
Evaluator: bind.Evaluator(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.
Copyright 2025 The Joe-cli Authors. All rights reserved. Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
Index ¶
- func Action[T any](fn func(T) cli.Action, t Binder[T]) cli.Action
- func Action2[T, U any](fn func(T, U) cli.Action, t Binder[T], u Binder[U]) cli.Action
- func Action3[T, U, V any](fn func(T, U, V) cli.Action, t Binder[T], u Binder[U], v Binder[V]) cli.Action
- func Call[T any](call func(T) error, t Binder[T]) cli.Action
- func Call2[T, U any](call func(T, U) error, t Binder[T], u Binder[U]) cli.Action
- func Call3[T, U, V any](call func(T, U, V) error, t Binder[T], u Binder[U], v Binder[V]) cli.Action
- func Evaluator[T any](factory func(T) expr.Evaluator, t Binder[T]) expr.Evaluator
- func Evaluator2[T, U any](eval func(T, U) expr.Evaluator, t Binder[T], u Binder[U]) expr.Evaluator
- func Evaluator3[T, U, V any](eval func(T, U, V) expr.Evaluator, t Binder[T], u Binder[U], v Binder[V]) expr.Evaluator
- func Indirect[T, V any](name any, call func(T, V) error, valopt ...V) cli.Action
- func Redirect[V any](name any, valopt ...V) cli.Action
- type Binder
- func BigFloat(nameopt ...any) Binder[*big.Float]
- func BigInt(nameopt ...any) Binder[*big.Int]
- func Bool(nameopt ...any) Binder[bool]
- func Bytes(nameopt ...any) Binder[[]byte]
- func Context() Binder[*cli.Context]
- func ContextValue[T any](key any) Binder[T]
- func Duration(nameopt ...any) Binder[time.Duration]
- func Exact[T any](valopt ...T) Binder[T]
- func FileSet(nameopt ...any) Binder[*cli.FileSet]
- func Float32(nameopt ...any) Binder[float32]
- func Float64(nameopt ...any) Binder[float64]
- func For[T any](nameopt ...any) Binder[T]
- func FromContext[T any](fn func(context.Context) T) Binder[T]
- func IP(nameopt ...any) Binder[net.IP]
- func Int(nameopt ...any) Binder[int]
- func Int8(nameopt ...any) Binder[int8]
- func Int16(nameopt ...any) Binder[int16]
- func Int32(nameopt ...any) Binder[int32]
- func Int64(nameopt ...any) Binder[int64]
- func Interface(nameopt ...any) Binder[any]
- func List(nameopt ...any) Binder[[]string]
- func Map(nameopt ...any) Binder[map[string]string]
- func NameValue(nameopt ...any) Binder[*cli.NameValue]
- func NameValues(nameopt ...any) Binder[[]*cli.NameValue]
- func Regexp(nameopt ...any) Binder[*regexp.Regexp]
- func String(nameopt ...any) Binder[string]
- func URL(nameopt ...any) Binder[*url.URL]
- func Uint(nameopt ...any) Binder[uint]
- func Uint8(nameopt ...any) Binder[uint8]
- func Uint16(nameopt ...any) Binder[uint16]
- func Uint32(nameopt ...any) Binder[uint32]
- func Uint64(nameopt ...any) Binder[uint64]
- func Value[T any](nameopt ...any) Binder[T]
- type FileBinder
- type Func
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Action ¶
Action obtains an action invokes the function to derive another action whilst binding the parameters. If this is added to the Uses timing, it will actually be run in the Action timing and the binders can also provide initializers if they have the method Initializer() Action (as the binders in this package do).
func Action2 ¶
Action2 obtains an action invokes the function to derive another action whilst binding the parameters. If this is added to the Uses timing, it will actually be run in the Action timing and the binders can also provide initializers if they have the method Initializer() Action (as the binders in this package do).
func Action3 ¶
func Action3[T, U, V any](fn func(T, U, V) cli.Action, t Binder[T], u Binder[U], v Binder[V]) cli.Action
Action3 obtains an action invokes the function to derive another action whilst binding the parameters. If this is added to the Uses timing, it will actually be run in the Action timing and the binders can also provide initializers if they have the method Initializer() Action (as the binders in this package do).
func Call ¶
Call obtains an action invokes the function, binding the parameters. If this is added to the Uses timing, it will actually be run in the Action timing and the binders can also provide initializers if they have the method Initializer() Action (as the binders in this package do).
func Call2 ¶
Call2 obtains an action invokes the function, binding the parameters. If this is added to the Uses timing, it will actually be run in the Action timing and the binders can also provide initializers if they have the method Initializer() Action (as the binders in this package do).
func Call3 ¶
Call3 obtains an action invokes the function, binding the parameters. If this is added to the Uses timing, it will actually be run in the Action timing and the binders can also provide initializers if they have the method Initializer() Action (as the binders in this package do).
func Evaluator2 ¶
Evaluator2 produces an evaluator from the bound values.
func Evaluator3 ¶
func Evaluator3[T, U, V any](eval func(T, U, V) expr.Evaluator, t Binder[T], u Binder[U], v Binder[V]) expr.Evaluator
Evaluator3 produces an evaluator from the bound values.
func Indirect ¶
Indirect binds a value to the specified option indirectly. For example, it is common to define a FileSet arg and a Boolean flag that controls whether or not the file set is enumerated recursively. You can use Indirect to update the arg indirectly by naming it and the bind function:
&cli.Arg{
Name: "files",
Value: new(cli.FileSet),
}
&cli.Flag{
Name: "recursive",
HelpText: "Whether files is recursively searched",
Action: bind.Indirect("files", (*cli.FileSet).SetRecursive),
}
The name parameter specifies the name of the flag or arg that is affected. The bind function is the function to set the value, and valopt is optional, and if specified, indicates the value to set; otherwise, the value is read from the flag.
func Redirect ¶
Redirect binds a value to the specified option. A common use case for this action is to manually create aliases for other flags. For example, say you have a flag --proto= and a flag --tls1.2. You could use Redirect to support it.
&cli.Flag{
Name: "proto",
}
&cli.Flag{
Name: "tls1.2",
HelpText: "Use TLS 1.2 connections",
Action: bind.Redirect("proto", "tls1.2"),
}
&cli.Flag{
Name: "tls1.3",
HelpText: "Use TLS 1.3 connections",
Action: bind.Redirect("proto", "tls1.3"),
}
The name parameter specifies the name of the flag or arg that is affected. The valopt is optional, and if specified, indicates the value to set; otherwise, the value is read from the flag.
Types ¶
type Binder ¶
type Binder[T any] interface { // Bind obtains the value from the context Bind(context.Context) (T, error) }
Binder provides a strategy for obtaining a value from the context
func BigFloat ¶
BigFloat obtains a binder that obtains a value from the context. If the name is not specified, then either the current flag or arg is used or the corresponding argument by index. When present in the Uses pipeline, this also sets up the corresponding flag or arg with a reasonable default of the same type.
func BigInt ¶
BigInt obtains a binder that obtains a value from the context. If the name is not specified, then either the current flag or arg is used or the corresponding argument by index. When present in the Uses pipeline, this also sets up the corresponding flag or arg with a reasonable default of the same type.
func Bool ¶
Bool obtains a binder that obtains a value from the context. If the name is not specified, then either the current flag or arg is used or the corresponding argument by index. When present in the Uses pipeline, this also sets up the corresponding flag or arg with a reasonable default of the same type.
func Bytes ¶
Bytes obtains a binder that obtains a value from the context. If the name is not specified, then either the current flag or arg is used or the corresponding argument by index. When present in the Uses pipeline, this also sets up the corresponding flag or arg with a reasonable default of the same type.
func ContextValue ¶
ContextValue locates a value within the context.
func Duration ¶
Duration obtains a binder that obtains a value from the context. If the name is not specified, then either the current flag or arg is used or the corresponding argument by index. When present in the Uses pipeline, this also sets up the corresponding flag or arg with a reasonable default of the same type.
func Exact ¶
Exact takes either the exact value that is specified or will take the value from the flag or arg.
func FileSet ¶
FileSet obtains a binder that obtains a value from the context. If the name is not specified, then either the current flag or arg is used or the corresponding argument by index. When present in the Uses pipeline, this also sets up the corresponding flag or arg with a reasonable default of the same type.
func Float32 ¶
Float32 obtains a binder that obtains a value from the context. If the name is not specified, then either the current flag or arg is used or the corresponding argument by index. When present in the Uses pipeline, this also sets up the corresponding flag or arg with a reasonable default of the same type.
func Float64 ¶
Float64 obtains a binder that obtains a value from the context. If the name is not specified, then either the current flag or arg is used or the corresponding argument by index. When present in the Uses pipeline, this also sets up the corresponding flag or arg with a reasonable default of the same type.
func FromContext ¶
FromContext locates a value within the context. A common value for the argument is cli.FromContext to obtain the cli.Context pointer. Indeed, the function Context provides this behavior.
func IP ¶
IP obtains a binder that obtains a value from the context. If the name is not specified, then either the current flag or arg is used or the corresponding argument by index. When present in the Uses pipeline, this also sets up the corresponding flag or arg with a reasonable default of the same type.
func Int ¶
Int obtains a binder that obtains a value from the context. If the name is not specified, then either the current flag or arg is used or the corresponding argument by index. When present in the Uses pipeline, this also sets up the corresponding flag or arg with a reasonable default of the same type.
func Int8 ¶
Int8 obtains a binder that obtains a value from the context. If the name is not specified, then either the current flag or arg is used or the corresponding argument by index. When present in the Uses pipeline, this also sets up the corresponding flag or arg with a reasonable default of the same type.
func Int16 ¶
Int16 obtains a binder that obtains a value from the context. If the name is not specified, then either the current flag or arg is used or the corresponding argument by index. When present in the Uses pipeline, this also sets up the corresponding flag or arg with a reasonable default of the same type.
func Int32 ¶
Int32 obtains a binder that obtains a value from the context. If the name is not specified, then either the current flag or arg is used or the corresponding argument by index. When present in the Uses pipeline, this also sets up the corresponding flag or arg with a reasonable default of the same type.
func Int64 ¶
Int64 obtains a binder that obtains a value from the context. If the name is not specified, then either the current flag or arg is used or the corresponding argument by index. When present in the Uses pipeline, this also sets up the corresponding flag or arg with a reasonable default of the same type.
func Interface ¶
Interface obtains a binder that obtains a value from the context. If the name is not specified, then either the current flag or arg is used or the corresponding argument by index. When present in the Uses pipeline, this also sets up the corresponding flag or arg with a reasonable default of the same type.
func List ¶
List obtains a binder that obtains a value from the context. If the name is not specified, then either the current flag or arg is used or the corresponding argument by index. When present in the Uses pipeline, this also sets up the corresponding flag or arg with a reasonable default of the same type.
func Map ¶
Map obtains a binder that obtains a value from the context. If the name is not specified, then either the current flag or arg is used or the corresponding argument by index. When present in the Uses pipeline, this also sets up the corresponding flag or arg with a reasonable default of the same type.
func NameValue ¶
NameValue obtains a binder that obtains a value from the context. If the name is not specified, then either the current flag or arg is used or the corresponding argument by index. When present in the Uses pipeline, this also sets up the corresponding flag or arg with a reasonable default of the same type.
func NameValues ¶
NameValues obtains a binder that obtains a value from the context. If the name is not specified, then either the current flag or arg is used or the corresponding argument by index. When present in the Uses pipeline, this also sets up the corresponding flag or arg with a reasonable default of the same type.
func Regexp ¶
Regexp obtains a binder that obtains a value from the context. If the name is not specified, then either the current flag or arg is used or the corresponding argument by index. When present in the Uses pipeline, this also sets up the corresponding flag or arg with a reasonable default of the same type.
func String ¶
String obtains a binder that obtains a value from the context. If the name is not specified, then either the current flag or arg is used or the corresponding argument by index. When present in the Uses pipeline, this also sets up the corresponding flag or arg with a reasonable default of the same type.
func URL ¶
URL obtains a binder that obtains a value from the context. If the name is not specified, then either the current flag or arg is used or the corresponding argument by index. When present in the Uses pipeline, this also sets up the corresponding flag or arg with a reasonable default of the same type.
func Uint ¶
Uint obtains a binder that obtains a value from the context. If the name is not specified, then either the current flag or arg is used or the corresponding argument by index. When present in the Uses pipeline, this also sets up the corresponding flag or arg with a reasonable default of the same type.
func Uint8 ¶
Uint8 obtains a binder that obtains a value from the context. If the name is not specified, then either the current flag or arg is used or the corresponding argument by index. When present in the Uses pipeline, this also sets up the corresponding flag or arg with a reasonable default of the same type.
func Uint16 ¶
Uint16 obtains a binder that obtains a value from the context. If the name is not specified, then either the current flag or arg is used or the corresponding argument by index. When present in the Uses pipeline, this also sets up the corresponding flag or arg with a reasonable default of the same type.
func Uint32 ¶
Uint32 obtains a binder that obtains a value from the context. If the name is not specified, then either the current flag or arg is used or the corresponding argument by index. When present in the Uses pipeline, this also sets up the corresponding flag or arg with a reasonable default of the same type.
func Uint64 ¶
Uint64 obtains a binder that obtains a value from the context. If the name is not specified, then either the current flag or arg is used or the corresponding argument by index. When present in the Uses pipeline, this also sets up the corresponding flag or arg with a reasonable default of the same type.
func Value ¶
Value obtains a binder that obtains a value from the context. If the name is not specified, then either the current flag or arg is used or the corresponding argument by index. When present in the Uses pipeline, this also sets up the corresponding flag or arg with a reasonable default of the same type.
type FileBinder ¶
type FileBinder struct {
// contains filtered or unexported fields
}
FileBinder provides a binder for [cli.File]
func File ¶
func File(nameopt ...any) *FileBinder
File obtains a binder that obtains a value from the context. If the name is not specified, then either the current flag or arg is used or the corresponding argument by index. When present in the Uses pipeline, this also sets up the corresponding flag or arg with a reasonable default of the same type.
func (*FileBinder) Base ¶
func (f *FileBinder) Base() Binder[string]
Base obtains the name for the file
func (*FileBinder) Dir ¶
func (f *FileBinder) Dir() Binder[string]
Dir obtains the name for the file
func (*FileBinder) Exists ¶
func (f *FileBinder) Exists() Binder[bool]
Exists obtains the name for the file
func (*FileBinder) Ext ¶
func (f *FileBinder) Ext() Binder[string]
Ext obtains the name for the file
func (*FileBinder) Name ¶
func (f *FileBinder) Name() Binder[string]
Name obtains the name for the file