Documentation
¶
Overview ¶
envflags provides a way to set flags from environment variables, working in conjunction with the flag package; it adds the option to look for an environment variable in between the flag and its defsult value, and also adds environment variable names to the usage string.
The precedence order for values is explicit flag -> environment variable -> default.
Index ¶
- Variables
- type Value
- func NewBool(env string, defaultValue bool) *Value[bool]
- func NewDuration(env string, defaultValue time.Duration) *Value[time.Duration]
- func NewEnvFlagValue[T any](envName string, defaultValue T, converter func(string) (T, error)) *Value[T]
- func NewInt(env string, defaultValue int) *Value[int]
- func NewLogLevel(env string, defaultValue slog.Level) *Value[slog.Level]
- func NewString(env, defaultValue string) *Value[string]
- func NewText[S encoding.TextUnmarshaler](env string, defaultValue S) *Value[S]
- func NewUint64(env string, defaultValue uint64) *Value[uint64]
Constants ¶
This section is empty.
Variables ¶
var ( EnvPrefix = "" // Will be added to usage strings for flags that have an // environment variable. The format string substitutes the // environment variable name. EnvUsageTemplate = "\nEnvironment: %s" )
Functions ¶
This section is empty.
Types ¶
type Value ¶
type Value[T any] struct { // contains filtered or unexported fields }
func NewEnvFlagValue ¶
func NewEnvFlagValue[T any]( envName string, defaultValue T, converter func(string) (T, error), ) *Value[T]
NewEnvFlagValue creates a new Value[T] with the given environment variable name. Pass "" to the envName to disable environment variables for this flag. The defaultValue is used if the environment variable or explicit flag are not set or cannot be converted.
func (*Value[T]) AddTo ¶
AddTo adds the flag to the given flag.FlagSet with the given name. usage is appended with environment flag info if applicable.