Documentation
¶
Overview ¶
Package goflagbuilder constructs command line flags and a file parser to manipulate a given structure. It uses reflection to traverse a potentially hierarchical object of structs and maps and install handlers in Go's standard flag package. Constructed parsers scan given documents line by line and apply any key/value pairs found.
Constructed flags have the form Foo=..., Bar=...,Obj.Field=... and so on. Maps and exposed fields of structs with primitive types are consumed, so in this case Foo and Bar might be map keys or public struct fields to a primitive. Nested maps and structs are followed, producing dot-notation hierarchical keys such as Obj.Field.
Primitive types understood by goflagbuilder include bool, float64, int64, int, string, uint64, and uint.
Primitive fields in the given object and sub-objects must be settable. In general this means structs should be passed in as pointers. Maps may also be set directly.
Struct fields may have a "help" struct tag, which will set the usage string for the corresponding flag.
Example (Extended) ¶
package main import ( "flag" "fmt" "log" "strings" "github.com/BellerophonMobile/goflagbuilder/v2/conf" "github.com/BellerophonMobile/goflagbuilder/v2/env" ) type foocomponent struct { Domain string Port int } type nestedstruct struct { Index float64 } type barcomponent struct { Label string Nested *nestedstruct } func main() { // Create some sample data masterconf := make(map[string]interface{}) masterconf["Foo"] = &foocomponent{ Domain: "example.com", Port: 9999, } masterconf["Bar"] = &barcomponent{ Label: "Bar Component", Nested: &nestedstruct{ Index: 79.3, }, } // Construct the flags if err := From(masterconf); err != nil { log.Fatal("CONSTRUCTION ERROR:", err) } // Read from a config file reader := strings.NewReader(` # Comment Foo.Port = 1234 Bar.Nested.Index=7.9 # SuccesS! `) if err := conf.Parse(reader, nil); err != nil { log.Fatal("Error:", err) } // Override settings from the environment if err := env.Parse(nil); err != nil { log.Fatal("Error:", err) } // Override settings from the command line flag.Parse() // Output our data fmt.Println(masterconf["Foo"].(*foocomponent).Port) fmt.Println(masterconf["Bar"].(*barcomponent).Nested.Index) }
Output: 1234 7.9
Example (Simple) ¶
package main import ( "flag" "log" ) type server struct { Domain string Port int } func main() { myserver := &server{} // Construct the flags if err := From(myserver); err != nil { log.Fatal("Error: " + err.Error()) } // Read from the command line to establish the param flag.Parse() }
Output:
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type FlagSet ¶
FlagSet is the interface for handling flags identified by GoFlagBuilder. FlagSet objects from Go's standard flag package meet this specification and are the intended primary target, in addition to an internal facade in front of the flag package's top level function, and the GoFlagBuilder Parser.
Directories
¶
Path | Synopsis |
---|---|
Package conf provides a simple configuration file format that reads values backed by a flag.FlagSet.
|
Package conf provides a simple configuration file format that reads values backed by a flag.FlagSet. |
Package env provides a means to read environment variables into values backed by a flag.FlagSet.
|
Package env provides a means to read environment variables into values backed by a flag.FlagSet. |