Documentation ¶
Overview ¶
Package optional provides a cascading option resolver for cli applications
Usage: Create a resolver by calling New(&config). Config should be a struct Add sources by calling AddSource.
Example ¶
type Config struct { ConfigPath string `default:"/etc/test/config.json" arg:"c" arg:"conf-path"` Name string `arg:"n" arg:"name" default:"New User"` Goodbye bool `default:"false" arg:"b" arg:"!h" arg:"goodbye"` } config := Config{} opts := New(&config) opts.AddSourceFast(0, opts.StructTags("default")) // Fast because we dont read until another AddSource opts.AddSource(2, opts.Arguments( []string{"optional_test", "-n", "Jamie", "--conf-path", "c:/test.conf"}, )) //normaly opts.AddSource(opts.Arguments(os.Args)) c, err := ioutil.ReadFile(config.ConfigPath) //Open JSON on disk if err != nil { fmt.Println(err.Error()) } else { m := make(map[string]interface{}) json.Unmarshal(c, &m) opts.AddSource(1, Wrapped{m}) } //Config should be set as: // ConfigPath: "D:/test.conf" (from Arguments) // Name: "Jamie" (from Arguments) // Goodbye: false (from Defaults) h := "Hello " if config.Goodbye { h = "Goodbye " } fmt.Println(h + config.Name) fmt.Println(config.ConfigPath) //Output will be: Hello Jamie (Unless you changed the conf)
Output:
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type ArgumentsOptionSource ¶
type ArgumentsOptionSource struct {
// contains filtered or unexported fields
}
ArgumentsOptionSource implements OptionSource, pulling data from the passed argument array
func (ArgumentsOptionSource) Get ¶
func (s ArgumentsOptionSource) Get(key string) (interface{}, bool)
Get implements OptionSource
type OptionSource ¶
type OptionSource interface { //Get will be given a path, period deliminated and should return the value related to it Get(key string) (value interface{}, ok bool) }
A OptionSource can be passed to AddSource to add a source for values
type Opts ¶
type Opts struct {
// contains filtered or unexported fields
}
Opts holds cascaded sources
func (*Opts) AddSource ¶
func (o *Opts) AddSource(i int, s OptionSource)
AddSource adds a source to the opt, 'i' being the priority level (higher number = higher priority)
func (*Opts) AddSourceFast ¶
func (o *Opts) AddSourceFast(i int, s OptionSource)
AddSourceFast adds a source to the opt without updating the parent struct
Example ¶
type Config struct { A string `default:"default"` B string `default:"default" someOtherDefault:"OtherDefault"` C string `default:"default"` } config := Config{ A: "Allocate", // Will be overwritten } opts := New(&config) // Does not change struct fmt.Println(config) //{Allocate } opts.AddSource(1, opts.StructTags("default")) opts.AddSourceFast(2, opts.StructTags("someOtherDefault")) fmt.Println(config) //{default default default} opts.Recalc() fmt.Println(config) //{default OtherDefault default}
Output: {Allocate } {default default default} {default OtherDefault default}
func (*Opts) Arguments ¶
func (o *Opts) Arguments(s []string) ArgumentsOptionSource
Arguments creates a (Arguments)OptionSource pulling data from the passed argument array.
Parser rules shorthand (1 char) has a single dash longhand (1+char ) has two dashes shorthand bools can be strung together -abcd will set a,b,c, and d to true shorthand bools with names begining with a '!' will be inverted ie: Fast bool `args:"Fast" args:"f" args:"!s"` '--' stops parsing
func (*Opts) Recalc ¶
func (o *Opts) Recalc()
Recalc sets all of the values in the struct to the correct values This is called automatically after AddSource
func (*Opts) StructTags ¶
func (o *Opts) StructTags(key string) StructTagsOptionSource
StructTags creates a (StructTags)OptionSource pulling from struct's tags
type StructTagsOptionSource ¶
type StructTagsOptionSource struct {
// contains filtered or unexported fields
}
StructTagsOptionSource implements OptionSource, pulling data from Struct tags
func (StructTagsOptionSource) Get ¶
func (s StructTagsOptionSource) Get(key string) (interface{}, bool)
Get implements OptionSource