bind

package
v0.18.0 Latest Latest
Warning

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

Go to latest
Published: Jul 12, 2026 License: BSD-3-Clause Imports: 11 Imported by: 7

Documentation

Overview

Package bind provides functions for creating late-bound actions. It's common to produce actions that delegate to other actions, 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"))

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Action

func Action[T any, Action cli.Action](fn func(T) Action, t Binder[T]) cli.Action

Action obtains an action that 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 that 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 that 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

func After[T any, Action cli.Action](fn func(T) Action, t Binder[T]) cli.Action

After obtains an action that 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

func After2[T, U any, Action cli.Action](fn func(T, U) Action, t Binder[T], u Binder[U]) cli.Action

After2 obtains an action that 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 that 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

func AfterCall[T any](call func(T) error, t Binder[T]) cli.Action

AfterCall obtains an action that 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

func AfterCall2[T, U any](call func(T, U) error, t Binder[T], u Binder[U]) cli.Action

AfterCall2 obtains an action that 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 that 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

func Before[T any, Action cli.Action](fn func(T) Action, t Binder[T]) cli.Action

Before obtains an action that 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 that 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 that 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

func BeforeCall[T any](call func(T) error, t Binder[T]) cli.Action

BeforeCall obtains an action that 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

func BeforeCall2[T, U any](call func(T, U) error, t Binder[T], u Binder[U]) cli.Action

BeforeCall2 obtains an action that 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 that 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

func Call[T any](call func(T) error, binderopt ...Binder[T]) cli.Action

Call obtains an action that invokes the function, binding the parameters. The binder is optional. When not present, it defaults to the binder representing the value of the current flag or arg. 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

func Call2[T, U any](call func(T, U) error, t Binder[T], u Binder[U]) cli.Action

Call2 obtains an action that 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

func Call3[T, U, V any](call func(T, U, V) error, t Binder[T], u Binder[U], v Binder[V]) cli.Action

Call3 obtains an action that 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

func Indirect[T, V any](name any, call func(T, V) error, valopt ...V) cli.Action

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

func Redirect[V any](name any, valopt ...V) cli.Action

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

func SetPointer[V any](v *V, binder Binder[V]) cli.Action

SetPointer creates an action that stores the value obtained from binder into v.

func Setter added in v0.15.0

func Setter[T, V any](fn func(T, V), target T, binder Binder[V]) cli.Action

Setter provides a binding over the typical setter accessor for a value, func(*T, V) also commonly in the form func(*T) SetMethod (V) when it is a method with a selector.

Types

type ActionBinder added in v0.12.0

type ActionBinder[T any] interface {
	cli.Action
	Binder[T]
}

ActionBinder provides a binder which can also be used as an action

func NewActionBinder added in v0.12.0

func NewActionBinder[T any](action cli.Action, binder Binder[T]) ActionBinder[T]

NewActionBinder provides a binder 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 binder depends on. The action binder is usually placed both into the Uses pipeline and used in context of the bind where its value is calculated.

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

func BigFloat(nameopt ...any) Binder[*big.Float]

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

func BigInt(nameopt ...any) Binder[*big.Int]

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 Bytes

func Bytes(nameopt ...any) Binder[[]byte]

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 Context

func Context() Binder[*cli.Context]

Context binds the context as a parameter.

func ContextValue

func ContextValue[T any](key any) Binder[T]

ContextValue returns a binder that retrieves the value stored under key in the standard context.Context.

func Duration

func Duration(nameopt ...any) Binder[time.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 Elem added in v0.12.0

func Elem[T any, TP *T](src Binder[TP]) Binder[T]

Elem dereferences a pointer binder, returning a binder for the element type.

func Exact

func Exact[T any](valopt ...T) Binder[T]

Exact takes either the exact value that is specified or will take the value from the flag or arg.

func FS added in v0.10.0

func FS() Binder[cli.FS]

FS binds the file system as a parameter.

func FileSet

func FileSet(nameopt ...any) Binder[*cli.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

func Float32(nameopt ...any) Binder[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

func Float64(nameopt ...any) Binder[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

func FromContext[T any](fn func(context.Context) T) Binder[T]

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

func IP(nameopt ...any) Binder[net.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

func Int(nameopt ...any) Binder[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

func Int8(nameopt ...any) Binder[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

func Int16(nameopt ...any) Binder[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

func Int32(nameopt ...any) Binder[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

func Int64(nameopt ...any) Binder[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

func Interface(nameopt ...any) Binder[any]

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

func List(nameopt ...any) Binder[[]string]

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

func Map(nameopt ...any) Binder[map[string]string]

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

func NameValues(nameopt ...any) Binder[[]*cli.NameValue]

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 Occurrences added in v0.14.1

func Occurrences[T any](name any, value T, valuesopt ...T) Binder[T]

Occurrences provides a binder that maps the occurrence count of the named flag to a value. The first argument after name is returned when the flag does not appear, and valuesopt specifies each of the values for the corresponding number of occurrences. If the flag appears more times than there are values, the last value is returned. As a special case, if the value is set to zero (and no other values for valuesopt and the type is strictly int), it actually means the count of occurrences will be used.

func Pointer added in v0.12.0

func Pointer[T any](src Binder[T]) Binder[*T]

Pointer wraps a binder to return a pointer to the bound value.

func Regexp

func Regexp(nameopt ...any) Binder[*regexp.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 Seen added in v0.16.0

func Seen(nameopt ...any) Binder[bool]

Seen provides a binder which sets the value according to whether the value has been seen. Seen provides a binder which sets

func Seq added in v0.12.0

func Seq[T, U any](binder Binder[T], then func(T) (U, error)) Binder[U]

Seq applies an additional function to a binding. As a special case, if the original binder implements additional conventions, those are propagated into the result.

func SeqContext added in v0.12.0

func SeqContext[T, U any](binder Binder[T], then func(context.Context, T) (U, error)) Binder[U]

SeqContext applies an additional function with a context to a binding. As a special case, if the original binder implements additional conventions, those are propagated into the result.

func Stdin added in v0.11.0

func Stdin() Binder[io.Reader]

Stdin binds stdin.

func Stdout added in v0.11.0

func Stdout() Binder[io.Writer]

Stdout binds stdout.

func String

func String(nameopt ...any) Binder[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

func URL(nameopt ...any) Binder[*url.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

func Uint(nameopt ...any) Binder[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

func Uint8(nameopt ...any) Binder[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

func Uint16(nameopt ...any) Binder[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

func Uint32(nameopt ...any) Binder[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

func Uint64(nameopt ...any) Binder[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

func Value[T any](nameopt ...any) Binder[T]

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 BoolBinder added in v0.11.0

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

BoolBinder is a Binder for bool values. In addition to the standard Binder interface it exposes derived bindings such as BoolBinder.Negated.

func Bool

func Bool(nameopt ...any) *BoolBinder

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 (*BoolBinder) Bind added in v0.11.0

func (b *BoolBinder) Bind(c context.Context) (bool, error)

func (*BoolBinder) Negated added in v0.11.0

func (b *BoolBinder) Negated() Binder[bool]

Negated obtains the logical NOT value for a Boolean

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 base name of the file

func (*FileBinder) Bind

func (f *FileBinder) Bind(c context.Context) (*cli.File, error)

func (*FileBinder) CreateWriter added in v0.14.1

func (f *FileBinder) CreateWriter() Binder[io.Writer]

CreateWriter obtains a writer for the file

func (*FileBinder) Dir

func (f *FileBinder) Dir() Binder[string]

Dir obtains the directory component of the file path

func (*FileBinder) Exists

func (f *FileBinder) Exists() Binder[bool]

Exists reports whether the file exists

func (*FileBinder) Ext

func (f *FileBinder) Ext() Binder[string]

Ext obtains the file extension

func (*FileBinder) Name

func (f *FileBinder) Name() Binder[string]

Name obtains the name for the file

func (*FileBinder) OpenReader added in v0.14.1

func (f *FileBinder) OpenReader() Binder[io.Reader]

OpenReader obtains a reader for the file

type Func

type Func[T any] func(c *cli.Context) (T, error)

Func provides a function that binds

func (Func[T]) Bind

func (f Func[T]) Bind(c context.Context) (T, error)

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) Bind added in v0.16.0

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

Directories

Path Synopsis
Code generated by counterfeiter.
Code generated by counterfeiter.

Jump to

Keyboard shortcuts

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