Skip to content

Plugin System

A plugin bundles custom flag type codecs and named validators into one registration unit. It is the recommended way to share domain-specific flag types across CLIs and teams.

type Plugin interface {
Name() string
Register(r v5.PluginRegistrar) error
}

PluginRegistrar exposes three methods:

Method Purpose
TypeCodec(typ reflect.Type, codec TypeCodec) register a full TypeCodec implementation
TypeCodecFunc(typ reflect.Type, codec TypeCodecFunc) register the functional adapter
Validator(name string, fn FlagValidator) register a named validator for validate:"name" tags
if err := v5.RegisterPlugin(DatabasePlugin{}); err != nil {
return err
}
cli, err := v5.NewCLI[AppConfig]("app", "My app", AppConfig{},
v5.WithPlugin(DatabasePlugin{}),
)

Both entry points write the global type and validator registries. RegisterPlugin applies immediately; WithPlugin applies during NewCLI, before flags are registered, and a failed registration becomes the error returned by NewCLI. Register before constructing any CLI whose flags use the plugin’s types.

package main
import (
"context"
"fmt"
"os"
"reflect"
"strings"
"github.com/larsartmann/cmdguard/v5/pkg/cmdguard/v5"
"github.com/spf13/pflag"
)
type DSN string
type DatabasePlugin struct{}
func (DatabasePlugin) Name() string { return "database-types" }
func (DatabasePlugin) Register(r v5.PluginRegistrar) error {
r.TypeCodecFunc(reflect.TypeFor[DSN](), v5.TypeCodecFunc{
RegisterFunc: func(flags *pflag.FlagSet, tag v5.FlagTag) error {
flags.StringP(tag.Name, tag.Short, tag.Default, tag.Help)
return nil
},
ParseFunc: func(value string, _ v5.FlagTag) (any, error) {
return DSN(value), nil
},
DefaultFunc: func(tag v5.FlagTag) any {
return DSN(tag.Default)
},
})
r.Validator("dsn-scheme", func(value string) error {
if !strings.Contains(value, "://") {
return fmt.Errorf("DSN %q must include a scheme", value)
}
return nil
})
return nil
}
type AppConfig struct {
Database DSN `flag:"database" default:"postgres://localhost/app" validate:"dsn-scheme" help:"Database DSN"`
}
func main() {
if err := v5.RegisterPlugin(DatabasePlugin{}); err != nil {
fmt.Fprintf(os.Stderr, "registering plugin: %v\n", err)
os.Exit(1)
}
cli, err := v5.NewCLI[AppConfig]("app", "Example app", AppConfig{})
if err != nil {
fmt.Fprintf(os.Stderr, "creating CLI: %v\n", err)
os.Exit(1)
}
connect, err := v5.NewCommand("connect", v5.NoFlags{},
func(_ context.Context, cfg *AppConfig, _ v5.NoFlags) error {
fmt.Printf("connecting to %s\n", cfg.Database)
return nil
},
v5.WithShort("Connect to the database"),
)
b := v5.NewBuilder(cli)
v5.Try(b, connect, err)
if buildErr := b.Build(); buildErr != nil {
fmt.Fprintf(os.Stderr, "registering commands: %v\n", buildErr)
os.Exit(1)
}
cli.ExecuteAndExit(context.Background())
}

The DSN field now parses into the struct, and the dsn-scheme validator runs for both the declared default and an explicit --database value.

TypeCodec is the interface behind every flag type. It has Register(flags *pflag.FlagSet, tag FlagTag) error, Parse(value string, tag FlagTag) (any, error), and Default(tag FlagTag) any. TypeCodecFunc adapts three functions through its RegisterFunc, ParseFunc, and DefaultFunc fields.

Register a codec directly when no plugin wrapper is needed with v5.RegisterTypeCodec(reflect.TypeFor[DSN](), codec), which wraps ErrServiceRegistration when the type or codec is nil.

A validator is any v5.FlagValidator, that is func(value string) error. Register one globally with v5.RegisterValidator(name, fn), then reference it from a tag such as validate:"dsn-scheme". Unknown names fail with ErrUnknownValidator.

Older name v5 name
TypeHandler TypeCodec
TypeHandlerFunc TypeCodecFunc
RegisterTypeHandler RegisterTypeCodec
RegisterTypeHandlers and RegisterValidators Plugin.Register(PluginRegistrar)

The registry-level plugin API is gone; plugins register through PluginRegistrar, and per-CLI wiring goes through WithPlugin.