Documentation
¶
Overview ¶
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.
Binders ¶
For binders built-in to this package as well as any that implement the interface interface { SetName(any) }, the name of the binder can be omitted and is implicitly set. The name will be the index of the binder in the function call used within a command or the empty string if used within an arg or flag. For example, the following are equivalent if they are used within the Uses pipeline of a command that defines two args "first" and "second", because of this built-in behavior:
bind.Call2(myFunction, bind.String(), bind.Int()) // names omitted
bind.Call2(myFunction, bind.String(0), bind.Int(1))
bind.Call2(myFunction, bind.String("first"), bind.Int("second"))
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.
Index ¶
- func Action[T any, Action cli.Action](fn func(T) Action, t Binder[T]) cli.Action
- func Action2[T, U any, Action cli.Action](fn func(T, U) Action, t Binder[T], u Binder[U]) cli.Action
- func Action3[T, U, V any, Action cli.Action](fn func(T, U, V) Action, t Binder[T], u Binder[U], v Binder[V]) cli.Action
- func After[T any, Action cli.Action](fn func(T) Action, t Binder[T]) cli.Action
- func After2[T, U any, Action cli.Action](fn func(T, U) Action, t Binder[T], u Binder[U]) cli.Action
- func After3[T, U, V any, Action cli.Action](fn func(T, U, V) Action, t Binder[T], u Binder[U], v Binder[V]) cli.Action
- func AfterCall[T any](call func(T) error, t Binder[T]) cli.Action
- func AfterCall2[T, U any](call func(T, U) error, t Binder[T], u Binder[U]) cli.Action
- func AfterCall3[T, U, V any](call func(T, U, V) error, t Binder[T], u Binder[U], v Binder[V]) cli.Action
- func Before[T any, Action cli.Action](fn func(T) Action, t Binder[T]) cli.Action
- func Before2[T, U any, Action cli.Action](fn func(T, U) Action, t Binder[T], u Binder[U]) cli.Action
- func Before3[T, U, V any, Action cli.Action](fn func(T, U, V) Action, t Binder[T], u Binder[U], v Binder[V]) cli.Action
- func BeforeCall[T any](call func(T) error, t Binder[T]) cli.Action
- func BeforeCall2[T, U any](call func(T, U) error, t Binder[T], u Binder[U]) cli.Action
- func BeforeCall3[T, U, V any](call func(T, U, V) error, 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 Indirect[T, V any](name any, call func(T, V) error, valopt ...V) cli.Action
- func Initializers(binders ...any) cli.Action
- func Redirect[V any](name any, valopt ...V) cli.Action
- func SetPointer[V any](v *V, binder Binder[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 FS() Binder[cli.FS]
- func FileSet(nameopt ...any) Binder[*cli.FileSet]
- func Float32(nameopt ...any) Binder[float32]
- func Float64(nameopt ...any) Binder[float64]
- 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 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
- type NameValueBinder
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 ¶
func Action2[T, U any, Action cli.Action](fn func(T, U) Action, t Binder[T], u Binder[U]) cli.Action
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, Action cli.Action](fn func(T, U, V) 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 After ¶ added in v0.10.0
After 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 After timing and the binders can also provide initializers if they have the method Initializer() Action (as the binders in this package do).
func After2 ¶ added in v0.10.0
After2 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 After timing and the binders can also provide initializers if they have the method Initializer() Action (as the binders in this package do).
func After3 ¶ added in v0.10.0
func After3[T, U, V any, Action cli.Action](fn func(T, U, V) Action, t Binder[T], u Binder[U], v Binder[V]) cli.Action
After3 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 After timing and the binders can also provide initializers if they have the method Initializer() Action (as the binders in this package do).
func AfterCall ¶ added in v0.10.0
AfterCall obtains an action invokes the function, binding the parameters. If this is added to the Uses timing, it will actually be run in the After timing and the binders can also provide initializers if they have the method Initializer() Action (as the binders in this package do).
func AfterCall2 ¶ added in v0.10.0
AfterCall2 obtains an action invokes the function, binding the parameters. If this is added to the Uses timing, it will actually be run in the After timing and the binders can also provide initializers if they have the method Initializer() Action (as the binders in this package do).
func AfterCall3 ¶ added in v0.10.0
func AfterCall3[T, U, V any](call func(T, U, V) error, t Binder[T], u Binder[U], v Binder[V]) cli.Action
AfterCall3 obtains an action invokes the function, binding the parameters. If this is added to the Uses timing, it will actually be run in the After timing and the binders can also provide initializers if they have the method Initializer() Action (as the binders in this package do).
func Before ¶ added in v0.10.0
Before 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 Before timing and the binders can also provide initializers if they have the method Initializer() Action (as the binders in this package do).
func Before2 ¶ added in v0.10.0
func Before2[T, U any, Action cli.Action](fn func(T, U) Action, t Binder[T], u Binder[U]) cli.Action
Before2 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 Before timing and the binders can also provide initializers if they have the method Initializer() Action (as the binders in this package do).
func Before3 ¶ added in v0.10.0
func Before3[T, U, V any, Action cli.Action](fn func(T, U, V) Action, t Binder[T], u Binder[U], v Binder[V]) cli.Action
Before3 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 Before timing and the binders can also provide initializers if they have the method Initializer() Action (as the binders in this package do).
func BeforeCall ¶ added in v0.10.0
BeforeCall obtains an action invokes the function, binding the parameters. If this is added to the Uses timing, it will actually be run in the Before timing and the binders can also provide initializers if they have the method Initializer() Action (as the binders in this package do).
func BeforeCall2 ¶ added in v0.10.0
BeforeCall2 obtains an action invokes the function, binding the parameters. If this is added to the Uses timing, it will actually be run in the Before timing and the binders can also provide initializers if they have the method Initializer() Action (as the binders in this package do).
func BeforeCall3 ¶ added in v0.10.0
func BeforeCall3[T, U, V any](call func(T, U, V) error, t Binder[T], u Binder[U], v Binder[V]) cli.Action
BeforeCall3 obtains an action invokes the function, binding the parameters. If this is added to the Uses timing, it will actually be run in the Before 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 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 Initializers ¶ added in v0.10.0
func Initializers(binders ...any) cli.Action
Initializers obtains the initializers for a sequence of binders. For a binder that has a method Initializer() Action, such method will be called to retrieve the initializer. For a binder that has a method SetName(any), the method will be called to set the implicit name that can be used when a binder is used within a function. Refer to the package overview for information about implicit naming.
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.
func SetPointer ¶ added in v0.9.3
SetPointer sets a pointer as the binding action
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 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
type NameValueBinder ¶ added in v0.10.0
type NameValueBinder struct {
// contains filtered or unexported fields
}
NameValueBinder provides a binder for cli.NameValue
func NameValue ¶
func NameValue(nameopt ...any) *NameValueBinder
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 (*NameValueBinder) Name ¶ added in v0.10.0
func (f *NameValueBinder) Name() Binder[string]
Name provides a delegate binder which obtains the name part
func (*NameValueBinder) Value ¶ added in v0.10.0
func (f *NameValueBinder) Value() Binder[string]
Value provides a delegate binder which obtains the value part