Documentation
¶
Overview ¶
Package clibind provides a tag/reflect-based mapper between urfave/cli/v3 flags and a struct. Usage:
type Config struct {
Name string `cli:"name,n,A" cliDefault:"guest" cliUsage:"User name"`
Count int `cli:"count,c" cliDefault:"3" cliUsage:"How many items"`
Delay time.Duration `cli:"delay,d" cliDefault:"250ms" cliUsage:"Wait duration"`
When time.Time `cli:"when,w" cliDefault:"2025-01-02T15:04:05Z" cliUsage:"When (RFC3339)"`
IDs []uuid.UUID `cli:"ids" cliDefault:"b33a...-0001, b33a...-0002" cliUsage:"Comma-separated UUIDs"`
Tags []string `cli:"tags" cliDefault:"alpha,beta" cliUsage:"Comma-separated"`
}
app := &cli.App{
Flags: clibind.FlagsFromStruct(Config{}),
Action: func(ctx context.Context, c *cli.Command) error {
var cfg Config
if err := clibind.Bind(c, &cfg); err != nil { return err }
// use cfg...
return nil
},
}
Index ¶
- func Bind(ctx *cli.Command, dest any) error
- func CommandWithBinding[T any](base *cli.Command, name string, fn func(ctx context.Context, t T) error) *cli.Command
- func FlagsFromStruct(v any) []cli.Flag
- func WithBinding[T any](fn func(ctx context.Context, t T) error) func(ctx context.Context, c *cli.Command) (err error)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Bind ¶
Bind populates struct fields from CLI flag values defined in the given command context. It expects dest to be a pointer to a struct whose fields are tagged or named to correspond to the command’s flags.
dest must be a non-nil pointer to a struct, otherwise Bind returns an error.
func CommandWithBinding ¶
func CommandWithBinding[T any]( base *cli.Command, name string, fn func(ctx context.Context, t T) error, ) *cli.Command
CommandWithBinding creates a new CLI command that automatically binds command-line flags into a typed configuration struct before executing the provided handler function.
It combines command construction and type-safe binding in one step.
If base is nil, a new *cli.Command is created. The resulting command’s Action is set using WithBinding(fn), and its Name is set to the provided name.
Example:
func runServer(ctx context.Context, cfg ServerConfig) error { ... }
root := &cli.Command{Name: "root"}
server := clibind.CommandWithBinding(root, "serve", runServer)
When executed, the "serve" subcommand will parse CLI flags, populate a ServerConfig instance via Bind, and then invoke runServer with the bound configuration.
func FlagsFromStruct ¶
FlagsFromStruct inspects exported fields with `cli` and other tags and generates cli.Flag definitions. It is safe to pass either a struct or a pointer to a struct. Unexported fields are ignored.
func WithBinding ¶
func WithBinding[T any]( fn func(ctx context.Context, t T) error, ) func(ctx context.Context, c *cli.Command) (err error)
WithBinding wraps a typed handler function so that it automatically binds CLI flag values to a struct before invoking the handler.
The generic parameter T defines the type of the struct into which CLI flags will be bound. The provided function fn receives a populated instance of T.
This allows you to write clean, strongly typed handlers without manually parsing or binding CLI flags.
Types ¶
This section is empty.