Documentation
¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Parse ¶
Parse is a convenience function to parse configuration from CLI flags, environment variables, and struct tags.
Example (MultipleFlags) ¶
package main
import (
"fmt"
"os"
"github.com/bherbruck/configlib"
)
func main() {
// Set up environment and args for example
os.Clearenv()
os.Args = []string{"myapp", "-H", "example.com", "-p", "3000", "-d"}
type Config struct {
Host string `env:"HOST" flag:"host,H" default:"localhost" desc:"Server host"`
Port int `env:"PORT" flag:"port,p" default:"8080" desc:"Server port"`
Debug bool `env:"DEBUG" flag:"debug,d" desc:"Enable debug mode"`
}
var cfg Config
err := configlib.Parse(&cfg)
if err != nil {
fmt.Println("Error:", err)
return
}
fmt.Printf("Host: %s\n", cfg.Host)
fmt.Printf("Port: %d\n", cfg.Port)
fmt.Printf("Debug: %v\n", cfg.Debug)
}
Output: Host: example.com Port: 3000 Debug: true
Example (MultipleMissingFields) ¶
package main
import (
"fmt"
"os"
"github.com/bherbruck/configlib"
)
func main() {
// Clear environment to ensure no values are set
os.Clearenv()
// Reset CLI args
os.Args = []string{"test"}
// Define a config struct with multiple required fields
type Config struct {
APIKey string `env:"API_KEY" flag:"api-key" required:"true"`
DatabaseURL string `env:"DATABASE_URL" flag:"database-url" required:"true"`
SecretKey string `env:"SECRET_KEY" flag:"secret-key" required:"true"`
Port int `env:"PORT" flag:"port" default:"8080"`
}
var cfg Config
err := configlib.Parse(&cfg)
if err != nil {
fmt.Println(err)
}
}
Output: missing required fields: - APIKey (env: API_KEY, flag: --api-key) - DatabaseURL (env: DATABASE_URL, flag: --database-url) - SecretKey (env: SECRET_KEY, flag: --secret-key)
Types ¶
type Option ¶
type Option func(*Parser)
Option is a functional option for configuring a Parser
func WithDisableAutoEnv ¶
func WithDisableAutoEnv() Option
WithDisableAutoEnv disables automatic generation of environment variable names
func WithDisableAutoFlag ¶
func WithDisableAutoFlag() Option
WithDisableAutoFlag disables automatic generation of CLI flag names
func WithEnvPrefix ¶
WithEnvPrefix sets a prefix for all environment variable names
type Parser ¶
type Parser struct {
// contains filtered or unexported fields
}
func ParseWithHelp ¶
ParseWithHelp is like Parse but returns a parser instance that can be used to print help
func (*Parser) PrintHelp ¶
func (p *Parser) PrintHelp()
PrintHelp prints a formatted help message showing all configuration options
Example ¶
package main
import (
"os"
"github.com/bherbruck/configlib"
)
func main() {
type Config struct {
Host string `env:"HOST" flag:"host" default:"localhost" desc:"Server host"`
Port int `env:"PORT" flag:"port" default:"8080" desc:"Server port"`
Debug bool `env:"DEBUG" flag:"debug" desc:"Enable debug mode"`
}
// Clear args to avoid triggering help
os.Args = []string{"myapp"}
var cfg Config
parser, _ := configlib.ParseWithHelp(&cfg)
parser.PrintHelp()
}
Output: Usage: myapp [options] Options: --host <value> Server host (default: localhost) --port <value> Server port (default: 8080) --debug Enable debug mode -h, --help Show this help message
Click to show internal directories.
Click to hide internal directories.