Documentation
¶
Index ¶
- func DefaultValues(ctx context.Context, data interface{}) (map[string]interface{}, error)
- func Fill(ctx context.Context, data interface{}, values map[string]interface{}) error
- func Parse(ctx context.Context, data interface{}) error
- func ParseAndPrint(ctx context.Context, data interface{}) error
- func ParseArgs(ctx context.Context, data interface{}, args []string) error
- func ParseEnv(ctx context.Context, data interface{}, environ []string) error
- func Print(ctx context.Context, data interface{}) error
- func ValidateRequired(ctx context.Context, data interface{}) error
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func DefaultValues ¶
DefaultValues returns all default values of the given struct.
func Parse ¶
Parse combines all functionality. It parses command-line arguments and environment variables into a struct using struct tags, then validates required fields are set.
Supported Types:
- Basic types: string, bool, int, int32, int64, uint, uint64, float64
- Pointer types: *float64 (optional values, nil if not provided)
- Standard library time types:
- time.Time and *time.Time: RFC3339 format (e.g., "2006-01-02T15:04:05Z")
- time.Duration and *time.Duration: Extended format supporting days (e.g., "1d2h30m", "7d")
- github.com/bborbe/time types:
- libtime.Duration and *libtime.Duration: Extended duration with weeks (e.g., "2w", "1w3d")
- libtime.DateTime and *libtime.DateTime: Timestamp with timezone
- libtime.Date and *libtime.Date: Date only (e.g., "2006-01-02")
- libtime.UnixTime and *libtime.UnixTime: Unix timestamp (seconds since epoch)
Pointer types (*Type) are optional and will be nil if not provided or if provided as empty string. Non-pointer types will use zero values if not provided.
Struct Tags:
- arg: Command-line argument name (required to parse field)
- env: Environment variable name (optional)
- default: Default value if not provided (optional)
- required: Mark field as required (optional)
- display: Control how value is displayed - "length" shows only length for sensitive data (optional)
- usage: Help text for the argument (optional)
Example:
type Config struct {
Host string `arg:"host" env:"HOST" default:"localhost" usage:"Server hostname"`
Port int `arg:"port" env:"PORT" default:"8080" required:"true"`
Timeout time.Duration `arg:"timeout" default:"30s" usage:"Request timeout"`
StartAt *time.Time `arg:"start" usage:"Optional start time"`
Password string `arg:"password" env:"PASSWORD" display:"length" usage:"API password"`
}
Precedence: Command-line arguments override environment variables, which override defaults.
func ParseAndPrint ¶ added in v2.3.0
func ParseArgs ¶
ParseArgs parses command-line arguments into the given struct using arg struct tags. See Parse() documentation for supported types and struct tag options.
Parameters:
- ctx: Context for error handling
- data: Pointer to struct with arg tags
- args: Command-line arguments (typically os.Args[1:])
Returns error if parsing fails or if default values are malformed.
func ParseEnv ¶
ParseEnv parses environment variables into the given struct using env struct tags. See Parse() documentation for supported types and struct tag options.
Parameters:
- ctx: Context for error handling
- data: Pointer to struct with env tags
- environ: Environment variables (typically os.Environ())
Returns error if parsing fails.
func Print ¶
Print all configured arguments. Set display:"hidden" to hide or display:"length" to only print the arguments length.
func ValidateRequired ¶
ValidateRequired fields are set and returns an error if not.
Types ¶
This section is empty.