Documentation ¶
Overview ¶
A unified configuration/environment/cli parser for Go, with a focus on simplicity and ease of use.
Example ¶
package main import ( "fmt" "os" "github.com/zafnz/configape" ) func main() { cfg := struct { Foo string `name:"foo" default:"baz" help:"This is the help for foo"` Bar int `default:"42"` Test bool `default:"true"` Verbosity int `name:"verbose" short:"v" default:"0" cfgtype:"counter" help:"Verbosity level"` Baz string }{} // Fake the os arguments, here we use --no-test to override // the default true value of test, and we increment verbosity twice os.Args = []string{"test", "--foo", "bar", "--no-test", "-v", "-v"} os.Setenv("CFG_BAZ", "environment") configape.Apply(&cfg, nil) fmt.Printf("Foo: %s\n", cfg.Foo) fmt.Printf("Bar: %d\n", cfg.Bar) fmt.Printf("Test: %t\n", cfg.Test) fmt.Printf("Baz: %s\n", cfg.Baz) fmt.Printf("Verbosity: %d\n", cfg.Verbosity) }
Output: Foo: bar Bar: 42 Test: false Baz: environment Verbosity: 2
Example (Complex) ¶
package main import ( "fmt" "os" "github.com/zafnz/configape" ) // This example shows how to use configape to parse a complex configuration // Define a struct to hold the configuration type ComplexConfig struct { // The name tag is used to map the configuration to the struct // field. If the name tag is not specified, the field name is // used instead. // The default tag is used to specify the default value for the // field. // The help tag is used to specify the help text for the field. // The cfgtype tag is used to specify the type of the field. // The short tag is used to specify the short name for the field. // The env tag is used to specify the environment variable name // for the field. // The required tag is used to specify that the field must be // specified. // This can be set with --simple-string or CFG_SIMPLE_STRING // or "SimpleString" in the config file. SimpleString string `help:"This is the help for simple string"` SimpleInt int `default:"42"` // This will default to 42 RequiredString string `required:"true"` // This must be specified Different string `name:"something"` // This can be set with --something or CFG_SOMETHING // Here this can be set with --short or -s. ShortFlag bool `name:"short" short:"s" help:"This is the help for the short flag"` NotOnCli string `cli:"-"` // This will not be on the cli, but is settable in the config file ConfigFile string `cli:"config" cfgtype:"configfile" help:"Load config from this file"` // Example of a sub-configuration. Here all of these settings // can be set with a prefix of "--database--" on the cli, or // as an object in the config file. Database struct { // Set with --database-host or CFG_DATABASE_HOST Host string `help:"Database host"` // Set with --database-port or CFG_DATABASE_PORT Port int `help:"Database port"` } } // Here we can see how to apply the configuration to the struct. // Note that because we haven't specified a specific name, we can // use camel case, or underscores, or dashes, for those fields. var cfgFile = ` { "simple_string": "foo", "SimpleInt": 69, "required_string": "bar", "not_on_cli": "set in config file" }` func main() { // Write out the config as a temporary file to use fh, _ := os.CreateTemp("", "configape") defer os.Remove(fh.Name()) fh.WriteString(cfgFile) os.Args = []string{"test", "--something", "set on cli", "--database-host", "dbhost", "-s", "--config", fh.Name()} cfg := ComplexConfig{} err := configape.Apply(&cfg, nil) if err != nil { fmt.Println(err) return } fmt.Printf("SimpleString: %s\n", cfg.SimpleString) fmt.Printf("SimpleInt: %d\n", cfg.SimpleInt) fmt.Printf("RequiredString: %s\n", cfg.RequiredString) fmt.Printf("Different: %s\n", cfg.Different) fmt.Printf("ShortFlag: %t\n", cfg.ShortFlag) fmt.Printf("NotOnCli: %s\n", cfg.NotOnCli) fmt.Printf("Database.Host: %s\n", cfg.Database.Host) }
Output: SimpleString: foo SimpleInt: 69 RequiredString: bar Different: set on cli ShortFlag: true NotOnCli: set in config file Database.Host: dbhost
Example (Help) ¶
package main import ( "fmt" "github.com/zafnz/configape" ) // This example shows how to use configape to parse a complex configuration // Define a struct to hold the configuration type HelpExampleConfig struct { // This is the same as the complex example, see that for an explanation // of all the tags. SimpleString string `help:"This is the help for simple string"` SimpleInt int `default:"42"` RequiredString string `required:"true"` Different string `name:"something"` ShortFlag bool `name:"short" short:"s" help:"This is the help for the short flag"` NotOnCli string `cli:"-"` ConfigFile string `cli:"config" cfgtype:"configfile" help:"Load config from this file"` Database struct { // Set with --database-host or CFG_DATABASE_HOST Host string `help:"Database host to connect to"` // Set with --database-port or CFG_DATABASE_PORT Port int `help:"Database port to use with the database host"` } } func main() { cfg := HelpExampleConfig{} help, _ := configape.Help(cfg, &configape.Options{Name: "example"}) fmt.Println(help) }
Output: example (v0.0.0) --simple-string <SimpleString> This is the help for simple string --simple-int <SimpleInt> (default: 42) --required-string <RequiredString> --something <something> --short, -s This is the help for the short flag --config <ConfigFile> Load config from this file database --database-host <Host> Database host to connect to --database-port <Port> Database port to use with the database host
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Apply ¶
Apply the configuration to the provided cfg struct, using the options provided.
Example ¶
package main import ( "fmt" "os" "github.com/zafnz/configape" ) func main() { cfg := struct { Foo string `name:"foo" default:"baz" help:"This is the help for foo"` Bar int `default:"42"` Test bool `default:"true"` Verbosity int `name:"verbose" short:"v" default:"0" cfgtype:"counter" help:"Verbosity level"` }{} // Fake the os arguments, here we use --no-test to override // the default true value of test, and we increment verbosity twice os.Args = []string{"test", "--foo", "bar", "--no-test", "-v", "-v"} configape.Apply(&cfg, nil) fmt.Printf("Foo: %s\n", cfg.Foo) fmt.Printf("Bar: %d\n", cfg.Bar) fmt.Printf("Test: %t\n", cfg.Test) fmt.Printf("Verbosity: %d\n", cfg.Verbosity) }
Output: Foo: bar Bar: 42 Test: false Verbosity: 2
func Help ¶
Returns a string suitable as the output for the help command. You can override the default output using the HelpWriter or Help function in the Options struct. Or you can disable help entirely with DisableHelp
Example ¶
package main import ( "os" "github.com/zafnz/configape" ) func main() { cfg := struct { Foo string `name:"foo" default:"baz" help:"This is the help for foo"` }{} options := configape.Options{ HelpWriter: os.Stdout, // By default goes to Stderr Name: "my-prog", Version: "1.2.3", } os.Args = []string{"test", "--help"} configape.Apply(&cfg, &options) }
Output: my-prog (v1.2.3) --foo <foo> (default: baz) This is the help for foo
Types ¶
type Options ¶
type Options struct { ConfigFilename string // Name of the config file to use ConfigFileType string // The file type, defaults to json and determines the file extension. EnvironmentPrefix string // Prefix for environment variables, empty string defaults to CFG_, if you really want no prefix, set to ! (not recommended) UseSingleDashArguments bool // If set, then arguments are expected as "-foo bar" instead of "--foo bar" (not recommended) Help func(str string) // If set, then this function will be called when the help flag is set. HelpHeader string // Help text that is prefixed to the help output. HelpWriter io.Writer // Where to write the help output, defaults to os.Stderr DisableHelpOnMissingRequired bool // If set, then the help will not be printed if a required setting is missing. DisableHelp bool // Disable the help flag DisableVersion bool // Disable the version flag Name string // Name of the program, used in the help output. Defaults to os.Args[0] Version string // Version of the program, used in the help output. Defaults to "v0.0.0" DisableEnviornment bool // Disable environment variables DisableConfigFile bool // Disable config file parsing DisableCommandLine bool // Disable command line parsing AllowUnknownConfigFileKeys bool // If set, then unknown keys in the config file will not cause an error. // contains filtered or unexported fields }
Options on how Config Ape should work.
Click to show internal directories.
Click to hide internal directories.