Documentation ΒΆ
Overview ΒΆ
Package flagstruct is a simple package that allows you to express flag and environment variable based configuration using structs and struct tagging.
Structures ΒΆ
flagstruct works on arbitrary structures with struct tagging. The following struct tags are supported:
- "flag": Maps the struct member to a command line flag.
- "env": Maps the struct member to an environment variable.
- "usage": Specifies the usage string to use for the flag.
Default values are derived from the value of the member in the struct. To see exactly how this works, check out the package example.
Example (Advanced) ΒΆ
This snippet shows how to use flag and environment parsing without using the Configure helper that couples configuration with parsing.
package main import ( "github.com/Benzinga/flagstruct" ) func main() { conf := struct { Compress bool `flag:"z" usage:"whether or not to use compression" env:"COMPRESS"` OutputFn string `flag:"out" usage:"output ~filename~"` }{ Compress: true, } // Set up flags. flagstruct.Struct(&conf) // Parse environment. flagstruct.ParseEnv() // Parse flags. flagstruct.Parse() }
Output:
Example (Simple) ΒΆ
This snippet shows how to use flag and environment variable parsing with flagstruct.
package main import ( "github.com/Benzinga/flagstruct" ) func main() { conf := struct { Compress bool `flag:"z" usage:"whether or not to use compression" env:"COMPRESS"` OutputFn string `flag:"out" usage:"output ~filename~"` }{ Compress: true, } // Parse flags based on structure. flagstruct.Configure(&conf) }
Output:
Index ΒΆ
- Variables
- func Configure(conf interface{}) error
- func Parse() error
- func ParseEnv() error
- func PrintStruct(conf interface{})
- func Struct(conf interface{}) error
- type FlagSet
- func (s *FlagSet) Configure(conf interface{}, arguments []string) error
- func (s *FlagSet) MakeStructUsage(conf interface{}) func()
- func (s *FlagSet) MakeUsage() func()
- func (s *FlagSet) ParseEnv() error
- func (s *FlagSet) PrintDefaults()
- func (s *FlagSet) PrintStruct(conf interface{})
- func (s *FlagSet) SetOutput(output io.Writer)
- func (s *FlagSet) Struct(conf interface{}) error
- type Value
Examples ΒΆ
Constants ΒΆ
This section is empty.
Variables ΒΆ
var CommandLine = &FlagSet{flag.CommandLine, os.Args[0], flag.ExitOnError, nil, map[string]Value{}}
CommandLine is the default set of command-line flags, parsed from os.Args.
Functions ΒΆ
func Configure ΒΆ
func Configure(conf interface{}) error
Configure sets up enhanced usage help, loads a structure, parses environment and parses flags.
func PrintStruct ΒΆ
func PrintStruct(conf interface{})
PrintStruct prints configuration flags based on the struct passed to `conf`.
Types ΒΆ
type FlagSet ΒΆ
A FlagSet represents a set of defined flags.
func NewFlagSet ΒΆ
func NewFlagSet(name string, errorHandling flag.ErrorHandling) *FlagSet
NewFlagSet returns a new, empty flag set with the specified name and error handling property.
func (*FlagSet) Configure ΒΆ
Configure sets up enhanced usage help, loads a structure, parses environment and parses flags.
func (*FlagSet) MakeStructUsage ΒΆ
func (s *FlagSet) MakeStructUsage(conf interface{}) func()
MakeStructUsage creates a usage function from a struct.
func (*FlagSet) MakeUsage ΒΆ
func (s *FlagSet) MakeUsage() func()
MakeUsage creates a usage function that prints the flags.
func (*FlagSet) PrintDefaults ΒΆ
func (s *FlagSet) PrintDefaults()
PrintDefaults prints to standard error the default values of all defined command-line flags in the set. This is copied from flag, adjusted to allow ~ quotes in default values.
func (*FlagSet) PrintStruct ΒΆ
func (s *FlagSet) PrintStruct(conf interface{})
PrintStruct prints flags based on the struct passed to `conf`.