Documentation
¶
Overview ¶
Package mflag provides integrated configuration management for Go applications, merging settings from default values, YAML files, and command-line flags.
Example ¶
defer func(oldArgs []string) {
os.Args = oldArgs
Reset()
}(os.Args)
Reset()
os.Args = []string{"cmd", "--host=flag.host", "--debug=false"}
SetDefault("host", "default.host")
SetDefault("port", 8080)
SetDefault("debug", true)
// Add default for a key that will not be overridden by config or flags
SetDefault("timeout", 5)
config.SetValue("host", "config.host")
config.SetValue("port", 9090)
Parse()
fmt.Printf("Host: %s (from flag)\n", GetString("host")) // Highest precedence
fmt.Printf("Port: %d (from config)\n", GetInt("port")) // Middle precedence
fmt.Printf("Debug: %t (from flag)\n", GetBool("debug")) // Flag overriding default
fmt.Printf("Timeout: %d (from default)\n", GetInt("timeout")) // Lowest precedence
Output: Host: flag.host (from flag) Port: 9090 (from config) Debug: false (from flag) Timeout: 5 (from default)
Index ¶
- Variables
- func AllKeys() []string
- func Debug()
- func GetBool(key string) bool
- func GetDuration(key string) time.Duration
- func GetFloat64(key string) float64
- func GetInt(key string) int
- func GetInt16(key string) int16
- func GetInt32(key string) int32
- func GetInt64(key string) int64
- func GetInt8(key string) int8
- func GetString(key string) string
- func GetStringMapString(key string) map[string]string
- func GetStringSet(key string) map[string]bool
- func GetStringSlice(key string) []string
- func GetUint(key string) uint
- func GetUint16(key string) uint16
- func GetUint32(key string) uint32
- func GetUint64(key string) uint64
- func GetUint8(key string) uint8
- func Init(filename string) error
- func IsSet(key string) bool
- func Parse()
- func ParseWithError() error
- func Reset()
- func SetDefault(key string, value interface{})
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var (
ErrInitFailed = errors.New("mflag: Init failed")
)
Functions ¶
func AllKeys ¶
func AllKeys() []string
AllKeys returns all keys in the config, flattened with dot notation. Must be called after Parse.
func Debug ¶
func Debug()
Debug prints all configuration values to standard output. Must be called after Parse.
func GetBool ¶
GetBool returns the value associated with the key as a boolean. Must be called after Parse.
func GetDuration ¶
GetDuration returns the value associated with the key as a time.Duration. Must be called after Parse.
func GetFloat64 ¶
GetFloat64 returns the value associated with the key as a float64. Must be called after Parse.
func GetInt ¶
GetInt returns the value associated with the key as an integer. Must be called after Parse.
func GetInt16 ¶
GetInt16 returns the value associated with the key as an int16. Must be called after Parse.
func GetInt32 ¶
GetInt32 returns the value associated with the key as an int32. Must be called after Parse.
func GetInt64 ¶
GetInt64 returns the value associated with the key as an int64. Must be called after Parse.
func GetInt8 ¶
GetInt8 returns the value associated with the key as an int8. Must be called after Parse.
func GetString ¶
GetString returns the value associated with the key as a string. It returns the final value after merging defaults, config file, and flags. Must be called after Parse.
func GetStringMapString ¶
GetStringMapString returns the value associated with the key as a map of strings. Must be called after Parse.
func GetStringSet ¶
GetStringSet returns the string slice value associated with a key as a map[string]bool (a set). This is useful for efficiently checking for the existence of an item in a list, like a feature flag. Must be called after Parse.
func GetStringSlice ¶
GetStringSlice returns the value associated with the key as a slice of strings. Must be called after Parse.
func GetUint ¶
GetUint returns the value associated with the key as a uint. Must be called after Parse.
func GetUint16 ¶
GetUint16 returns the value associated with the key as a uint16. Must be called after Parse.
func GetUint32 ¶
GetUint32 returns the value associated with the key as a uint32. Must be called after Parse.
func GetUint64 ¶
GetUint64 returns the value associated with the key as a uint64. Must be called after Parse.
func GetUint8 ¶
GetUint8 returns the value associated with the key as a uint8. Must be called after Parse.
func Init ¶
Init loads configuration from a YAML file at the given path. It should be called after setting defaults and before parsing flags.
func Parse ¶
func Parse()
Parse parses command-line arguments and merges all configuration sources. It MUST be called after setting defaults and calling Init. It dynamically creates command-line flags for all known configuration keys. Precedence: Flags > Config File > Defaults.
func ParseWithError ¶
func ParseWithError() error
ParseWithError is similar to Parse but returns an error on failure. This allows for more granular error handling. Note: This function creates its own temporary flag set and does not parse flags defined globally via the standard `flag` package.
func SetDefault ¶
func SetDefault(key string, value interface{})
SetDefault sets a default value for a key. Defaults have the lowest precedence and are overridden by config files and flags. It should be called before Init and Parse.
Types ¶
This section is empty.