full_example_cli

package
v0.0.0-...-d42e3b3 Latest Latest
Warning

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

Go to latest
Published: Jul 6, 2026 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewRootC

func NewRootC(exitOnDebug bool) (*cobra.Command, error)

Types

type DatabaseConfig

type DatabaseConfig struct {
	URL      string `flag:"db-url" flagdescr:"Database connection URL"`
	MaxConns int    `flagdescr:"Max database connections" default:"10" flagenv:"true"`
}

type Deeply

type Deeply struct {
	Setting string `flag:"deep-setting" default:"default-deep-setting"`
	Deeper  EvenDeeper
}

type Environment

type Environment string
const (
	EnvDevelopment Environment = "dev"
	EnvStaging     Environment = "staging"
	EnvProduction  Environment = "prod"
)

type EvenDeeper

type EvenDeeper struct {
	Setting   string `flag:"deeper-setting" default:"default-deeper-setting"`
	NoDefault string
}

type LogsOptions

type LogsOptions struct {
	flagkit.Follow
	flagkit.Output
	flagkit.Timeout
	flagkit.Quiet
	Service string `flag:"service" flagshort:"s" flagdescr:"Service name to show logs for" flagrequired:"true"`
}

LogsOptions demonstrates flagkit composition with multiple types. Combines Follow, Output, Timeout, and Quiet with an app-specific Service flag.

func (*LogsOptions) Attach

func (o *LogsOptions) Attach(c *cobra.Command) error

type PresetDemoOptions

type PresetDemoOptions struct {
	Role  string `flag:"role" flagpreset:"as-admin=admin;as-guest=guest;as-super=superadmin" validate:"required,oneof=admin guest"`
	Label string `flag:"label" flagpreset:"auto-label=  john doe  " mod:"trim,title" validate:"required,min=3,max=32"`
}

PresetDemoOptions demonstrates flagpreset values flowing through transform and validation logic.

func (*PresetDemoOptions) Transform

func (o *PresetDemoOptions) Transform(ctx context.Context) error

func (*PresetDemoOptions) Validate

func (o *PresetDemoOptions) Validate(ctx context.Context) []error

type ServerOptions

type ServerOptions struct {
	// Basic flags
	Host string `flag:"host" flagdescr:"Server host" default:"localhost"`
	Port int    `flagshort:"p" flagdescr:"Server port" flagrequired:"true" flagenv:"true"`

	// Environment variable binding
	APIKey string `flagenv:"true" flagdescr:"API authentication key"`

	// Env-only field: settable only via environment variable, not CLI flag
	SecretKey string `flagenv:"only" flag:"secret-key" flagdescr:"Secret signing key (env only)"`

	// Same in-memory type (bytes), different textual contracts at the CLI boundary.
	TokenHex    structcli.Hex    `flag:"token-hex" flaggroup:"Security" flagdescr:"Token bytes encoded as hex" flagenv:"true" default:"68656c6c6f"`
	TokenBase64 structcli.Base64 `flag:"token-base64" flaggroup:"Security" flagdescr:"Token bytes encoded as base64" flagenv:"true" default:"aGVsbG8="`

	// Network contracts using net families.
	BindIP        net.IP     `flag:"bind-ip" flaggroup:"Network" flagdescr:"Bind interface IP" flagenv:"true" default:"127.0.0.1"`
	BindMask      net.IPMask `flag:"bind-mask" flaggroup:"Network" flagdescr:"Bind interface mask" flagenv:"true" default:"ffffff00"`
	AdvertiseCIDR net.IPNet  `flag:"advertise-cidr" flaggroup:"Network" flagdescr:"Advertised service subnet (CIDR)" flagenv:"true" default:"127.0.0.0/24"`
	TrustedPeers  []net.IP   `` /* 132-byte string literal not displayed */

	// Flag grouping for organized help
	LogLevel zapcore.Level `flag:"log-level" flaggroup:"Logging" flagdescr:"Set log level"`
	LogFile  string        `flag:"log-file" flaggroup:"Logging" flagdescr:"Log file path" flagenv:"true"`

	// Nested structs for organization
	Database DatabaseConfig `flaggroup:"Database"`

	// Custom type
	TargetEnv Environment `flag:"target-env" flagdescr:"Set the target environment" default:"dev"`

	Deep Deeply
}

func (*ServerOptions) Attach

func (o *ServerOptions) Attach(c *cobra.Command) error

Attach defines flags without registering for auto-unmarshal. ServerOptions uses Attach (not Bind) because srv has subcommands and these options should only be unmarshalled when srv itself runs.

type UserConfig

type UserConfig struct {
	Email string `flag:"email" flagdescr:"User email" validate:"email"`
	Age   int    `flag:"age" flagdescr:"User age" validate:"min=18,max=120"`
	Name  string `flag:"name" flagdescr:"User name" mod:"trim,title"`
}

func (*UserConfig) Transform

func (o *UserConfig) Transform(ctx context.Context) error

Transform is called automatically during unmarshal (before validation).

func (*UserConfig) Validate

func (o *UserConfig) Validate(ctx context.Context) []error

Validate is called automatically during unmarshal.

type UtilityFlags

type UtilityFlags struct {
	Verbose int  `flagtype:"count" flagshort:"v" flaggroup:"Utility"`
	DryRun  bool `flag:"dry" flaggroup:"Utility" flagenv:"true"`
}

func (*UtilityFlags) Context

func (f *UtilityFlags) Context(ctx context.Context) context.Context

Context propagates UtilityFlags into the command context during unmarshal.

func (*UtilityFlags) FromContext

func (f *UtilityFlags) FromContext(ctx context.Context) error

Jump to

Keyboard shortcuts

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